34 #ifdef HAVE_LMY_ENGINE 35 #include "formic/utils/matrix.h" 36 #include "formic/utils/random.h" 37 #include "formic/utils/lmyengine/var_dependencies.h" 52 const std::optional<EstimatorManagerInput>& global_emi,
60 std::move(qmcdriver_input),
64 std::move(population),
65 "QMCLinearOptimizeBatched::",
67 "QMCLinearOptimizeBatched"),
68 objFuncWrapper_(*this),
69 #ifdef HAVE_LMY_ENGINE
70 vdeps(1,
std::vector<double>()),
84 MinMethod(
"OneShiftOnly"),
85 do_output_matrices_csv_(false),
86 do_output_matrices_hdf_(false),
87 output_matrices_initialized_(false),
88 freeze_parameters_(false),
97 vmcdriver_input_(vmcdriver_input),
99 global_emi_(global_emi)
132 #ifdef HAVE_LMY_ENGINE 134 std::vector<double> shift_scales(3, 1.0);
135 EngineObj =
new cqmc::engine::LMYEngine<ValueType>(&vdeps,
175 #ifdef HAVE_LMY_ENGINE 182 for (
int i = 0; i <
optparam.size(); i++)
199 <<
"*****************************" << std::endl
200 <<
"Compute parameter derivatives" << std::endl
201 <<
"*****************************" << std::endl
209 app_log() <<
" Execution time (derivatives) = " << std::setprecision(4) << t_deriv.
elapsed() << std::endl;
213 #ifdef HAVE_LMY_ENGINE 214 void QMCFixedSampleLinearOptimizeBatched::engine_start(cqmc::engine::LMYEngine<ValueType>* EngineObj,
216 std::string MinMethod)
218 app_log() <<
"entering engine_start function" << std::endl;
220 std::unique_ptr<EngineHandle> handle;
224 handle = std::make_unique<LMYEngineHandle>(*EngineObj);
226 handle = std::make_unique<NullEngineHandle>();
235 app_log() <<
"<opt stage=\"setup\">" << std::endl;
236 app_log() <<
" <log>" << std::endl;
241 app_log() <<
" Reading configurations from h5FileRoot " << std::endl;
251 app_log() <<
" Execution time = " << std::setprecision(4) << t1.
elapsed() << std::endl;
252 app_log() <<
" </log>" << std::endl;
253 app_log() << R
"(<opt stage="main" walkers=")" << optTarget->getNumSamples() << "\">" << std::endl;
269 <<
"******************" << std::endl
270 <<
"Generating samples" << std::endl
271 <<
"******************" << std::endl
277 app_log() <<
" Execution time (sampling) = " << std::setprecision(4) << t_gen.
elapsed() << std::endl;
288 const int numParams =
optTarget->getNumParams();
289 const int N = numParams + 1;
297 app_log() <<
"Doing gradient test run" << std::endl;
300 #ifdef HAVE_LMY_ENGINE 303 app_log() <<
"Doing hybrid run" << std::endl;
338 int Total_iterations(0);
340 const int numParams =
optTarget->getNumParams();
341 const int N = numParams + 1;
343 std::vector<RealType> currentParameterDirections(
N, 0);
344 std::vector<RealType> currentParameters(numParams, 0);
345 std::vector<RealType> bestParameters(numParams, 0);
346 for (
int i = 0; i < numParams; i++)
349 optdir.resize(numParams, 0);
354 Total_iterations += 1;
361 for (
int i = 0; i < numParams; i++)
362 optTarget->Params(i) = currentParameters[i];
376 optTarget->fillOverlapHamiltonianMatrices(Right, Left);
378 bool apply_inverse(
true);
390 for (
int i = 0; i <
N; i++)
391 for (
int j = 0; j <
N; j++)
392 od_largest = std::max(std::max(od_largest,
std::abs(Left(i, j)) -
std::abs(Left(i, i))),
394 app_log() <<
"od_largest " << od_largest << std::endl;
403 app_log() <<
" stabilityBase " << stabilityBase << std::endl;
406 bool acceptedOneMove(
false);
407 for (
int stability = 0; stability <
nstabilizers; stability++)
411 for (
int i = 0; i <
N; i++)
412 for (
int j = 0; j <
N; j++)
413 Right(i, j) = Left(j, i);
415 for (
int i = 1; i <
N; i++)
417 app_log() <<
" Using XS:" << XS <<
" " << failedTries <<
" " << stability << std::endl;
425 for (
int i = 0; i < numParams; i++)
426 bigVec = std::max(bigVec,
std::abs(currentParameterDirections[i + 1]));
434 app_log() <<
" Failed Step. Magnitude of largest parameter change: " 444 for (
int i = 0; i < numParams; i++)
450 for (
int i = 0; i < numParams; i++)
452 for (
int i = 0; i < numParams; i++)
453 optdir[i] = currentParameterDirections[i + 1];
474 app_log() <<
" Failed Step. Largest LM parameter change:" << biggestParameterChange << std::endl;
482 for (
int i = 0; i < numParams; i++)
484 app_log() <<
" Good Step. Largest LM parameter change:" << biggestParameterChange << std::endl;
494 app_log() <<
" OldCost: " << lastCost <<
" NewCost: " << newCost <<
" Delta Cost:" << (newCost - lastCost)
504 app_log() <<
" Good Step, but cost function invalid" << std::endl;
511 if (newCost < lastCost && goodStep)
514 for (
int i = 0; i < numParams; i++)
517 acceptedOneMove =
true;
525 else if (stability > 0)
533 if (failedTries > 20)
540 app_log() <<
"Setting new Parameters" << std::endl;
541 for (
int i = 0; i < numParams; i++)
542 optTarget->Params(i) = bestParameters[i];
546 app_log() <<
"Reverting to old Parameters" << std::endl;
547 for (
int i = 0; i < numParams; i++)
548 optTarget->Params(i) = currentParameters[i];
554 return (
optTarget->getReportCounter() > 0);
564 std::string useGPU(
"yes");
565 std::string vmcMove(
"pbyp");
566 std::string ReportToH5(
"no");
567 std::string OutputMatrices(
"no");
568 std::string OutputMatricesHDF(
"no");
569 std::string FreezeParameters(
"no");
571 oAttrib.
add(useGPU,
"gpu");
572 oAttrib.
add(vmcMove,
"move");
573 oAttrib.
add(ReportToH5,
"hdf5");
575 m_param.
add(OutputMatrices,
"output_matrices_csv", {
"no",
"yes"});
576 m_param.
add(OutputMatricesHDF,
"output_matrices_hdf", {
"no",
"yes"});
577 m_param.
add(FreezeParameters,
"freeze_parameters", {
"no",
"yes"});
598 app_warning() <<
" The option 'freeze_parameters' is enabled. Variational parameters will not be updated. This " 599 "run will not perform variational parameter optimization!" 606 processChildren(q, [&](
const std::string& cname,
const xmlNodePtr element) {
607 if (cname ==
"optimize")
610 if (!att.empty() && att ==
"gradient_test")
613 test_grad_input.
readXML(element);
615 testEngineObj = std::make_unique<GradientTest>(std::move(test_grad_input));
621 std::stringstream error_msg;
622 app_log() <<
"Unknown or missing 'method' attribute in optimize tag: " << att <<
"\n";
642 processOptXML(q, vmcMove, ReportToH5 ==
"yes", useGPU ==
"yes");
647 const std::string& vmcMove,
655 throw std::runtime_error(
"Unknown MinMethod!\n");
665 APP_ABORT(
"options_LMY_.targetExcited = \"yes\" requires that MinMethod = \"adaptive or descent");
671 app_log() <<
"test version of OpenMP threading with AdaptiveThreeShift optimizer" << std::endl;
677 throw std::runtime_error(
678 "options_LMY_.max_param_change must be positive in QMCFixedSampleLinearOptimizeBatched::put");
682 throw std::runtime_error(
683 "options_LMY_.max_relative_cost_change must be positive in QMCFixedSampleLinearOptimizeBatched::put");
687 throw std::runtime_error(
"shift_i must be positive in QMCFixedSampleLinearOptimizeBatched::put");
689 throw std::runtime_error(
"shift_s must be positive in QMCFixedSampleLinearOptimizeBatched::put");
693 throw std::runtime_error(
694 "options_LMY_.cost_increase_tol must be non-negative in QMCFixedSampleLinearOptimizeBatched::put");
704 xmlNodePtr qsave = opt_xml;
705 xmlNodePtr cur = qsave->children;
709 std::string cname((
const char*)(cur->name));
710 if (cname ==
"mcwalkerset")
732 qmcdriver_input_copy.
readXML(opt_xml);
733 vmcdriver_input_copy.
readXML(opt_xml);
745 vmcEngine->setUpdateMode(vmcMove[0] ==
'p');
748 bool AppendRun =
false;
754 auto& qmcdriver_input =
vmcEngine->getQMCDriverInput();
785 throw std::runtime_error(
"number of shifts must be odd in QMCFixedSampleLinearOptimizeBatched::prepare_shifts");
792 if (i < central_index)
793 retval.at(i) = central_shift / (4.0 * (central_index - i));
794 else if (i > central_index)
795 retval.at(i) = central_shift * (4.0 * (i - central_index));
796 else if (i == central_index)
797 retval.at(i) = central_shift;
812 app_log() <<
" " << std::right << std::setw(12) <<
"shift_i";
813 app_log() <<
" " << std::right << std::setw(12) <<
"shift_s";
814 app_log() <<
" " << std::right << std::setw(20) <<
"max param change";
815 app_log() <<
" " << std::right << std::setw(20) <<
"cost function value";
817 app_log() <<
" " << std::right << std::setw(12) <<
"------------";
818 app_log() <<
" " << std::right << std::setw(12) <<
"------------";
819 app_log() <<
" " << std::right << std::setw(20) <<
"--------------------";
820 app_log() <<
" " << std::right << std::setw(20) <<
"--------------------";
848 app_log() <<
" " << std::scientific << std::right << std::setw(12) << std::setprecision(4) << si;
849 app_log() <<
" " << std::scientific << std::right << std::setw(12) << std::setprecision(4) << ss;
850 app_log() <<
" " << std::scientific << std::right << std::setw(20) << std::setprecision(4) << mc;
851 app_log() <<
" " << std::fixed << std::right << std::setw(20) << std::setprecision(12) << cv;
856 app_log() <<
" " << std::right << std::setw(12) <<
"N/A";
857 app_log() <<
" " << std::right << std::setw(12) <<
"N/A";
858 app_log() <<
" " << std::right << std::setw(20) <<
"N/A";
859 app_log() <<
" " << std::right << std::setw(20) <<
"N/A";
860 app_log() <<
" " << std::right << std::setw(12) <<
"bad update";
865 app_log() <<
" " << std::right << std::setw(12) <<
"N/A";
866 app_log() <<
" " << std::right << std::setw(12) <<
"N/A";
867 app_log() <<
" " << std::right << std::setw(20) <<
"N/A";
868 app_log() <<
" " << std::fixed << std::right << std::setw(20) << std::setprecision(12) << cv;
869 app_log() <<
" " << std::right << std::setw(12) <<
"initial";
886 const std::vector<RealType>& cv,
887 const std::vector<double>& sh,
898 for (
int i = 0; i < cv.size(); i++)
905 const bool other_is_valid =
913 const bool closer_to_target =
917 if (cost_is_much_lower || (closer_to_target && cost_is_similar))
918 retval = (retval &&
true);
926 retval = (retval && cv.at(ii) <= cv.at(i));
962 const std::vector<double>& shifts_i,
963 const std::vector<double>& shifts_s,
964 std::vector<std::vector<RealType>>& parameterDirections)
967 const int nshifts = shifts_i.size();
970 const int numParams =
optTarget->getNumParams();
973 const int N = numParams + 1;
976 parameterDirections.resize(nshifts);
977 for (
int i = 0; i < parameterDirections.size(); i++)
978 parameterDirections.at(i).assign(
N, 0.0);
993 optTarget->fillOverlapHamiltonianMatrices(hamMat, ovlMat);
1016 invMat.
copy(ovlMat);
1020 for (
int shift_index = 0; shift_index < nshifts; shift_index++)
1023 sftMat.
copy(hamMat);
1026 for (
int i = 1; i <
N; i++)
1027 sftMat(i, i) += shifts_i.at(shift_index);
1030 for (
int i = 1; i <
N; i++)
1031 for (
int j = 1; j <
N; j++)
1032 sftMat(i, j) += shifts_s.at(shift_index) * ovlMat(i, j);
1038 for (
int i = 0; i <
N; i++)
1039 for (
int j = i + 1; j <
N; j++)
1040 std::swap(prdMat(i, j), prdMat(j, i));
1049 for (
int i = 0; i < numParams; i++)
1050 parameterDirections.at(shift_index).at(i + 1) *=
objFuncWrapper_.Lambda;
1066 #ifdef HAVE_LMY_ENGINE 1082 const bool saved_grads_flag =
optTarget->getneedGrads();
1085 const int init_num_samp =
optTarget->getNumSamples();
1091 const int numParams =
optTarget->getNumParams();
1096 std::vector<double> shift_scales(shifts_i.size(), 1.0);
1097 for (
int i = 0; i < shift_scales.size(); i++)
1107 previous_update.push_back(formic::ColVec<double>(numParams));
1108 for (
int i = 0; i < numParams; i++)
1109 previous_update.at(count).at(i) = 2.0 * (formic::random_number<double>() - 0.5);
1113 if (!EngineObj->full_init())
1116 formic::VarDeps real_vdeps(numParams, std::vector<double>());
1118 EngineObj->get_param(&vdeps,
1132 formic::VarDeps tmp_vdeps(numParams, std::vector<double>());
1134 EngineObj->var_deps_ptr_update(&vdeps);
1138 EngineObj->shift_update(shift_scales);
1141 EngineObj->turn_on_update();
1152 app_log() <<
"Skipping initialization at first" << std::endl;
1174 EngineObj->selectParameters();
1176 for (
int i = 0; i < numParams; i++)
1177 if (EngineObj->getParameterSetting(i))
1180 formic::VarDeps real_vdeps(new_num, std::vector<double>());
1182 EngineObj->var_deps_ptr_update(&vdeps);
1185 if (EngineObj->use_blm())
1188 std::vector<formic::ColVec<double>> trimmed_old_updates(previous_update.size());
1191 if (EngineObj->getOnHybrid())
1194 std::vector<std::vector<ValueType>> hybridBLM_Input =
descentEngineObj->retrieveHybridBLM_Input();
1197 app_log() <<
"Blocked LM is part of hybrid run. Need to filter vectors from descent. " << std::endl;
1200 for (
int i = 0; i < hybridBLM_Input.size(); i++)
1202 std::vector<ValueType> full_vec = hybridBLM_Input[i];
1203 std::vector<ValueType> filtered_vec;
1205 formic::ColVec<double> reduced_vector(new_num, 0.0);
1208 for (
int j = 0; j < full_vec.size(); j++)
1209 if (EngineObj->getParameterSetting(j))
1211 filtered_vec.push_back(full_vec[j]);
1216 hybridBLM_Input[i] = filtered_vec;
1217 trimmed_old_updates[i] = reduced_vector;
1220 #if !defined(QMC_COMPLEX) 1221 EngineObj->setHybridBLM_Input(hybridBLM_Input);
1230 app_log() <<
"Regular Blocked LM run. Need to filter old update vectors. " << std::endl;
1232 for (
int i = 0; i < previous_update.size(); i++)
1234 formic::ColVec<double> full_vec = previous_update[i];
1236 formic::ColVec<double> reduced_vector(new_num, 0.0);
1239 for (
int j = 0; j < full_vec.size(); j++)
1240 if (EngineObj->getParameterSetting(j))
1242 reduced_vector[count] = full_vec[j];
1246 trimmed_old_updates[i] = reduced_vector;
1263 EngineObj->buildMatricesFromDerivatives();
1268 int N = numParams + 1;
1273 EngineObj->energy_target_compute();
1274 const RealType starting_cost = EngineObj->target_value();
1275 const RealType init_energy = EngineObj->energy_mean();
1279 <<
"*************************************************************************************************" 1281 <<
"Solving the linear method equations on the initial sample with initial energy" << std::setw(20)
1282 << std::setprecision(12) << init_energy << std::endl
1283 <<
"*************************************************************************************************" 1288 EngineObj->wfn_update_prep();
1296 int numOptParams =
optTarget->getNumParams();
1309 EngineObj->clear_histories();
1317 EngineObj->buildMatricesFromDerivatives();
1322 app_log() <<
"Should be building matrices from stored samples" << std::endl;
1323 EngineObj->buildMatricesFromDerivatives();
1331 EngineObj->clear_histories();
1336 <<
"*********************************************************" << std::endl
1337 <<
"Solving the linear method equations on the initial sample" << std::endl
1338 <<
"*********************************************************" << std::endl
1342 std::vector<std::vector<RealType>> parameterDirections;
1343 #ifdef HAVE_LMY_ENGINE 1345 EngineObj->wfn_update_compute();
1351 parameterDirections.resize(shifts_i.size());
1352 for (
int i = 0; i < shifts_i.size(); i++)
1354 parameterDirections.at(i).assign(
N, 0.0);
1357 for (
int j = 0; j <
N; j++)
1358 parameterDirections.at(i).at(j) =
std::real(EngineObj->wfn_update().at(i *
N + j));
1361 parameterDirections.at(i).at(0) = 1.0;
1368 std::vector<std::vector<RealType>> tmpParameterDirections;
1369 tmpParameterDirections.resize(shifts_i.size());
1371 for (
int i = 0; i < shifts_i.size(); i++)
1373 tmpParameterDirections.at(i).assign(numParams + 1, 0.0);
1374 int lm_update_idx = 0;
1375 for (
int j = 0; j < numParams + 1; j++)
1379 tmpParameterDirections.at(i).at(j) = parameterDirections.at(i).at(j);
1382 else if (EngineObj->getParameterSetting(j - 1) ==
true)
1384 tmpParameterDirections.at(i).at(j) = parameterDirections.at(i).at(lm_update_idx);
1388 parameterDirections.at(i) = tmpParameterDirections.at(i);
1398 std::vector<RealType> currParams(numParams, 0.0);
1401 for (
int i = 0; i < numParams; i++)
1402 currParams.at(i) =
optTarget->Params(i);
1405 std::vector<bool> good_update(parameterDirections.size(),
true);
1408 std::vector<RealType> max_change(parameterDirections.size(), 0.0);
1409 for (
int k = 0; k < parameterDirections.size(); k++)
1411 for (
int i = 0; i < numParams; i++)
1413 std::max(max_change.at(k),
std::abs(parameterDirections.at(k).at(i + 1) / parameterDirections.at(k).at(0)));
1418 for (
int i = 0; i < numParams; i++)
1419 optTarget->Params(i) = currParams.at(i) + parameterDirections.at(central_index).at(i + 1);
1423 <<
"************************************************************" << std::endl
1424 <<
"Updating the guiding function with the middle shift's update" << std::endl
1425 <<
"************************************************************" << std::endl
1432 <<
"*************************************************************" << std::endl
1433 <<
"Generating a new sample based on the updated guiding function" << std::endl
1434 <<
"*************************************************************" << std::endl
1449 <<
"******************************************************************" << std::endl
1450 <<
"Comparing different shifts' cost function values on updated sample" << std::endl
1451 <<
"******************************************************************" << std::endl
1455 for (
int i = 0; i < numParams; i++)
1456 currParams.at(i) =
optTarget->Params(i);
1459 for (
int i = 0; i < numParams; i++)
1460 optTarget->Params(i) = currParams.at(i) - parameterDirections.at(central_index).at(i + 1);
1465 for (
int i = 0; i < numParams; i++)
1467 for (
int j = 0; j < parameterDirections.size(); j++)
1469 if (j != central_index)
1470 parameterDirections.at(j).at(i + 1) -= parameterDirections.at(central_index).at(i + 1);
1478 for (
int k = 0; k < parameterDirections.size(); k++)
1480 for (
int i = 0; i < numParams; i++)
1481 optTarget->Params(i) = currParams.at(i) + (k == central_index ? 0.0 : parameterDirections.at(k).at(i + 1));
1483 costValues.at(k) =
optTarget->LMYEngineCost(
false, EngineObj);
1484 good_update.at(k) = (good_update.at(k) &&
1486 if (!good_update.at(k))
1487 costValues.at(k) =
std::abs(1.5 * initCost) + 1.0;
1491 const std::vector<RealType>* bestDirection = 0;
1492 int best_shift = -1;
1495 if (
is_best_cost(k, costValues, shifts_i, initCost) && good_update.at(k))
1498 bestDirection = ¶meterDirections.at(k);
1505 for (
int k = 0; k < good_update.size(); k++)
1506 print_cost_summary(shifts_i.at(k), shifts_s.at(k), max_change.at(k), costValues.at(k), k, best_shift,
1514 for (
int i = 0; i < numParams; i++)
1515 optTarget->Params(i) = currParams.at(i) + (best_shift == central_index ? 0.0 : bestDirection->at(i + 1));
1517 <<
"*****************************************************************************" << std::endl
1518 <<
"Applying the update for shift_i = " << std::scientific << std::right << std::setw(12)
1519 << std::setprecision(4) <<
bestShift_i <<
" and shift_s = " << std::scientific << std::right
1520 << std::setw(12) << std::setprecision(4) <<
bestShift_s << std::endl
1521 <<
"*****************************************************************************" << std::endl
1530 for (
int i = 0; i < numParams; i++)
1531 optTarget->Params(i) = currParams.at(i) - parameterDirections.at(central_index).at(i + 1);
1533 <<
"***********************************************************" << std::endl
1534 <<
"Reverting to old parameters and increasing shift magnitudes" << std::endl
1535 <<
"***********************************************************" << std::endl
1543 formic::ColVec<RealType> update_dirs(numParams, 0.0);
1544 for (
int i = 0; i < numParams; i++)
1546 update_dirs.at(i) =
std::real(bestDirection->at(i + 1) + parameterDirections.at(central_index).at(i + 1));
1547 previous_update.insert(previous_update.begin(), update_dirs);
1551 previous_update.pop_back();
1555 optTarget->setneedGrads(saved_grads_flag);
1561 optTarget->setNumSamples(init_num_samp);
1565 return (
optTarget->getReportCounter() > 0);
1578 const int numParams =
optTarget->getNumParams();
1581 const int N = numParams + 1;
1584 std::vector<RealType> currentParameters(numParams, 0.0);
1587 for (
int i = 0; i < numParams; i++)
1591 std::vector<RealType> parameterDirections;
1592 parameterDirections.assign(
N, 0.0);
1612 Timer t_build_matrices;
1615 <<
"*****************************************" << std::endl
1616 <<
"Building overlap and Hamiltonian matrices" << std::endl
1617 <<
"*****************************************" << std::endl;
1620 optTarget->fillOverlapHamiltonianMatrices(hamMat, ovlMat);
1630 std::string newh5 =
get_root_name() +
".linear_matrices.h5";
1631 hout.
create(newh5, H5F_ACC_TRUNC);
1632 hout.
write(ovlMat,
"overlap");
1633 hout.
write(hamMat,
"Hamiltonian");
1637 app_log() <<
" Execution time (building matrices) = " << std::setprecision(4) << t_build_matrices.
elapsed()
1647 <<
"**************************" << std::endl
1648 <<
"Solving eigenvalue problem" << std::endl
1649 <<
"**************************" << std::endl;
1651 invMat.
copy(ovlMat);
1653 for (
int i = 1; i <
N; i++)
1656 if (invMat(i, i) == 0)
1661 for (
int i = 1; i <
N; i++)
1662 for (
int j = 1; j <
N; j++)
1669 app_log() <<
" Using generalized eigenvalue solver (ggev)" << std::endl;
1674 app_log() <<
" Using inverse + regular eigenvalue solver (geev)" << std::endl;
1679 app_log() <<
"ARPACK not compiled into this QMCPACK executable" << std::endl;
1680 throw std::runtime_error(
"ARPACK not present (QMC_USE_ARPACK not set)");
1684 throw std::runtime_error(
"Unknown eigenvalue solver: " +
eigensolver_);
1687 app_log() <<
" Execution time (eigenvalue) = " << std::setprecision(4) << t_eigen.
elapsed() << std::endl;
1694 hout.
write(lowestEV,
"lowest_eigenvalue");
1695 hout.
write(parameterDirections,
"scaled_eigenvector");
1701 for (
int i = 0; i < numParams; i++)
1712 for (
int i = 0; i < numParams; i++)
1713 optTarget->Params(i) = currentParameters.at(i) + parameterDirections.at(i + 1);
1717 int max_element = 0;
1718 for (
int i = 0; i < numParams; i++)
1719 if (
std::abs(parameterDirections.at(i + 1)) > largestChange)
1721 largestChange =
std::abs(parameterDirections.at(i + 1));
1725 <<
"Among totally " << numParams <<
" optimized parameters, " 1726 <<
"largest LM parameter change : " << largestChange <<
" at parameter " << max_element << std::endl;
1733 <<
"******************************************************************************" << std::endl
1734 <<
"Init Cost = " << std::scientific << std::right << std::setw(12) << std::setprecision(4) << initCost
1735 <<
" New Cost = " << std::scientific << std::right << std::setw(12) << std::setprecision(4) << newCost
1736 <<
" Delta Cost = " << std::scientific << std::right << std::setw(12) << std::setprecision(4)
1737 << newCost - initCost << std::endl
1738 <<
"******************************************************************************" << std::endl;
1742 app_log() << std::endl <<
"The new set of parameters is not valid. Revert to the old set!" << std::endl;
1743 for (
int i = 0; i < numParams; i++)
1744 optTarget->Params(i) = currentParameters.at(i);
1758 app_log() << std::endl <<
"The new set of parameters is valid. Updating the trial wave function!" << std::endl;
1764 <<
"*****************************************************************************" << std::endl
1765 <<
"Applying the update for shift_i = " << std::scientific << std::right << std::setw(12)
1766 << std::setprecision(4) <<
bestShift_i <<
" and shift_s = " << std::scientific << std::right
1767 << std::setw(12) << std::setprecision(4) <<
bestShift_s << std::endl
1768 <<
"*****************************************************************************" << std::endl;
1774 return (
optTarget->getReportCounter() > 0);
1777 #ifdef HAVE_LMY_ENGINE 1786 if (descent_num == 0)
1797 for (
int i = 0; i < results.size(); i++)
1815 return (
optTarget->getReportCounter() > 0);
1821 #ifdef HAVE_LMY_ENGINE 1822 bool QMCFixedSampleLinearOptimizeBatched::hybrid_run()
1829 EngineObj->setOnHybrid(
true);
1838 std::vector<std::vector<ValueType>> hybridBLM_Input =
descentEngineObj->retrieveHybridBLM_Input();
1839 #if !defined(QMC_COMPLEX) 1841 EngineObj->setHybridBLM_Input(hybridBLM_Input);
1850 app_log() <<
"Finished a hybrid step" << std::endl;
1851 return (
optTarget->getReportCounter() > 0);
OptimizerType previous_optimizer_type
type of the previous optimization method, updated by processOptXML before run
MatrixA::value_type invert_matrix(MatrixA &M, bool getdet=true)
invert a matrix
int num_shifts
number of shifts we will try
int nkept
number of directions kept
RealType bestShift_i
the previous best identity shift
int nolds
number of old updates kept
RealType costFunc(RealType dl)
OutputMatrix output_hamiltonian_
void start()
common operation to start optimization
const ParticleSet & get_golden_electrons() const
std::ostream & app_warning()
Abstraction of information on executor environments.
void write(T &data, const std::string &aname)
write the data to the group aname and check status runtime error is issued on I/O error ...
MCPopulation population_
the entire (on node) walker population it serves VMCBatch and DMCBatch right now but will be polymorp...
xmlNodePtr wfNode
xml node to be dumped
QMCHamiltonian & get_golden_hamiltonian()
helper functions for EinsplineSetBuilder
int rank() const
return the rank
const std::map< std::string, OptimizerType > OptimizerNames
size_t getActiveWalkers() const
return the number of active walkers
Input representation for VMC driver class runtime parameters.
OutputMatrix output_overlap_
void resetSampleCount()
Set the sample count to zero but preserve the storage.
std::string h5_file_root_
bool adaptive_three_shift_run()
This is a data structure strictly for QMCDriver and its derived classes.
NewTimer & cost_function_timer_
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
std::vector< double > prepare_shifts(const double central_shift) const
returns a vector of three shift values centered around the provided shift.
OptimizerType current_optimizer_type
type of the current optimization method, updated by processOptXML before run
QMCDriverNew Base class for Unified Drivers.
bool block_lm
whether we are doing block algorithm
LMYOptions options_LMY_
LMY engine related options.
bool put(xmlNodePtr cur)
assign attributes to the set
A set of light weight walkers that are carried between driver sections and restart.
void print_cost_summary_header()
prints a header for the summary of each shift's result
void close()
close all the open groups and file
bool filter_info
whether to filter parameters for the lm
RealType shift_s_base
Shift_s adjustment base.
NewTimer & initialize_timer_
bool doHybrid
whether to use hybrid method
std::unique_ptr< GradientTest > testEngineObj
std::vector< IndexType > walkers_per_crowd
Communicate * Controller
Global Communicator for a process.
int size() const
return the number of tasks
void copy(const This_t &rhs)
NewTimer & line_min_timer_
std::vector< RealType > optdir
bool put(std::istream &is) override
read from std::istream
std::unique_ptr< QMCCostFunctionBase > optTarget
target cost function to optimize
bool ValidCostFunction(bool valid)
std::bitset< QMC_MODE_MAX > qmc_driver_mode_
bits to classify QMCDriver
static Real getLowestEigenvector_Inv(Matrix< Real > &A, Matrix< Real > &B, std::vector< Real > &ev)
Solve the generalized eigenvalue problem and return a scaled eigenvector corresponding to the selecte...
RealType bestShift_s
the previous best overlap shift
Wrapping information on parallelism.
NewTimer & eigenvalue_timer_
RealType shift_i_input
current shift_i, shift_s input values
NewTimer & generate_samples_timer_
bool one_shift_run()
Performs one iteration of the linear method using an adaptive scheme that tries three different shift...
IndexType get_num_crowds() const
RealType max_param_change
max amount a parameter may change relative to current wave function weight
static Real getLowestEigenvector_Gen(Matrix< Real > &A, Matrix< Real > &B, std::vector< Real > &ev)
Solve the generalized eigenvalue problem and return a scaled eigenvector corresponding to the selecte...
IndexType get_total_walkers() const
~QMCFixedSampleLinearOptimizeBatched() override
Destructor.
const std::string & get_root_name() const override
std::string eigensolver_
Choice of eigenvalue solver.
void readXML(xmlNodePtr xml_input)
std::unique_ptr< HybridEngine > hybridEngineObj
omp_int_t omp_get_max_threads()
bool is_best_cost(const int ii, const std::vector< RealType > &cv, const std::vector< double > &sh, const RealType ic) const
Returns whether the proposed new cost is the best compared to the others.
QMCFixedSampleLinearOptimizeBatched(const ProjectData &project_data, QMCDriverInput &&qmcdriver_input, const std::optional< EstimatorManagerInput > &global_emi, VMCDriverInput &&vmcdriver_input, WalkerConfigurations &wc, MCPopulation &&population, SampleStack &samples, Communicate *comm)
Constructor.
IndexType get_walkers_per_rank() const
WalkerConfigurations & walker_configs_ref_
void product(const Matrix< T > &A, const Matrix< T > &B, Matrix< T > &C)
static function to perform C=AB for real matrices
Compilation units that construct QMCDriverInput need visibility to the actual input classes types in ...
RealType omega_shift
the shift to use when targeting an excited state
Real getLowestEigenvector(Matrix< Real > &A, std::vector< Real > &ev) const
Communicate * myComm
pointer to Communicate
class to handle a set of attributes of an xmlNode
This a subclass for runtime errors that will occur on all ranks.
void print_cost_summary(const double si, const double ss, const RealType mc, const RealType cv, const int ind, const int bi, const bool gu)
prints a summary of the computed cost for the given shift
Definition of QMCDriver which performs VMC and optimization.
RealType cost_increase_tol
the tolerance to cost function increases when choosing the best shift in the adaptive shift method ...
Tests for variational parameter derivatives.
NewTimer & createGlobalTimer(const std::string &myname, timer_levels mylevel)
int nsamp_comp
number of samples to do in correlated sampling part
NRCOptimizationFunctionWrapper< QMCFixedSampleLinearOptimizeBatched > objFuncWrapper_
RealType target_shift_i
the shift_i value that the adaptive shift method should aim for
std::string MinMethod
name of the current optimization method, updated by processOptXML before run
static QMCDriverNew::AdjustedWalkerCounts adjustGlobalWalkerCount(Communicate &comm, const IndexType current_configs, const IndexType requested_total_walkers, const IndexType requested_walkers_per_rank, const RealType reserve_walkers, int num_crowds)
}@
#define APP_ABORT(msg)
Widely used but deprecated fatal error macros from legacy code.
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
bool previous_linear_methods_run()
std::vector< RealType > optparam
std::string getXMLAttributeValue(const xmlNodePtr cur, const std::string_view name)
get the value string for attribute name if name is unfound in cur you get an empty string back this i...
void solveShiftsWithoutLMYEngine(const std::vector< double > &shifts_i, const std::vector< double > &shiffts_s, std::vector< std::vector< RealType >> ¶meterDirections)
For each set of shifts, solves the linear method eigenproblem by building and diagonalizing the matri...
const TrialWaveFunction & get_golden_twf() const
void output(Matrix< RealType > &mat)
Print matrix to text-formatted scalar.dat file.
void add(PDT &aparam, const std::string &aname_in, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new parameter corresponding to an xmlNode <parameter>
MakeReturn< UnaryNode< FnSqrt, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t sqrt(const Vector< T1, C1 > &l)
bool targetExcited
whether we are targeting an excited state
QMCTraits::RealType RealType
void readXML(xmlNodePtr xml_input)
bool create(const std::filesystem::path &fname, unsigned flags=H5F_ACC_TRUNC)
create a file
void processChildren(const xmlNodePtr cur, const F &functor)
process through all the children of an XML element F is a lambda or functor void F/[](const std::stri...
void readXML(xmlNodePtr cur)
Reads qmc section xml node parameters.
std::unique_ptr< DescentEngine > descentEngineObj
NewTimer & build_olv_ham_timer_
bool processOptXML(xmlNodePtr cur, const std::string &vmcMove, bool reportH5, bool useGPU)
process xml node value (parameters for both VMC and OPT) for the actual optimization ...
bool store_samples
whether to store samples for the lm
std::vector< xmlNodePtr > mcwalkerNodePtr
a list of mcwalkerset element
RealType max_relative_cost_change
the maximum relative change in the cost function for the adaptive three-shift scheme ...
bool is_manager() const
return true if the rank == 0
int nblocks
number of blocks used in block algorithm
const ProjectData & project_data_
project info for accessing global fileroot and series id
QMCTraits::RealType RealType
void finish()
common operation to finish optimization, used by the derived classes
bool filter_param
whether to filter parameters for the lm
int nstabilizers
Number of iterations maximum before generating new configurations.
std::bitset< 2 > accept_history
accept history, remember the last 2 iterations, value 00, 01, 10, 11
int Max_iterations
Number of iterations maximum before generating new configurations.
bool run() override
Run the Optimization algorithm.
double ratio_threshold
threshold for filtering parameters for the lm
void barrier_and_abort(const std::string &msg) const
QMCDriverInput qmcdriver_input_
const std::optional< EstimatorManagerInput > global_emi_
This is retained in order to construct and reconstruct the vmcEngine.
bool do_output_matrices_hdf_
void init_file(const std::string &root_name, const std::string &name, int N)
Open a text-formatted scalar.dat file and print the header line.
void add(PDT &aparam, const std::string &aname, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new attribute
bool output_matrices_initialized_
void process(xmlNodePtr cur) override
preprocess xml node
Real getNonLinearRescale(std::vector< Real > &dP, Matrix< Real > &S, const QMCCostFunctionBase &optTarget) const
bool do_output_matrices_csv_
Input representation for Driver base class runtime parameters.
std::unique_ptr< VMCBatched > vmcEngine
vmc engine
VMCDriverInput vmcdriver_input_
bool isnan(float a)
return true if the value is NaN.