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

ROM calculations of Pinned H2O with 3 DOF #289

Draft
wants to merge 20 commits into
base: ROMFPMD
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 0 additions & 63 deletions examples/PinnedH2O/generate_coord.py

This file was deleted.

28 changes: 28 additions & 0 deletions examples/PinnedH2O/get_ROM_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import subprocess
import re

pattern = r"For energy fraction: \d+\.\d+, take first (\d+) of \d+ basis vectors"

print("\\begin{tabular}{|c||c|c|c|c|c|c|c|}")
print("\\hline")
print("$k$ & $\\varepsilon = 10^{-1}$ & $\\varepsilon = 10^{-2}$ & $\\varepsilon = 10^{-3}$ & $\\varepsilon = 10^{-4}$ & $\\varepsilon = 10^{-5}$ & Snapshots \\\\")
print("\\hline")

for t in range(10):
k = 50*(t+1)
snapshots = 4*k
grep_command = f"grep 'take first' basis_1_{k}_Pinned_H2O.out"
result = subprocess.run(grep_command, shell=True, capture_output=True, text=True)
matches = re.findall(pattern, result.stdout)
energy_fractions = {
"0.9": matches[0],
"0.99": matches[1],
"0.999": matches[2],
"0.9999": matches[3],
"0.99999": matches[4],
}
line = f"{k} & {energy_fractions['0.9']} & {energy_fractions['0.99']} & {energy_fractions['0.999']} & {energy_fractions['0.9999']} & {energy_fractions['0.99999']} & {snapshots} \\\\"
print(line)

print("\\hline")
print("\\end{tabular}")
3 changes: 3 additions & 0 deletions src/ExtendedGridOrbitals.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ class ExtendedGridOrbitals : public Orbitals
const pb::Grid& mygrid = mymesh->grid();
return mygrid.maxDomainSize();
}
#ifdef MGMOL_HAS_LIBROM
void set(std::string file_path, int rdim);
#endif
};

#endif
4 changes: 2 additions & 2 deletions src/MGmol.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ class MGmol : public MGmolInterface
}

#ifdef MGMOL_HAS_LIBROM
int save_orbital_snapshot(std::string snapshot_dir, OrbitalsType& orbitals);
void project_orbital(std::string snapshot_dir, int rdim, OrbitalsType& orbitals);
int save_orbital_snapshot(std::string file_path, OrbitalsType& orbitals);
void project_orbital(std::string file_path, int rdim, OrbitalsType& orbitals);
#endif
};
// Instantiate static variables here to avoid clang warnings
Expand Down
1 change: 0 additions & 1 deletion src/md.cc
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,6 @@ void MGmol<OrbitalsType>::md(OrbitalsType** orbitals, Ions& ions)
force(**orbitals, ions);

#ifdef MGMOL_HAS_LIBROM
// TODO: cleanup
if (ct.getROMOptions().num_orbbasis > 0)
{
if (onpe0)
Expand Down
20 changes: 20 additions & 0 deletions src/rom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#ifdef MGMOL_HAS_LIBROM

#include "LocGridOrbitals.h"
#include "ExtendedGridOrbitals.h"
#include "MGmol.h"

#include "librom.h"
Expand Down Expand Up @@ -91,6 +92,25 @@ void MGmol<OrbitalsType>::project_orbital(std::string file_path, int rdim, Orbit
}
}

void ExtendedGridOrbitals::set(std::string file_path, int rdim)
{
const int dim = getLocNumpt();

CAROM::BasisReader reader(file_path);
CAROM::Matrix* orbital_basis = reader.getSpatialBasis(rdim);

Control& ct = *(Control::instance());
Mesh* mymesh = Mesh::instance();
pb::GridFunc<ORBDTYPE> gf_psi(mymesh->grid(), ct.bcWF[0], ct.bcWF[1], ct.bcWF[2]);
CAROM::Vector psi;
for (int i = 0; i < rdim; ++i)
{
orbital_basis->getColumn(i, psi);
gf_psi.assign(psi.getData());
setPsi(gf_psi, i);
}
}

template class MGmol<LocGridOrbitals>;
template class MGmol<ExtendedGridOrbitals>;

Expand Down
11 changes: 11 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ add_executable(testWFEnergyAndForces
${CMAKE_SOURCE_DIR}/tests/WFEnergyAndForces/testWFEnergyAndForces.cc)
add_executable(testDMandEnergyAndForces
${CMAKE_SOURCE_DIR}/tests/DMandEnergyAndForces/testDMandEnergyAndForces.cc)
add_executable(testPinnedH2O_3DOF
${CMAKE_SOURCE_DIR}/tests/PinnedH2O_3DOF/testPinnedH2O_3DOF.cc)

if(${MAGMA_FOUND})
add_executable(testOpenmpOffload
Expand Down Expand Up @@ -369,6 +371,14 @@ add_test(NAME testDMandEnergyAndForces
${CMAKE_CURRENT_SOURCE_DIR}/DMandEnergyAndForces/coords.in
${CMAKE_CURRENT_SOURCE_DIR}/DMandEnergyAndForces/lrs.in
${CMAKE_CURRENT_SOURCE_DIR}/../potentials)
add_test(NAME testPinnedH2O_3DOF
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/PinnedH2O_3DOF/test.py
${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} 4 ${MPIEXEC_PREFLAGS}
${CMAKE_CURRENT_BINARY_DIR}/testPinnedH2O_3DOF
${CMAKE_CURRENT_SOURCE_DIR}/PinnedH2O_3DOF/mgmol.cfg
${CMAKE_CURRENT_SOURCE_DIR}/PinnedH2O_3DOF/coords.in
${CMAKE_CURRENT_SOURCE_DIR}/PinnedH2O_3DOF/lrs.in
${CMAKE_CURRENT_SOURCE_DIR}/../potentials)

if(${MAGMA_FOUND})
add_test(NAME testOpenmpOffload
Expand Down Expand Up @@ -557,6 +567,7 @@ target_link_libraries(testDirectionalReduce PRIVATE MPI::MPI_CXX)
target_link_libraries(testEnergyAndForces PRIVATE mgmol_src)
target_link_libraries(testWFEnergyAndForces PRIVATE mgmol_src)
target_link_libraries(testDMandEnergyAndForces PRIVATE mgmol_src)
target_link_libraries(testPinnedH2O_3DOF PRIVATE mgmol_src)
target_link_libraries(testIons PRIVATE mgmol_src)

if(${MAGMA_FOUND})
Expand Down
1 change: 1 addition & 0 deletions tests/PinnedH2O_3DOF/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3 test.py srun -p debug -n 64 ../../build_quartz/tests/testPinnedH2O_3DOF mgmol_online.cfg coords_1.00_1.00_0.0.in ../../potentials
Binary file added tests/PinnedH2O_3DOF/aux/PinnedH2O_3DOF.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions tests/PinnedH2O_3DOF/aux/get_ROM_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import subprocess
import re

bondlength_num_increments = (2, 5, 10)
bondangle_num_increments = (2, 5, 10)

pattern = r"For energy fraction: \d+\.\d+, take first (\d+) of \d+ basis vectors"

print("\\begin{tabular}{|c|c||c|c|c|c|c|c|c|}")
print("\\hline")
print("$N_L$ & $N_\\theta$ & $\\varepsilon = 10^{-1}$ & $\\varepsilon = 10^{-2}$ & $\\varepsilon = 10^{-3}$ & $\\varepsilon = 10^{-4}$ & $\\varepsilon = 10^{-5}$ & Snapshots \\\\")
print("\\hline")

for _, N_L in enumerate(bondlength_num_increments):
for _, N_theta in enumerate(bondangle_num_increments):
snapshots = 2*(N_L+1)*(N_L+2)*(N_theta+1)
grep_command = f"grep 'take first' basis_{N_L}_{N_theta}_PinnedH2O_3DOF.out"
result = subprocess.run(grep_command, shell=True, capture_output=True, text=True)
matches = re.findall(pattern, result.stdout)
energy_fractions = {
"0.9": matches[0],
"0.99": matches[1],
"0.999": matches[2],
"0.9999": matches[3],
"0.99999": matches[4],
}
line = f"{N_L} & {N_theta} & {energy_fractions['0.9']} & {energy_fractions['0.99']} & {energy_fractions['0.999']} & {energy_fractions['0.9999']} & {energy_fractions['0.99999']} & {snapshots} \\\\"
print(line)

print("\\hline")
print("\\end{tabular}")
69 changes: 69 additions & 0 deletions tests/PinnedH2O_3DOF/aux/plot_PinnedH2O_3DOF.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

ref_bondlength = 1.83
ref_bondangle = 104.5

# factors and increments for bond lengths and bond angle
bondlength_factor = np.linspace(0.95, 1.05, 3)
bondangle_increment = np.linspace(-5, 5, 3)

fig, ax = plt.subplots()

for d_bondangle in bondangle_increment:
bondangle = ref_bondangle + d_bondangle
x = ref_bondlength * np.cos(np.radians(bondangle / 2))
y = ref_bondlength * np.sin(np.radians(bondangle / 2))
for i, f_bondlength1 in enumerate(bondlength_factor):
H1_x = f_bondlength1*x
H1_y = f_bondlength1*y
ax.plot(H1_x, H1_y, 'ro', markersize=8, alpha=0.1)
for f_bondlength2 in bondlength_factor[:(i+1)]:
H2_x = f_bondlength2*x
H2_y = -f_bondlength2*y
ax.plot(H2_x, H2_y, 'bo', markersize=8, alpha=0.1)

ref_bondangle = np.radians(ref_bondangle)
x = ref_bondlength * np.cos(ref_bondangle / 2)
y = ref_bondlength * np.sin(ref_bondangle / 2)

ax.plot([0, x], [0, y], 'r-', lw=2)
ax.plot([0, x], [0, -y], 'b-', lw=2)
ax.plot(0, 0, 'ko', markersize=8)
ax.plot(x, y, 'ro', markersize=8)
ax.plot(x, -y, 'bo', markersize=8)

arc1 = matplotlib.patches.Arc(xy=(0, 0),
width=0.5,
height=0.5,
angle=0,
theta1=0,
theta2=ref_bondangle/2,
color='red',
linewidth=2)
ax.add_patch(arc1)
ax.text(0.4, 0.25, r"$\frac{\theta}{2}$", ha='center', va='center', fontsize=12, color='red')

arc2 = matplotlib.patches.Arc(xy=(0, 0),
width=0.5,
height=0.5,
angle=0,
theta1=-ref_bondangle/2,
theta2=0,
color='blue',
linewidth=2)
ax.add_patch(arc2)
ax.text(0.4, -0.25, r"$\frac{\theta}{2}$", ha='center', va='center', fontsize=12, color='blue')

ax.text(x / 2 - 0.1, y / 2, r"$L_1$", ha='right', color='red')
ax.text(x / 2 - 0.1, -y / 2, r"$L_2$", ha='right', color='blue')

ax.plot([-2, 2], [0, 0], 'k--', lw=1)
ax.set_xlim(-2.0, 2.0)
ax.set_ylim(-2.0, 2.0)
ax.set_aspect('equal')
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.grid(True)
plt.savefig("PinnedH2O_3DOF.png")
Loading
Loading