From ad32b69cb982122fe4dc0555fd8eb916c0f61ca2 Mon Sep 17 00:00:00 2001 From: Mark Dewing Date: Sat, 31 Jul 2021 14:01:36 -0500 Subject: [PATCH 1/6] Add linear matrix HDF output Add parameter "output_matrices_hdf" to output the linear method matrices in HDF format. Also add information (shifts, etc) and outputs (eigenvalue, eigenvector) to reproduce the generalized eigensolver's output and check alternate implementations against QMCPACK's implementation. The file is named "linear_matrices.h5". Currently there is no support for using the root project name or the series number. --- .../QMCFixedSampleLinearOptimizeBatched.cpp | 31 ++++++++++++++++--- .../QMCFixedSampleLinearOptimizeBatched.h | 3 ++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp index 58145b33f9..e999d29a60 100644 --- a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp +++ b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp @@ -100,6 +100,7 @@ QMCFixedSampleLinearOptimizeBatched::QMCFixedSampleLinearOptimizeBatched(const P previous_optimizer_type_(OptimizerType::NONE), current_optimizer_type_(OptimizerType::NONE), do_output_matrices_(false), + do_output_matrices_hdf_(false), output_matrices_initialized_(false), freeze_parameters_(false) @@ -324,7 +325,7 @@ bool QMCFixedSampleLinearOptimizeBatched::previous_linear_methods_run() app_log() << " Using XS:" << XS << " " << failedTries << " " << stability << std::endl; eigenvalue_timer_.start(); getLowestEigenvector(Right, currentParameterDirections); - Lambda = getNonLinearRescale(currentParameterDirections, S); + Lambda = getNonLinearRescale(currentParameterDirections, S); eigenvalue_timer_.stop(); // biggest gradient in the parameter direction vector RealType bigVec(0); @@ -471,6 +472,7 @@ void QMCFixedSampleLinearOptimizeBatched::process(xmlNodePtr q) std::string vmcMove("pbyp"); std::string ReportToH5("no"); std::string OutputMatrices("no"); + std::string OutputMatricesHDF("no"); std::string FreezeParameters("no"); OhmmsAttributeSet oAttrib; oAttrib.add(useGPU, "gpu"); @@ -478,13 +480,15 @@ void QMCFixedSampleLinearOptimizeBatched::process(xmlNodePtr q) oAttrib.add(ReportToH5, "hdf5"); m_param.add(OutputMatrices, "output_matrices"); + m_param.add(OutputMatricesHDF, "output_matrices_hdf"); m_param.add(FreezeParameters, "freeze_parameters"); oAttrib.put(q); m_param.put(q); - do_output_matrices_ = (OutputMatrices != "no"); - freeze_parameters_ = (FreezeParameters != "no"); + do_output_matrices_ = (OutputMatrices != "no"); + do_output_matrices_hdf_ = (OutputMatricesHDF != "no"); + freeze_parameters_ = (FreezeParameters != "no"); // Use freeze_parameters with output_matrices to generate multiple lines in the output with // the same parameters so statistics can be computed in post-processing. @@ -1288,6 +1292,17 @@ bool QMCFixedSampleLinearOptimizeBatched::one_shift_run() output_hamiltonian_.output(hamMat); } + hdf_archive hout; + if (do_output_matrices_hdf_) + { + std::string newh5 = "linear_matrices.h5"; + hout.create(newh5, H5F_ACC_TRUNC); + hout.write(ovlMat, "overlap"); + hout.write(hamMat, "Hamiltonian"); + hout.write(bestShift_i, "bestShift_i"); + hout.write(bestShift_s, "bestShift_s"); + } + // apply the identity shift for (int i = 1; i < N; i++) { @@ -1313,11 +1328,19 @@ bool QMCFixedSampleLinearOptimizeBatched::one_shift_run() std::swap(prdMat(i, j), prdMat(j, i)); // compute the lowest eigenvalue of the product matrix and the corresponding eigenvector - getLowestEigenvector(prdMat, parameterDirections); + RealType lowestEV = getLowestEigenvector(prdMat, parameterDirections); // compute the scaling constant to apply to the update Lambda = getNonLinearRescale(parameterDirections, ovlMat); + if (do_output_matrices_hdf_) + { + hout.write(lowestEV, "lowest_eigenvalue"); + hout.write(parameterDirections, "scaled_eigenvector"); + hout.write(Lambda, "non_linear_rescale"); + hout.close(); + } + // scale the update by the scaling constant for (int i = 0; i < numParams; i++) parameterDirections.at(i + 1) *= Lambda; diff --git a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.h b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.h index 356dd032cc..0813d3a4ac 100644 --- a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.h +++ b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.h @@ -207,6 +207,9 @@ class QMCFixedSampleLinearOptimizeBatched : public QMCLinearOptimizeBatched, // Output Hamiltonian and overlap matrices bool do_output_matrices_; + // Output Hamiltonian and overlap matrices in HDF format + bool do_output_matrices_hdf_; + // Flag to open the files on first pass and print header line bool output_matrices_initialized_; From 068a3df11d105a55c85c31c54dcc2196ce76ee74 Mon Sep 17 00:00:00 2001 From: Mark Dewing Date: Mon, 2 Aug 2021 13:07:01 -0500 Subject: [PATCH 2/6] Change variable name suffix. Since there is now do_output_matrices_hdf_, change do_output_matrices_ to do_output_matrices_csv_ for clarity. --- .../WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp | 8 ++++---- .../WFOpt/QMCFixedSampleLinearOptimizeBatched.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp index e999d29a60..8935bd7849 100644 --- a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp +++ b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp @@ -99,7 +99,7 @@ QMCFixedSampleLinearOptimizeBatched::QMCFixedSampleLinearOptimizeBatched(const P MinMethod("OneShiftOnly"), previous_optimizer_type_(OptimizerType::NONE), current_optimizer_type_(OptimizerType::NONE), - do_output_matrices_(false), + do_output_matrices_csv_(false), do_output_matrices_hdf_(false), output_matrices_initialized_(false), freeze_parameters_(false) @@ -208,7 +208,7 @@ QMCFixedSampleLinearOptimizeBatched::RealType QMCFixedSampleLinearOptimizeBatche bool QMCFixedSampleLinearOptimizeBatched::run() { - if (do_output_matrices_ && !output_matrices_initialized_) + if (do_output_matrices_csv_ && !output_matrices_initialized_) { numParams = optTarget->getNumParams(); int N = numParams + 1; @@ -486,7 +486,7 @@ void QMCFixedSampleLinearOptimizeBatched::process(xmlNodePtr q) oAttrib.put(q); m_param.put(q); - do_output_matrices_ = (OutputMatrices != "no"); + do_output_matrices_csv_ = (OutputMatrices != "no"); do_output_matrices_hdf_ = (OutputMatricesHDF != "no"); freeze_parameters_ = (FreezeParameters != "no"); @@ -1286,7 +1286,7 @@ bool QMCFixedSampleLinearOptimizeBatched::one_shift_run() optTarget->fillOverlapHamiltonianMatrices(hamMat, ovlMat); invMat.copy(ovlMat); - if (do_output_matrices_) + if (do_output_matrices_csv_) { output_overlap_.output(ovlMat); output_hamiltonian_.output(hamMat); diff --git a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.h b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.h index 0813d3a4ac..f810a5f968 100644 --- a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.h +++ b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.h @@ -205,7 +205,7 @@ class QMCFixedSampleLinearOptimizeBatched : public QMCLinearOptimizeBatched, bool doHybrid; // Output Hamiltonian and overlap matrices - bool do_output_matrices_; + bool do_output_matrices_csv_; // Output Hamiltonian and overlap matrices in HDF format bool do_output_matrices_hdf_; From f3fc3a6f626beff37244378d614b74c0759ea1a1 Mon Sep 17 00:00:00 2001 From: Mark Dewing Date: Mon, 2 Aug 2021 13:16:43 -0500 Subject: [PATCH 3/6] Use candidate values to ParameterSet.add --- .../WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp index 8935bd7849..bbc1eaacda 100644 --- a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp +++ b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp @@ -479,16 +479,16 @@ void QMCFixedSampleLinearOptimizeBatched::process(xmlNodePtr q) oAttrib.add(vmcMove, "move"); oAttrib.add(ReportToH5, "hdf5"); - m_param.add(OutputMatrices, "output_matrices"); - m_param.add(OutputMatricesHDF, "output_matrices_hdf"); - m_param.add(FreezeParameters, "freeze_parameters"); + m_param.add(OutputMatrices, "output_matrices", {"no", "yes"}); + m_param.add(OutputMatricesHDF, "output_matrices_hdf", {"no", "yes"}); + m_param.add(FreezeParameters, "freeze_parameters", {"no", "yes"}); oAttrib.put(q); m_param.put(q); - do_output_matrices_csv_ = (OutputMatrices != "no"); - do_output_matrices_hdf_ = (OutputMatricesHDF != "no"); - freeze_parameters_ = (FreezeParameters != "no"); + do_output_matrices_csv_ = (OutputMatrices == "yes"); + do_output_matrices_hdf_ = (OutputMatricesHDF == "yes"); + freeze_parameters_ = (FreezeParameters == "yes"); // Use freeze_parameters with output_matrices to generate multiple lines in the output with // the same parameters so statistics can be computed in post-processing. From d61d0c136cbaa62b0e3f0ea4e84cefc2892944a7 Mon Sep 17 00:00:00 2001 From: Mark Dewing Date: Mon, 2 Aug 2021 15:35:38 -0500 Subject: [PATCH 4/6] Add csv suffix to parameter name Change output_matrices to output_matrices_csv. --- src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimize.cpp | 2 +- src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimize.cpp b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimize.cpp index ff9b3146da..5a4f85fb74 100644 --- a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimize.cpp +++ b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimize.cpp @@ -472,7 +472,7 @@ bool QMCFixedSampleLinearOptimize::put(xmlNodePtr q) oAttrib.add(vmcMove, "move"); oAttrib.add(ReportToH5, "hdf5"); - m_param.add(OutputMatrices, "output_matrices"); + m_param.add(OutputMatrices, "output_matrices_csv"); m_param.add(FreezeParameters, "freeze_parameters"); oAttrib.put(q); diff --git a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp index bbc1eaacda..fed012a8fa 100644 --- a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp +++ b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp @@ -479,7 +479,7 @@ void QMCFixedSampleLinearOptimizeBatched::process(xmlNodePtr q) oAttrib.add(vmcMove, "move"); oAttrib.add(ReportToH5, "hdf5"); - m_param.add(OutputMatrices, "output_matrices", {"no", "yes"}); + m_param.add(OutputMatrices, "output_matrices_csv", {"no", "yes"}); m_param.add(OutputMatricesHDF, "output_matrices_hdf", {"no", "yes"}); m_param.add(FreezeParameters, "freeze_parameters", {"no", "yes"}); From fc2df3ff2444519bca1a55d31268222512211295 Mon Sep 17 00:00:00 2001 From: Mark Dewing Date: Mon, 2 Aug 2021 15:43:30 -0500 Subject: [PATCH 5/6] Add documentation for linear method output --- docs/methods.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/methods.rst b/docs/methods.rst index ae30ac8295..791f16b047 100644 --- a/docs/methods.rst +++ b/docs/methods.rst @@ -1143,6 +1143,26 @@ the values found in the \*.sXXX.opt.h5 file. Be careful to keep the pair of optimized CI coefficients and Jastrow coefficients together to avoid inconsistencies. +Output of intermediate values +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Use the following parameters to the linear optimizers to output intermediate values such as the overlap and Hamiltonian matrices. + + +-------------------------+--------------+-------------+-------------+--------------------------------------------------+ + | **Name** | **Datatype** | **Values** | **Default** | **Description** | + +=========================+==============+=============+=============+==================================================+ + | ``output_matrices_csv`` | text | yes, no | no | Output linear method matrices to CSV files | + +-------------------------+--------------+-------------+-------------+--------------------------------------------------+ + | ``output_matrices_hdf`` | text | yes, no | no | Output linear method matrices to HDF file | + +-------------------------+--------------+-------------+-------------+--------------------------------------------------+ + | ``freeze_parameters`` | text | yes, no | no | Do not update parameters between iterations | + +-------------------------+--------------+-------------+-------------+--------------------------------------------------+ + + The ``output_matrices_csv`` parameter will write to .ham.s000.scalar.dat and .ovl.scalar.dat. One line per iteration of the optimizer loop. Combined with ``freeze_parameters``, this allows computing error bars on the matrices for use in regression testing. + + The ``output_matrices_hdf`` parameter will output in HDF format the matrices used in the linear method along with the shifts and the eigenvalue and eigenvector produced by QMCPACK. The file is named "linear_matrices.h5". It only works with the batched optimizer (``linear_batch``) + + .. _dmc: Diffusion Monte Carlo From 507dd96d1f81bbfb32fbbfe205fbcb6690b38fdb Mon Sep 17 00:00:00 2001 From: Mark Dewing Date: Mon, 9 Aug 2021 18:18:19 -0500 Subject: [PATCH 6/6] Add prefix to linear matrices hdf file Add the root name (project name + series) as the prefix. --- docs/methods.rst | 2 +- src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/methods.rst b/docs/methods.rst index 791f16b047..68628358f7 100644 --- a/docs/methods.rst +++ b/docs/methods.rst @@ -1160,7 +1160,7 @@ Use the following parameters to the linear optimizers to output intermediate val The ``output_matrices_csv`` parameter will write to .ham.s000.scalar.dat and .ovl.scalar.dat. One line per iteration of the optimizer loop. Combined with ``freeze_parameters``, this allows computing error bars on the matrices for use in regression testing. - The ``output_matrices_hdf`` parameter will output in HDF format the matrices used in the linear method along with the shifts and the eigenvalue and eigenvector produced by QMCPACK. The file is named "linear_matrices.h5". It only works with the batched optimizer (``linear_batch``) + The ``output_matrices_hdf`` parameter will output in HDF format the matrices used in the linear method along with the shifts and the eigenvalue and eigenvector produced by QMCPACK. The file is named "..linear_matrices.h5". It only works with the batched optimizer (``linear_batch``) .. _dmc: diff --git a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp index fed012a8fa..a05ba7091f 100644 --- a/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp +++ b/src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp @@ -1295,7 +1295,7 @@ bool QMCFixedSampleLinearOptimizeBatched::one_shift_run() hdf_archive hout; if (do_output_matrices_hdf_) { - std::string newh5 = "linear_matrices.h5"; + std::string newh5 = get_root_name() + ".linear_matrices.h5"; hout.create(newh5, H5F_ACC_TRUNC); hout.write(ovlMat, "overlap"); hout.write(hamMat, "Hamiltonian");