From 7a3c30cb5d35434005b16c5eb23237d79fe8f10c Mon Sep 17 00:00:00 2001 From: carlocamilloni Date: Wed, 17 Jun 2020 15:26:04 +0200 Subject: [PATCH 1/6] MetaI: added option to optimize sigma_max --- src/isdb/MetainferenceBase.cpp | 27 ++++++++++++++++++++++++++- src/isdb/MetainferenceBase.h | 5 +++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/isdb/MetainferenceBase.cpp b/src/isdb/MetainferenceBase.cpp index 1b48ac4f04..3e90057e0e 100644 --- a/src/isdb/MetainferenceBase.cpp +++ b/src/isdb/MetainferenceBase.cpp @@ -67,6 +67,7 @@ void MetainferenceBase::registerKeywords( Keywords& keys ) { keys.add("optional","DSIGMA","maximum MC move of the uncertainty parameter"); keys.add("compulsory","OPTSIGMAMEAN","NONE","Set to NONE/SEM to manually set sigma mean, or to estimate it on the fly"); keys.add("optional","SIGMA_MEAN0","starting value for the uncertainty in the mean estimate"); + keys.add("optional","SIGMA_MAX_STEPS", "Number of steps used to optimise SIGMA_MAX, before that the SIGMA_MAX value is used"); keys.add("optional","TEMP","the system temperature - this is only needed if code doesn't pass the temperature to plumed"); keys.add("optional","MC_STEPS","number of MC steps"); keys.add("optional","MC_CHUNKSIZE","MC chunksize"); @@ -124,6 +125,9 @@ MetainferenceBase::MetainferenceBase(const ActionOptions&ao): nsel_(1), iselect(0), optsigmamean_stride_(0), + N_optimized_step_(0), + optimized_step_(0), + sigmamax_opt_done_(false), decay_w_(1.) { parseFlag("DOSCORE", doscore_); @@ -195,6 +199,13 @@ MetainferenceBase::MetainferenceBase(const ActionOptions&ao): parse("OPTSIGMAMEAN", stringa_optsigma); if(stringa_optsigma=="NONE") do_optsigmamean_=0; else if(stringa_optsigma=="SEM") do_optsigmamean_=1; + else if(stringa_optsigma=="SEM_MAX") do_optsigmamean_=2; + + unsigned aver_max_steps=0; + parse("SIGMA_MAX_STEPS", aver_max_steps); + if(aver_max_steps==0&&do_optsigmamean_==2) aver_max_steps=averaging*1000; + if(aver_max_steps>0&&do_optsigmamean_<2) error("SIGMA_MAX_STEPS can only be used together with OPTSIGMAMEAN=SEM_MAX"); + if(aver_max_steps>0&&do_optsigmamean_==2) N_optimized_step_=aver_max_steps; vector read_sigma_mean_; parseVector("SIGMA_MEAN0",read_sigma_mean_); @@ -1367,7 +1378,21 @@ void MetainferenceBase::get_sigma_mean(const double fact, const double var_fact, sigma_mean2_tmp[0] = max_now; valueSigmaMean[0]->set(sqrt(sigma_mean2_tmp[0])); } - // endif sigma optimization + // endif sigma mean optimization + // start sigma max optimization + if(do_optsigmamean_>1&&!sigmamax_opt_done_) { + for(unsigned i=0;i(optimized_step_); + sigmamax_opt_done_=true; + for(unsigned i=0;i > > sigma_mean2_last_; unsigned optsigmamean_stride_; + // optimize sigma max + unsigned N_optimized_step_; + unsigned optimized_step_; + bool sigmamax_opt_done_; + std::vector sigma_max_est_; // average weights double decay_w_; From 13bc6336aaceeb413acc51bcde468641f8e218b2 Mon Sep 17 00:00:00 2001 From: carlocamilloni Date: Wed, 17 Jun 2020 16:09:54 +0200 Subject: [PATCH 2/6] astyle --- src/isdb/MetainferenceBase.cpp | 6 +++--- src/isdb/MetainferenceBase.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/isdb/MetainferenceBase.cpp b/src/isdb/MetainferenceBase.cpp index 3e90057e0e..ad94d657df 100644 --- a/src/isdb/MetainferenceBase.cpp +++ b/src/isdb/MetainferenceBase.cpp @@ -1381,15 +1381,15 @@ void MetainferenceBase::get_sigma_mean(const double fact, const double var_fact, // endif sigma mean optimization // start sigma max optimization if(do_optsigmamean_>1&&!sigmamax_opt_done_) { - for(unsigned i=0;i(optimized_step_); sigmamax_opt_done_=true; - for(unsigned i=0;i > > sigma_mean2_last_; unsigned optsigmamean_stride_; // optimize sigma max - unsigned N_optimized_step_; - unsigned optimized_step_; + unsigned N_optimized_step_; + unsigned optimized_step_; bool sigmamax_opt_done_; std::vector sigma_max_est_; From 49b72f3a35de49e86b1fd6a66c970ecae67a8ba6 Mon Sep 17 00:00:00 2001 From: carlocamilloni Date: Wed, 17 Jun 2020 16:22:21 +0200 Subject: [PATCH 3/6] MetaI: added missing resize --- src/isdb/MetainferenceBase.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/isdb/MetainferenceBase.cpp b/src/isdb/MetainferenceBase.cpp index ad94d657df..5f2e93e4fa 100644 --- a/src/isdb/MetainferenceBase.cpp +++ b/src/isdb/MetainferenceBase.cpp @@ -428,6 +428,8 @@ void MetainferenceBase::Initialise(const unsigned input) } } + sigma_max_est_.resize(sigma_max_.size(), 0.); + IFile restart_sfile; restart_sfile.link(*this); if(getRestart()&&restart_sfile.FileExist(status_file_name_)) { From 2f63cf4a4c16c72661c9f746f9e43fa09b828a39 Mon Sep 17 00:00:00 2001 From: carlocamilloni Date: Mon, 22 Jun 2020 23:07:58 +0200 Subject: [PATCH 4/6] MetaI: opt_sigma_max - save sigma_max to status file - read sigma_max from status file but it should also save the status --- src/isdb/Metainference.cpp | 47 ++++++++++++++++++++++++++++++++-- src/isdb/MetainferenceBase.cpp | 13 +++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/isdb/Metainference.cpp b/src/isdb/Metainference.cpp index 802d29d90e..2e163ce5a1 100644 --- a/src/isdb/Metainference.cpp +++ b/src/isdb/Metainference.cpp @@ -241,6 +241,11 @@ class Metainference : public bias::Bias // optimize sigma mean vector< vector < vector > > sigma_mean2_last_; unsigned optsigmamean_stride_; + // optimize sigma max + unsigned N_optimized_step_; + unsigned optimized_step_; + bool sigmamax_opt_done_; + std::vector sigma_max_est_; // average weights unsigned average_weights_stride_; @@ -312,6 +317,7 @@ void Metainference::registerKeywords(Keywords& keys) { keys.add("optional","DSIGMA","maximum MC move of the uncertainty parameter"); keys.add("compulsory","OPTSIGMAMEAN","NONE","Set to NONE/SEM to manually set sigma mean, or to estimate it on the fly"); keys.add("optional","SIGMA_MEAN0","starting value for the uncertainty in the mean estimate"); + keys.add("optional","SIGMA_MAX_STEPS", "Number of steps used to optimise SIGMA_MAX, before that the SIGMA_MAX value is used"); keys.add("optional","TEMP","the system temperature - this is only needed if code doesn't pass the temperature to plumed"); keys.add("optional","MC_STEPS","number of MC steps"); keys.add("optional","MC_CHUNKSIZE","MC chunksize"); @@ -361,6 +367,9 @@ Metainference::Metainference(const ActionOptions&ao): do_reweight_(false), do_optsigmamean_(0), optsigmamean_stride_(0), + N_optimized_step_(0), + optimized_step_(0), + sigmamax_opt_done_(false), average_weights_stride_(1) { bool noensemble = false; @@ -451,6 +460,13 @@ Metainference::Metainference(const ActionOptions&ao): parse("OPTSIGMAMEAN", stringa_optsigma); if(stringa_optsigma=="NONE") do_optsigmamean_=0; else if(stringa_optsigma=="SEM") do_optsigmamean_=1; + else if(stringa_optsigma=="SEM_MAX") do_optsigmamean_=2; + + unsigned aver_max_steps=0; + parse("SIGMA_MAX_STEPS", aver_max_steps); + if(aver_max_steps==0&&do_optsigmamean_==2) aver_max_steps=averaging*1000; + if(aver_max_steps>0&&do_optsigmamean_<2) error("SIGMA_MAX_STEPS can only be used together with OPTSIGMAMEAN=SEM_MAX"); + if(aver_max_steps>0&&do_optsigmamean_==2) N_optimized_step_=aver_max_steps; // resize vector for sigma_mean history sigma_mean2_last_.resize(nsel); @@ -458,7 +474,7 @@ Metainference::Metainference(const ActionOptions&ao): vector read_sigma_mean_; parseVector("SIGMA_MEAN0",read_sigma_mean_); - if(!do_optsigmamean_ && read_sigma_mean_.size()==0 && !getRestart()) + if(do_optsigmamean_==0 && read_sigma_mean_.size()==0 && !getRestart()) error("If you don't use OPTSIGMAMEAN and you are not RESTARTING then you MUST SET SIGMA_MEAN0"); if(noise_type_==MGAUSS||noise_type_==MOUTLIERS||noise_type_==GENERIC) { @@ -616,6 +632,8 @@ Metainference::Metainference(const ActionOptions&ao): } } + sigma_max_est_.resize(sigma_max_.size(), 0.); + IFile restart_sfile; restart_sfile.link(*this); if(getRestart()&&restart_sfile.FileExist(status_file_name_)) { @@ -666,6 +684,12 @@ Metainference::Metainference(const ActionOptions&ao): Tools::convert(i,msg); restart_sfile.scanField("sigma_"+msg,sigma_[i]); } + for(unsigned i=0; iset(sqrt(sigma_mean2_tmp[0])); } - // endif sigma optimization + // endif sigma mean optimization + // start sigma max optimization + if(do_optsigmamean_>1&&!sigmamax_opt_done_) { + for(unsigned i=0; i(optimized_step_); + sigmamax_opt_done_=true; + for(unsigned i=0; i read_sigma_mean_; parseVector("SIGMA_MEAN0",read_sigma_mean_); - if(!do_optsigmamean_ && read_sigma_mean_.size()==0 && !getRestart() && doscore_) + if(do_optsigmamean_==0 && read_sigma_mean_.size()==0 && !getRestart() && doscore_) error("If you don't use OPTSIGMAMEAN and you are not RESTARTING then you MUST SET SIGMA_MEAN0"); if(noise_type_==MGAUSS||noise_type_==MOUTLIERS||noise_type_==GENERIC) { @@ -480,6 +480,12 @@ void MetainferenceBase::Initialise(const unsigned input) Tools::convert(i,msg); restart_sfile.scanField("sigma_"+msg,sigma_[i]); } + for(unsigned i=0; i Date: Sat, 27 Jun 2020 14:20:55 +0200 Subject: [PATCH 5/6] MetaI: adapt Dsigma to sigma Max - from restart --- src/isdb/Metainference.cpp | 10 +++++++--- src/isdb/MetainferenceBase.cpp | 7 +++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/isdb/Metainference.cpp b/src/isdb/Metainference.cpp index 2e163ce5a1..412c6e708c 100644 --- a/src/isdb/Metainference.cpp +++ b/src/isdb/Metainference.cpp @@ -588,8 +588,8 @@ Metainference::Metainference(const ActionOptions&ao): Dsigma_.resize(read_dsigma.size()); Dsigma_=read_dsigma; } else { - Dsigma_.resize(sigma_max_.size()); - for(unsigned i=0; i bias(nrep_,0); if(master) { bias[replica_] = getArgument(narg); + //bias[replica_] = ((1.0/plumed.getAtoms().getKbT())- (1.0/kbt_) )*getArgument(narg); if(nrep_>1) multi_sim_comm.Sum(&bias[0], nrep_); } comm.Sum(&bias[0], nrep_); @@ -1521,9 +1525,9 @@ void Metainference::get_weights(const unsigned iselect, double &fact, double &va const double maxbias = *(std::max_element(bias.begin(), bias.end())); for(unsigned i=0; i (average_weights_stride_); if(!firstTimeW[iselect]) { diff --git a/src/isdb/MetainferenceBase.cpp b/src/isdb/MetainferenceBase.cpp index acd0c47b9d..4e7d09d161 100644 --- a/src/isdb/MetainferenceBase.cpp +++ b/src/isdb/MetainferenceBase.cpp @@ -315,8 +315,8 @@ MetainferenceBase::MetainferenceBase(const ActionOptions&ao): Dsigma_.resize(read_dsigma.size()); Dsigma_=read_dsigma; } else { - Dsigma_.resize(sigma_max_.size()); - for(unsigned i=0; i Date: Sat, 27 Jun 2020 14:24:58 +0200 Subject: [PATCH 6/6] MetaI: set new Dsigma after new sigma_max --- src/isdb/Metainference.cpp | 5 ++++- src/isdb/MetainferenceBase.cpp | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/isdb/Metainference.cpp b/src/isdb/Metainference.cpp index 412c6e708c..a3cde1d0c7 100644 --- a/src/isdb/Metainference.cpp +++ b/src/isdb/Metainference.cpp @@ -1631,7 +1631,10 @@ void Metainference::get_sigma_mean(const unsigned iselect, const double fact, co if(optimized_step_==N_optimized_step_) { double isteps = 1./static_cast(optimized_step_); sigmamax_opt_done_=true; - for(unsigned i=0; i(optimized_step_); sigmamax_opt_done_=true; - for(unsigned i=0; i