Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read and write orbital rotation parameters #4580

Merged
merged 6 commits into from
May 1, 2023

Conversation

markdewing
Copy link
Contributor

Implement write/readVariationalParameters for orbital rotation.

The parameters associated with either global rotation or a history list are stored to the VP HDF file.

The parameters in myVars are also saved so the first call to resetParameters works correctly. The first call occurs in WaveFunctionFactory::buildTWF after the VP file is read. By "works correctly" , I mean the parameters set in myVars and the parameters in read into VariableSet match, so the change in parameters in RotatedSPOs::resetParametersExclusive is zero, and that causes no change to the carefully restored rotation matrices.

There are unit tests that do minimal testing of the code paths. There will need to be more complete tests of this functionality.

What type(s) of changes does this code introduce?

Delete the items that do not apply

  • New feature

Does this introduce a breaking change?

  • No

What systems has this change been tested on?

desktop

Checklist

Update the following with a yes where the items apply. If you're unsure about any of them, don't hesitate to ask. This is
simply a reminder of what we are going to look for before merging your code.

  • Yes. This PR is up to date with current the current state of 'develop'
  • Yes. Code added or changed in the PR has been clang-formatted
  • Yes This PR adds tests to cover any new code, or to catch a bug that is being fixed
  • No. Documentation has been added (if appropriate)

Add functions for applying either global rotation or history list.
The choice of history list vs. global rotation for a given run
can be independent of how is was stored in the VP file.
That is, the method used to restore the parameters in RotatedSPOs
is determined by what method is stored in the VP file.
Once the parameters in the RotatedSPO class are set up, it does not matter
what method future optimization runs use.
hout.push("RotatedSPOs");
if (use_global_rot_)
{
hid_t grp = hout.push("rotation_global");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind picking up #4547?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be done in this PR though?
Merge this and keep moving?
@markdewing your preferences?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to make the change in #4547 separate from this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#4547 is definitely a separate PR. I would like it to be addressed instead of mentioning the same issue in multiple PRs. We can let this PR. go.

@prckent
Copy link
Contributor

prckent commented May 1, 2023

Test this please

if (use_global_rot_)
{
hid_t grp = hout.push("rotation_global");
std::string rot_global_name = std::string("rotation_global_") + SPOSet::getName();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add const if the variable won't be modified?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a separate discussion about what good ideas we want to enforce on the code. Adding 'const' to variables is not listed in the "Uses of const" section of the coding standards. And I question whether it's necessary for variables - it adds extra characters to the declaration without adding a lot of information (IMO).

hid_t grp = hout.push("rotation_global");
std::string rot_global_name = std::string("rotation_global_") + SPOSet::getName();

int nparam = myVarsFull.size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add const if the variable won't be modified?

Copy link
Contributor

@ye-luo ye-luo May 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int nparam = myVarsFull.size();

seems to initialize nparam as myVarsFull.size() and it may be modified.

const int nparam = myVarsFull.size();

made it clear. No modification will be applied on nparam. It can be safely used as myVarsFull.size() in the intended scope.

Copy link
Contributor Author

@markdewing markdewing May 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used nparam as a shortcut name for myVarsFull.size() here and as a shortcut name for myVars.size() later. While these are technically in different scopes and the compiler is okay with the name reuse, it might be confusing to the reader. I updated the shortcut name to nparam_full when referring to myVarsFull.size().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is exactly the purpose of using const to enhance readability. I have seen temp being reused in a similar way and completely destroys the readability.

else
{
hid_t grp = hout.push("rotation_history");
size_t rows = history_params_.size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add const if the variable won't be modified?

size_t rows = history_params_.size();
size_t cols = 0;
if (rows > 0)
cols = history_params_[0].size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const size_t cols = rows > 0 ? history_params_[0].size() : 0;

for (size_t j = 0; j < cols; j++)
tmp(i, j) = history_params_[i][j];

std::string rot_hist_name = std::string("rotation_history_") + SPOSet::getName();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add const if the variable won't be modified?

if (!hin.getShape<RealType>(rot_param_name, sizes))
throw std::runtime_error("Failed to read rotation_params in VP file");

int nparam_actual = sizes[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add const if the variable won't be modified?

}

hin.push("rotation_params", false);
std::string rot_param_name = std::string("rotation_params_") + SPOSet::getName();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add const if the variable won't be modified?

throw std::runtime_error("Failed to read rotation_params in VP file");

int nparam_actual = sizes[0];
int nparam = myVars.size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add const if the variable won't be modified?

src/QMCWaveFunctions/tests/test_RotatedSPOs.cpp Outdated Show resolved Hide resolved
@@ -21,6 +21,7 @@
#include "QMCWaveFunctions/EinsplineSetBuilder.h"
#include "QMCWaveFunctions/RotatedSPOs.h"
#include "checkMatrix.hpp"
#include "FakeSPO.h"

#include <stdio.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdio.h->cstdio

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of change is starting to feel overly picky. Part of my motivation for #4559 was to make these kind of small but widespread updates to the code base separately. Then hopefully new code would pick up the changes by copying the updated code, and reviews would need to be less concerned about these kind of changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about. Fair point. It is a line not part of this PR.
I actually just wanted to take a note as I was reading through the code base and raise awareness of developers.

Copy link
Contributor

@ye-luo ye-luo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although getting pushbacks, I still feel the need of using const more than the current state. Will update dev docs.

@ye-luo
Copy link
Contributor

ye-luo commented May 1, 2023

Test this please

@prckent prckent self-requested a review May 1, 2023 21:15
Copy link
Contributor

@prckent prckent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this gets tweaked further, I'll note that I do agree that adding const does improve readability, particularly for things like int nparam = myVars.size();. A casual reader can more readily see what will or will not be changed.

@prckent prckent merged commit 81f9e4b into QMCPACK:develop May 1, 2023
@markdewing markdewing deleted the rw_rotation_params branch May 2, 2023 04:34
@prckent prckent mentioned this pull request Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants