Skip to content

Commit

Permalink
Merge pull request #3332 from markdewing/matrix_output_hdf
Browse files Browse the repository at this point in the history
Add linear matrix HDF output
  • Loading branch information
ye-luo authored Aug 10, 2021
2 parents a5b725b + 3b56bd3 commit 18efaa4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
20 changes: 20 additions & 0 deletions docs/methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <base name>.ham.s000.scalar.dat and <base name>.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 "<base name>.<series number>.linear_matrices.h5". It only works with the batched optimizer (``linear_batch``)


.. _dmc:

Diffusion Monte Carlo
Expand Down
2 changes: 1 addition & 1 deletion src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
41 changes: 32 additions & 9 deletions src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ 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)

Expand Down Expand Up @@ -207,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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -471,20 +472,23 @@ 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");
oAttrib.add(vmcMove, "move");
oAttrib.add(ReportToH5, "hdf5");

m_param.add(OutputMatrices, "output_matrices");
m_param.add(FreezeParameters, "freeze_parameters");
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"});

oAttrib.put(q);
m_param.put(q);

do_output_matrices_ = (OutputMatrices != "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.
Expand Down Expand Up @@ -1282,12 +1286,23 @@ 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);
}

hdf_archive hout;
if (do_output_matrices_hdf_)
{
std::string newh5 = get_root_name() + ".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++)
{
Expand All @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/QMCDrivers/WFOpt/QMCFixedSampleLinearOptimizeBatched.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ 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_;

// Flag to open the files on first pass and print header line
bool output_matrices_initialized_;
Expand Down

0 comments on commit 18efaa4

Please sign in to comment.