Commit bd45787b authored by frey_m's avatar frey_m
Browse files

Merge branch 'master' of gitlab.psi.ch:OPAL/src into sampler-memory-reduction

parents 39a99b95 9c617167
...@@ -83,20 +83,20 @@ protected: ...@@ -83,20 +83,20 @@ protected:
/// Write the variator config file /// Write the variator config file
void writeVariatorCfg(); void writeVariatorCfg();
// implementing poller hooks ///@{ implementing poller hooks
bool onMessage(MPI_Status status, size_t length); virtual bool onMessage(MPI_Status status, size_t length);
void postPoll(); virtual void postPoll();
void setupPoll() {} virtual void setupPoll() {}
void prePoll() { virtual void prePoll() {
// std::ostringstream debug; // std::ostringstream debug;
// debug << "IN PRE POLL: "; // debug << "IN PRE POLL: ";
// debug << getStateString(curState_m) << std::endl; // debug << getStateString(curState_m) << std::endl;
// progress_->log(debug); // progress_->log(debug);
} }
void onStop() {} virtual void onStop() {}
///@}
// helper sending evaluation requests to the pilot /// helper sending evaluation requests to the pilot
void dispatch_forward_solves(); void dispatch_forward_solves();
...@@ -140,16 +140,16 @@ private: ...@@ -140,16 +140,16 @@ private:
Variator_t; Variator_t;
boost::scoped_ptr<Variator_t> variator_m; boost::scoped_ptr<Variator_t> variator_m;
std::vector<unsigned int> pp_all; std::vector<unsigned int> pp_all; ///< IDs of population
std::vector<unsigned int> parent_queue_; std::vector<unsigned int> parent_queue_; ///< IDs that will make new offspring
std::set<unsigned int> archive_; // std::set<unsigned int> archive_;
std::set<unsigned int> to_selector_; std::set<unsigned int> to_selector_; ///< Successfully run IDs to go into population
// to compute the front // to compute the front
std::vector<int> copies; std::vector<int> copies; ///< number of individuals in the n-th front
std::vector<double> dist; std::vector<double> dist;
std::vector< std::vector<int> > front; std::vector< std::vector<int> > front; ///< individuals in each front
std::map<size_t, double> fitness_; std::map<size_t, double> fitness_; ///< map between id and fitness (sum of front number and dist)
/// communicator bundle for the optimizer /// communicator bundle for the optimizer
Comm::Bundle_t comms_; Comm::Bundle_t comms_;
......
...@@ -253,7 +253,7 @@ bool FixedPisaNsga2<CO, MO>::onMessage(MPI_Status status, size_t length) { ...@@ -253,7 +253,7 @@ bool FixedPisaNsga2<CO, MO>::onMessage(MPI_Status status, size_t length) {
job_trace_->log(dump); job_trace_->log(dump);
std::cout << "NON-EXISTING JOB with ID = " << jid << std::endl; std::cout << "NON-EXISTING JOB with ID = " << jid << std::endl;
throw OptPilotException("FixedPisaNsga2::onMessage", throw OptPilotException("FixedPisaNsga2::onMessage",
"non-existing job"); "non-existing job");
} }
boost::shared_ptr<individual> ind = it->second; boost::shared_ptr<individual> ind = it->second;
...@@ -624,7 +624,7 @@ void FixedPisaNsga2<CO, MO>::runStateMachine() { ...@@ -624,7 +624,7 @@ void FixedPisaNsga2<CO, MO>::runStateMachine() {
typedef typename FixedPisaNsga2::Individual_t individual; typedef typename FixedPisaNsga2::Individual_t individual;
typename std::map<unsigned int, boost::shared_ptr<individual> > typename std::map<unsigned int, boost::shared_ptr<individual> >
::iterator it; ::iterator it;
for(it = variator_m->population()->begin(); for(it = variator_m->population()->begin();
it != variator_m->population()->end(); it++) { it != variator_m->population()->end(); it++) {
//archive_.insert(it->first); //archive_.insert(it->first);
pp_all.push_back(it->first); pp_all.push_back(it->first);
...@@ -687,12 +687,13 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() { ...@@ -687,12 +687,13 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() {
typename std::map<unsigned int, boost::shared_ptr<individual> >::iterator it; typename std::map<unsigned int, boost::shared_ptr<individual> >::iterator it;
it = variator_m->population()->begin(); it = variator_m->population()->begin();
// find maximum length of ID
auto maxID = it->first; auto maxID = it->first;
for(it ++; it != variator_m->population()->end(); it++) { for(it ++; it != variator_m->population()->end(); it++) {
if (it->first > maxID) if (it->first > maxID)
maxID = it->first; maxID = it->first;
} }
size_t numDigits = std::to_string(maxID).length(); const size_t numDigits = std::to_string(maxID).length();
size_t next = 0, last = 0; size_t next = 0, last = 0;
std::string delimiter = ","; std::string delimiter = ",";
next = file_param_descr_.find(delimiter, last); next = file_param_descr_.find(delimiter, last);
...@@ -711,7 +712,7 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() { ...@@ -711,7 +712,7 @@ void FixedPisaNsga2<CO, MO>::dumpPopulationToFile() {
file.precision(6); file.precision(6);
file << std::scientific; file << std::scientific;
for(it = variator_m->population()->begin(); for(it = variator_m->population()->begin();
it != variator_m->population()->end(); it++) { it != variator_m->population()->end(); it++) {
file << std::setw(numDigits + 1) << it->first << " "; file << std::setw(numDigits + 1) << it->first << " ";
...@@ -862,38 +863,35 @@ void FixedPisaNsga2<CO, MO>::calcFitnesses() ...@@ -862,38 +863,35 @@ void FixedPisaNsga2<CO, MO>::calcFitnesses()
int i, j, l; int i, j, l;
int size = pp_all.size(); int size = pp_all.size();
std::vector<int> d(size); std::vector<int> d(size,0); // 1 if dominated by another ind, 0 if not
std::vector<int> f(size); std::vector<int> f(size,1); // 1 if not yet in a front, -1 if in a front
/* initialize fitness and strength values */ /* initialize fitness and strength values */
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
fitness_.insert(std::pair<size_t, double>(pp_all[i], 0.0)); fitness_.insert(std::pair<size_t, double>(pp_all[i], 0.0));
d[i] = 1;
f[i] = 1;
copies[i] = 0; copies[i] = 0;
} }
/* calculate strength values */ /* calculate strength values */
int num = size; int num = size; // number of individuals not yet in a front
for (l = 0; l < size; l++) { for (l = 0; l < size; l++) { // loop over fronts, there can be maximally `size` fronts
/* find next front */ /* find next front */
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
d[i] = 0; d[i] = 0; // reset dominate flag
if (f[i] != -1) { if (f[i] == -1) continue; // don't consider if already in a front
for (j = 0; j < i && d[i] == 0; j++) for (j = 0; j < size && j != i; j++) {
if (f[j] != -1) if (f[j] == -1) continue;
if (dominates(pp_all[j], pp_all[i])) if (dominates(pp_all[j], pp_all[i])) {
d[i] = 1; d[i] = 1;
for(j = i+1; j < size && d[i] == 0; j++) break;
if (f[j] != -1) }
if (dominates(pp_all[j], pp_all[i]))
d[i] = 1;
} }
} }
/* extract front */ /* extract front */
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
if (f[i] != -1 && d[i] == 0) { if (f[i] != -1 && d[i] == 0) {
// add to front
fitness_[pp_all[i]] = l; fitness_[pp_all[i]] = l;
f[i] = -1; f[i] = -1;
num--; num--;
...@@ -982,18 +980,19 @@ void FixedPisaNsga2<CO, MO>::environmentalSelection() { ...@@ -982,18 +980,19 @@ void FixedPisaNsga2<CO, MO>::environmentalSelection() {
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
fitness_[pp_all[i]] += 1.0 / dist[i]; fitness_[pp_all[i]] += 1.0 / dist[i];
// get alpha_m fittest individuals
for (size_t i = 0; i < alpha_m; i++) { for (size_t i = 0; i < alpha_m; i++) {
int min = i; int min = i;
for (int j = i + 1; j < size; j++) { for (int j = i + 1; j < size; j++) {
if (fitness_[pp_all[j]] < fitness_[pp_all[min]]) if (fitness_[pp_all[j]] < fitness_[pp_all[min]])
min = j; min = j;
} }
// swap
unsigned int p_min = pp_all[min]; unsigned int p_min = pp_all[min];
pp_all[min] = pp_all[i]; pp_all[min] = pp_all[i];
pp_all[i] = p_min; pp_all[i] = p_min;
} }
// erase others
pp_all.erase(pp_all.begin() + alpha_m, pp_all.end()); pp_all.erase(pp_all.begin() + alpha_m, pp_all.end());
} }
...@@ -1003,12 +1002,12 @@ template< template <class> class CO, template <class> class MO > ...@@ -1003,12 +1002,12 @@ template< template <class> class CO, template <class> class MO >
void FixedPisaNsga2<CO, MO>::matingSelection() { void FixedPisaNsga2<CO, MO>::matingSelection() {
//FIXME: //FIXME:
int tournament = 2; int tournament = 1; ///< number of opponents
for (size_t i = 0; i < selector_mu_; i++) { for (size_t i = 0; i < selector_mu_; i++) {
int winner = irand(pp_all.size()); int winner = irand(pp_all.size());
for (int j = 1; j < tournament; j++) { for (int j = 0; j < tournament; j++) {
int opponent = irand(pp_all.size()); int opponent = irand(pp_all.size());
if (fitness_[pp_all[opponent]] < fitness_[pp_all[winner]] if (fitness_[pp_all[opponent]] < fitness_[pp_all[winner]]
|| winner == opponent) { || winner == opponent) {
......
...@@ -153,7 +153,7 @@ public: ...@@ -153,7 +153,7 @@ public:
if (viableA == true && viableB == true) { if (viableA == true && viableB == true) {
break; break;
} }
std::cout << "Individual not viable, I try again: iter= " << iter << std::endl; std::cout << "Individual not viable, I try again: iter= " << iter << std::endl;
iter++; iter++;
// if maximum number of tries then create new individual(s) // if maximum number of tries then create new individual(s)
if (iter > 100) { if (iter > 100) {
......
...@@ -21,3 +21,25 @@ void CmdArguments::split(std::string &name, ...@@ -21,3 +21,25 @@ void CmdArguments::split(std::string &name,
boost::trim(name); boost::trim(name);
boost::trim(value); boost::trim(value);
} }
char** CmdArguments::getArguments() const {
const unsigned int size = arguments_.size();
char** args = new char*[2 * size];
unsigned int i = 0;
auto it = arguments_.cbegin();
const auto end = arguments_.cend();
for (; it != end; ++ it) {
const std::string &key = it->first;
char* argname = new char[key.length() + 1];
strcpy(argname, key.c_str());
args[i ++] = argname;
const std::string &value = it->second;
char* argvalue = new char[value.length() + 1];
strcpy(argvalue, value.c_str());
args[i ++] = argvalue;
}
return args;
}
\ No newline at end of file
...@@ -31,6 +31,9 @@ public: ...@@ -31,6 +31,9 @@ public:
addArguments(argc, argv); addArguments(argc, argv);
} }
CmdArguments()
{}
~CmdArguments() ~CmdArguments()
{} {}
...@@ -84,6 +87,36 @@ public: ...@@ -84,6 +87,36 @@ public:
return value; return value;
} }
template <class T>
void addArgument(const std::string &name, const T &value) {
std::ostringstream oss;
oss << value;
if (arguments_.find(name) != arguments_.end()) {
throw OptPilotException("CmdArguments::addArgument",
"Argument '" + name + "' exists");
}
arguments_.insert(std::make_pair(name, oss.str()));
}
template <class T>
void replaceArgument(const std::string &name, const T &value) {
std::ostringstream oss;
oss << value;
if (arguments_.find(name) == arguments_.end()) {
arguments_.insert(std::make_pair(name, oss.str()));
return;
}
arguments_.at(name) = oss.str();
}
char** getArguments() const;
unsigned int getNumArguments() const {
return arguments_.size();
}
//template<> //template<>
//size_t getArg<size_t>(const std::string name, bool isFatal = false) { //size_t getArg<size_t>(const std::string name, bool isFatal = false) {
//return getArg<size_t>(name, 0, isFatal); //return getArg<size_t>(name, 0, isFatal);
...@@ -135,4 +168,4 @@ private: ...@@ -135,4 +168,4 @@ private:
typedef boost::shared_ptr<CmdArguments> CmdArguments_t; typedef boost::shared_ptr<CmdArguments> CmdArguments_t;
#endif #endif
\ No newline at end of file
...@@ -81,13 +81,13 @@ namespace Hypervolume { ...@@ -81,13 +81,13 @@ namespace Hypervolume {
POINT p = *(POINT*)v1; POINT p = *(POINT*)v1;
POINT q = *(POINT*)v2; POINT q = *(POINT*)v2;
#if hyper_opt == 1 #if hyper_opt == 1
for (int i = n - fr - 1; i >= 0; i--) for (int i = n - fr - 1; i >= 0; i--) {
#else #else
for (int i = n - 1; i >= 0; i--) for (int i = n - 1; i >= 0; i--) {
#endif #endif
if BEATS(p.objectives[i],q.objectives[i]) return 1; if BEATS(p.objectives[i],q.objectives[i]) return 1;
else else if BEATS(q.objectives[i],p.objectives[i]) return -1;
if BEATS(q.objectives[i],p.objectives[i]) return -1; }
return 0; return 0;
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
using namespace std; using namespace std;
void IlyaPogorelovFilter::apply(vector<double> &LineDensity) { void StencilFilter::apply(vector<double> &LineDensity) {
int size = LineDensity.size(); int size = LineDensity.size();
vector<double> temp1(size + 8, 0.0); vector<double> temp1(size + 8, 0.0);
vector<double> temp2(5, 0.0); vector<double> temp2(5, 0.0);
...@@ -29,7 +29,7 @@ void IlyaPogorelovFilter::apply(vector<double> &LineDensity) { ...@@ -29,7 +29,7 @@ void IlyaPogorelovFilter::apply(vector<double> &LineDensity) {
LineDensity[i] = (7. * (temp1[i+2] + temp1[i+6]) + 24. * (temp1[i+3] + temp1[i+5]) + 34.*temp1[i+4]) / 96.; LineDensity[i] = (7. * (temp1[i+2] + temp1[i+6]) + 24. * (temp1[i+3] + temp1[i+5]) + 34.*temp1[i+4]) / 96.;
} }
void IlyaPogorelovFilter::calc_derivative(vector<double> &LineDensity, const double &h) { void StencilFilter::calc_derivative(vector<double> &LineDensity, const double &h) {
vector<double> temp(LineDensity.begin(), LineDensity.end()); vector<double> temp(LineDensity.begin(), LineDensity.end());
const double N = LineDensity.size(); const double N = LineDensity.size();
LineDensity[0] = (-25.*temp[0] + 48.*temp[1] - 36.*temp[2] + 16.*temp[3] + temp[4]) / (12.*h); LineDensity[0] = (-25.*temp[0] + 48.*temp[1] - 36.*temp[2] + 16.*temp[3] + temp[4]) / (12.*h);
...@@ -40,5 +40,4 @@ void IlyaPogorelovFilter::calc_derivative(vector<double> &LineDensity, const dou ...@@ -40,5 +40,4 @@ void IlyaPogorelovFilter::calc_derivative(vector<double> &LineDensity, const dou
LineDensity[N-2] = (-temp[N-5] + 6.*temp[N-4] - 18.*temp[N-3] + 10.*temp[N-2] + 3.*temp[N-1]) / (12.*h); LineDensity[N-2] = (-temp[N-5] + 6.*temp[N-4] - 18.*temp[N-3] + 10.*temp[N-2] + 3.*temp[N-1]) / (12.*h);
LineDensity[N-1] = (3.*temp[N-5] - 16.*temp[N-4] + 36.*temp[N-3] - 48.*temp[N-2] + 25.*temp[N-1]) / (12.*h); LineDensity[N-1] = (3.*temp[N-5] - 16.*temp[N-4] + 36.*temp[N-3] - 48.*temp[N-2] + 25.*temp[N-1]) / (12.*h);
apply(LineDensity); apply(LineDensity);
} }
\ No newline at end of file
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
#include "Filters/Filter.h" #include "Filters/Filter.h"
class IlyaPogorelovFilter: public Filter { class StencilFilter: public Filter {
public: public:
IlyaPogorelovFilter() { ;} StencilFilter() { ;}
void apply(std::vector<double> &histogram); void apply(std::vector<double> &histogram);
void calc_derivative(std::vector<double> &histogram, const double &h); void calc_derivative(std::vector<double> &histogram, const double &h);
}; };
#endif // CLASSIC_STENCIL_HH #endif // CLASSIC_STENCIL_HH
\ No newline at end of file
...@@ -132,7 +132,7 @@ void CSRIGFWakeFunction::calculateLineDensity(PartBunchBase<double, 3> *bunch, s ...@@ -132,7 +132,7 @@ void CSRIGFWakeFunction::calculateLineDensity(PartBunchBase<double, 3> *bunch, s
(*fit)->apply(lineDensity_m); (*fit)->apply(lineDensity_m);
} }
dlineDensitydz_m.assign(lineDensity_m.begin(), lineDensity_m.end()); dlineDensitydz_m.assign(lineDensity_m.begin(), lineDensity_m.end());
filters_m.back()->calc_derivative(dlineDensitydz_m, meshInfo.second); diffOp_m.calc_derivative(dlineDensitydz_m, meshInfo.second);
} }
void CSRIGFWakeFunction::calculateGreenFunction(PartBunchBase<double, 3> *bunch, double meshSpacing) void CSRIGFWakeFunction::calculateGreenFunction(PartBunchBase<double, 3> *bunch, double meshSpacing)
......
...@@ -2,11 +2,13 @@ ...@@ -2,11 +2,13 @@
#define CSRIGFWAKEFUNCTION_HH #define CSRIGFWAKEFUNCTION_HH
#include "Solvers/WakeFunction.hh" #include "Solvers/WakeFunction.hh"
#include "Filters/Stencil.h"
#include <vector> #include <vector>
#include <string> #include <string>
class ElementBase; class ElementBase;
class Filter;
class CSRIGFWakeFunction: public WakeFunction { class CSRIGFWakeFunction: public WakeFunction {
public: public:
...@@ -27,6 +29,7 @@ private: ...@@ -27,6 +29,7 @@ private:
double calcPsi(const double &psiInitial, const double &x, const double &Ds) const; double calcPsi(const double &psiInitial, const double &x, const double &Ds) const;
std::vector<Filter *> filters_m; std::vector<Filter *> filters_m;
StencilFilter diffOp_m;
LineDensity lineDensity_m; LineDensity lineDensity_m;
LineDensity dlineDensitydz_m; LineDensity dlineDensitydz_m;
...@@ -61,4 +64,4 @@ private: ...@@ -61,4 +64,4 @@ private:
}; };
#endif //CSRIGFWAKEFUNCTION_HH #endif //CSRIGFWAKEFUNCTION_HH
\ No newline at end of file
...@@ -17,7 +17,6 @@ CSRWakeFunction::CSRWakeFunction(const std::string &name, ElementBase *element, ...@@ -17,7 +17,6 @@ CSRWakeFunction::CSRWakeFunction(const std::string &name, ElementBase *element,
filters_m(filters.begin(), filters.end()), filters_m(filters.begin(), filters.end()),
lineDensity_m(), lineDensity_m(),
dlineDensitydz_m(), dlineDensitydz_m(),
d2lineDensitydz2_m(),
bendRadius_m(0.0), bendRadius_m(0.0),
totalBendAngle_m(0.0) totalBendAngle_m(0.0)
{ } { }
...@@ -126,7 +125,7 @@ void CSRWakeFunction::calculateLineDensity(PartBunchBase<double, 3> *bunch, std: ...@@ -126,7 +125,7 @@ void CSRWakeFunction::calculateLineDensity(PartBunchBase<double, 3> *bunch, std:
} }
dlineDensitydz_m.assign(lineDensity_m.begin(), lineDensity_m.end()); dlineDensitydz_m.assign(lineDensity_m.begin(), lineDensity_m.end());
filters_m.back()->calc_derivative(dlineDensitydz_m, meshInfo.second); diffOp_m.calc_derivative(dlineDensitydz_m, meshInfo.second);
} }
void CSRWakeFunction::calculateContributionInside(size_t sliceNumber, double angleOfSlice, double meshSpacing) { void CSRWakeFunction::calculateContributionInside(size_t sliceNumber, double angleOfSlice, double meshSpacing) {
......
...@@ -2,11 +2,13 @@ ...@@ -2,11 +2,13 @@
#define CSRWAKEFUNCTION_HH #define CSRWAKEFUNCTION_HH
#include "Solvers/WakeFunction.hh" #include "Solvers/WakeFunction.hh"
#include "Filters/Stencil.h"
#include <vector> #include <vector>
#include <string> #include <string>
class ElementBase; class ElementBase;
class Filter;
class CSRWakeFunction: public WakeFunction { class CSRWakeFunction: public WakeFunction {
public: public:
...@@ -26,9 +28,10 @@ private: ...@@ -26,9 +28,10 @@ private:
double calcPsi(const double &psiInitial, const double &x, const double &Ds) const; double calcPsi(const double &psiInitial, const double &x, const double &Ds) const;
std::vector<Filter *> filters_m; std::vector<Filter *> filters_m;
StencilFilter diffOp_m;
LineDensity lineDensity_m; LineDensity lineDensity_m;
LineDensity dlineDensitydz_m; LineDensity dlineDensitydz_m;
LineDensity d2lineDensitydz2_m;
// Longitudinal CSR field. // Longitudinal CSR field.
std::vector<double> Ez_m; std::vector<double> Ez_m;
...@@ -55,4 +58,4 @@ private: ...@@ -55,4 +58,4 @@ private:
}; };
#endif //CSRWAKEFUNCTION_HH #endif //CSRWAKEFUNCTION_HH
\ No newline at end of file
...@@ -176,7 +176,7 @@ void OpalFilter::initOpalFilter() { ...@@ -176,7 +176,7 @@ void OpalFilter::initOpalFilter() {
} else if (type == "RELATIVEFFTLOWPASS") { } else if (type == "RELATIVEFFTLOWPASS") {
filter_m = new RelativeFFTLowPassFilter(std::abs(Attributes::getReal(itsAttr[THRESHOLD]))); filter_m = new RelativeFFTLowPassFilter(std::abs(Attributes::getReal(itsAttr[THRESHOLD])));
} else if (type == "STENCIL") { } else if (type == "STENCIL") {
filter_m = new IlyaPogorelovFilter(); filter_m = new StencilFilter();
} else { } else {
filter_m = 0; filter_m = 0;
INFOMSG("no filter attached" << endl); INFOMSG("no filter attached" << endl);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment