Skip to content

Commit

Permalink
extend n iter reinflate to be a vector reinflation sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
jtwhite79 committed Dec 12, 2024
1 parent 16a0cdd commit b0aaafb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
39 changes: 30 additions & 9 deletions src/libs/pestpp_common/EnsembleMethodUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2492,7 +2492,18 @@ double L2PhiHandler::calc_std(map<string, double> *phi_map)

double L2PhiHandler::get_representative_phi(phiType pt)
{
if (pest_scenario->get_pestpp_options().get_ies_n_iter_mean() < 0) {
//if (pest_scenario->get_pestpp_options().get_ies_n_iter_mean() < 0)
bool use_min = false;
for (auto& fac : pest_scenario->get_pestpp_options().get_ies_n_iter_mean())
{
if (fac < 0)
{
use_min = true;
break;
}
}
if (use_min)
{
return get_min(pt);
}

Expand Down Expand Up @@ -4286,7 +4297,7 @@ void EnsembleMethod::throw_em_error(string message)

}

bool EnsembleMethod::should_terminate()
bool EnsembleMethod::should_terminate(int current_n_iter_mean)
{
//todo: use ies accept fac here?
double phiredstp = pest_scenario.get_control_info().phiredstp;
Expand All @@ -4306,10 +4317,10 @@ bool EnsembleMethod::should_terminate()
message(1, "phiredstp: ", phiredstp);
message(1, "nphistp: ", nphistp);
message(1, "nphinored (also used for consecutive bad lambda cycles): ", nphinored);
int n_mean_iter = pest_scenario.get_pestpp_options().get_ies_n_iter_mean();
//int n_mean_iter = pest_scenario.get_pestpp_options().get_ies_n_iter_mean();
vector<double>::iterator begin_idx = best_mean_phis.begin();
if ((n_mean_iter > 0) && (best_mean_phis.size() > n_mean_iter))
begin_idx = best_mean_phis.end() - (n_mean_iter+1); //bc of prior phi and then adding the mean shift to the list
if ((current_n_iter_mean > 0) && (best_mean_phis.size() > current_n_iter_mean))
begin_idx = best_mean_phis.end() - (current_n_iter_mean+1); //bc of prior phi and then adding the mean shift to the list
if (best_mean_phis.size() > 0)
{

Expand All @@ -4330,7 +4341,7 @@ bool EnsembleMethod::should_terminate()
for (auto& phi : best_mean_phis)
{
ratio = (phi - best_phi_yet) / phi;
if ((i>=(iter - n_mean_iter)) && (ratio <= phiredstp))
if ((i>=(iter - current_n_iter_mean)) && (ratio <= phiredstp))
count++;
i++;
}
Expand Down Expand Up @@ -4897,11 +4908,21 @@ void EnsembleMethod::initialize(int cycle, bool run, bool use_existing)

consec_bad_lambda_cycles = 0;
reinflate_to_minphi_real = false;
if (pest_scenario.get_pestpp_options().get_ies_n_iter_mean() < 0)
bool use_min = false;
for (auto& fac : pest_scenario.get_pestpp_options().get_ies_n_iter_mean())
{
if (fac < 0)
{
use_min = true;
fac *= -1;
}
}

if (use_min)
{
message(2,"n_iter_mean < 0, using min-phi real for re-inflation, resetting n_iter_reinflate to positive");
message(2,"n_iter_reinflate < 0, using min-phi real for re-inflation, resetting n_iter_reinflate to positive");
reinflate_to_minphi_real = true;
pest_scenario.get_pestpp_options_ptr()->set_ies_n_iter_mean(-1 * pest_scenario.get_pestpp_options().get_ies_n_iter_mean());
//pest_scenario.get_pestpp_options_ptr()->set_ies_n_iter_mean(-1 * pest_scenario.get_pestpp_options().get_ies_n_iter_mean());
}
lam_mults = pest_scenario.get_pestpp_options().get_ies_lam_mults();
if (lam_mults.size() == 0)
Expand Down
2 changes: 1 addition & 1 deletion src/libs/pestpp_common/EnsembleMethodUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class EnsembleMethod
RunManagerAbstract* _run_mgr_ptr, string _alg_tag="EnsembleMethod");

virtual void throw_em_error(string message);
bool should_terminate();
bool should_terminate(int current_n_iter_mean=0);
void sanity_checks();
//template<typename T, typename A>
//void message(int level, const string& _message, vector<T, A> _extras, bool echo = true);
Expand Down
20 changes: 16 additions & 4 deletions src/libs/pestpp_common/EnsembleSmoother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ void IterEnsembleSmoother::iterate_2_solution()
ofstream &frec = file_manager.rec_ofstream();

bool accept;
int n_iter_mean = pest_scenario.get_pestpp_options().get_ies_n_iter_mean();
vector<int> n_iter_mean = pest_scenario.get_pestpp_options().get_ies_n_iter_mean();

int iters_since_reinflate = 0;
int n_iter_mean_idx = 0;
int current_n_iter_mean = n_iter_mean[n_iter_mean_idx];
int solution_iter = 0;
for (int i = 0; i < pest_scenario.get_control_info().noptmax; i++)
{
Expand Down Expand Up @@ -72,15 +76,23 @@ void IterEnsembleSmoother::iterate_2_solution()
else
consec_bad_lambda_cycles++;

if ((n_iter_mean> 0) && (solution_iter % n_iter_mean == 0))
//if ((n_iter_mean > 0) && (solution_iter % n_iter_mean == 0))
if ((current_n_iter_mean > 0) && (iters_since_reinflate >= current_n_iter_mean))
{
iter++;
reset_par_ensemble_to_prior_mean();
iters_since_reinflate = 0;
if (n_iter_mean.size() > n_iter_mean_idx)
{
n_iter_mean_idx++;
current_n_iter_mean = n_iter_mean[n_iter_mean_idx];
}
}

if (should_terminate())
if (should_terminate(current_n_iter_mean))
{
if (iter > pest_scenario.get_pestpp_options().get_ies_n_iter_mean()) {
//if (iter > pest_scenario.get_pestpp_options().get_ies_n_iter_mean()) {
if (iter > current_n_iter_mean) {
break;
}
else{
Expand Down
15 changes: 12 additions & 3 deletions src/libs/pestpp_common/pest_data_structs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,13 @@ bool PestppOptions::assign_ies_value_by_key(const string& key, const string& val
{
passed_args.insert("IES_N_ITER_MEAN");
passed_args.insert("IES_N_ITER_REINFLATE");
convert_ip(value,ies_n_iter_mean);
ies_n_iter_mean.clear();
vector<string> tok;
tokenize(value, tok, ",");
for (const auto& fac : tok)
{
ies_n_iter_mean.push_back(convert_cp<int>(fac));
}
return true;
}
else if (key == "IES_UPDATE_BY_REALS")
Expand Down Expand Up @@ -1837,7 +1843,10 @@ void PestppOptions::summary(ostream& os) const
os << "ies_localizer_forgive_extra: " << ies_localizer_forgive_missing << endl;
os << "ies_phi_factors_file: " << ies_phi_fractions_file << endl;
os << "ies_phi_factors_by_real: " << ies_phi_factors_by_real << endl;
os << "ies_n_iter_reinflate: " << ies_n_iter_mean << endl;
os << "ies_n_iter_reinflate: " << endl;
for (auto v : ies_n_iter_mean)
os << v << ",";
os << endl;
os << "ies_updatebyreals: " << ies_updatebyreals << endl;


Expand Down Expand Up @@ -2024,7 +2033,7 @@ void PestppOptions::set_defaults()
set_ies_localizer_forgive_missing(false);
set_ies_phi_fractions_files("");
set_ies_phi_factors_by_real(false);
set_ies_n_iter_mean(0);
set_ies_n_iter_mean(vector<int>{0});
set_ies_updatebyreals(false);
set_save_dense(false);

Expand Down
6 changes: 3 additions & 3 deletions src/libs/pestpp_common/pest_data_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,8 @@ class PestppOptions {
void set_ies_multimodal_alpha(double _flag) { ies_multimodal_alpha = _flag; }
void set_ensemble_output_precision(int prec) { ensemble_output_precision = prec;}
int get_ensemble_output_precision() const {return ensemble_output_precision;}
void set_ies_n_iter_mean(int _n_iter_mean) {ies_n_iter_mean = _n_iter_mean;}
int get_ies_n_iter_mean() const {return ies_n_iter_mean;}
void set_ies_n_iter_mean(vector<int> _n_iter_mean) {ies_n_iter_mean = _n_iter_mean;}
vector<int> get_ies_n_iter_mean() const {return ies_n_iter_mean;}


string get_gsa_method() const { return gsa_method; }
Expand Down Expand Up @@ -828,7 +828,7 @@ class PestppOptions {
bool ies_localizer_forgive_missing;
string ies_phi_fractions_file;
bool ies_phi_factors_by_real;
int ies_n_iter_mean;
vector<int> ies_n_iter_mean;
bool ies_updatebyreals;


Expand Down

0 comments on commit b0aaafb

Please sign in to comment.