Skip to content

Commit

Permalink
Cleaned up the imports, moved constants to separate file temporarily to
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders Brakestad committed Mar 25, 2022
1 parent 20a4217 commit 7960907
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
20 changes: 9 additions & 11 deletions python/mrchem/CUBEparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
# For information on the complete list of contributors to MRChem, see:
# <https://mrchem.readthedocs.io/>
#
from .input_parser.plumbing import pyparsing as pp

import os
from json import dump

BOHR_2_METER = 5.29177210903e-11
"""Conversion from atomic units of length (Bohr) to meter (CODATA 2018)"""
ANGSTROM_2_BOHR = 1e-10 / BOHR_2_METER
#ANGSTROM_2_BOHR = 1.889725989
"""Conversion factor from Angstrom to Bohr"""
from .input_parser.plumbing import pyparsing as pp
from.physical_constants import PhysicalConstants as PC


def write_cube_dict(file_dict, world_unit):
all_path_list = []
Expand Down Expand Up @@ -175,7 +173,7 @@ def count(s, l, t):

# get cube origin data
N_atoms = parsed_cube["NATOMS"]
origin = parsed_cube["ORIGIN"] if (world_unit == "bohr") else [p*ANGSTROM_2_BOHR for p in parsed_cube["ORIGIN"]]
origin = parsed_cube["ORIGIN"] if (world_unit == "bohr") else [p*PC.ANGSTROM_2_BOHR for p in parsed_cube["ORIGIN"]]

# Set the amount of values depending on if the DSET_IDs were present or not
if (len(parsed_cube["DSET_IDS"]) != 0):
Expand All @@ -200,9 +198,9 @@ def count(s, l, t):
if (world_unit == "bohr"):
Voxel_axes = [parsed_cube["XAXIS"]["VECTOR"], parsed_cube["YAXIS"]["VECTOR"], parsed_cube["ZAXIS"]["VECTOR"]]
else:
X_voxel = [p*ANGSTROM_2_BOHR for p in parsed_cube["XAXIS"]["VECTOR"]]
Y_voxel = [p*ANGSTROM_2_BOHR for p in parsed_cube["YAXIS"]["VECTOR"]]
Z_voxel = [p*ANGSTROM_2_BOHR for p in parsed_cube["ZAXIS"]["VECTOR"]]
X_voxel = [p*PC.ANGSTROM_2_BOHR for p in parsed_cube["XAXIS"]["VECTOR"]]
Y_voxel = [p*PC.ANGSTROM_2_BOHR for p in parsed_cube["YAXIS"]["VECTOR"]]
Z_voxel = [p*PC.ANGSTROM_2_BOHR for p in parsed_cube["ZAXIS"]["VECTOR"]]
Voxel_axes = [X_voxel, Y_voxel, Z_voxel]

# get the atom coordinates
Expand All @@ -211,7 +209,7 @@ def count(s, l, t):

Z_n = [atom["ATOMIC_NUMBER"] for atom in parsed_cube["GEOM"]]
atom_charges = [atom["CHARGE"] for atom in parsed_cube["GEOM"]]
atom_coords = [atom["POSITION"] if (world_unit == "bohr") else [p*ANGSTROM_2_BOHR for p in atom["POSITION"]] for atom in parsed_cube["GEOM"]]
atom_coords = [atom["POSITION"] if (world_unit == "bohr") else [p*PC.ANGSTROM_2_BOHR for p in atom["POSITION"]] for atom in parsed_cube["GEOM"]]

# construct the CUBE vector. Indexing is CUBE_vector[MO_ID][i*N_vals[1]*N_vals[2] + j*N_vals[2] + k] where i, j and k correspond to steps in the X, Y and Z voxel axes directions respectively.
CUBE_vector = []
Expand Down
14 changes: 11 additions & 3 deletions python/mrchem/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@

import math

from .helpers import *
from .CUBEparser import *
from .helpers import (
write_scf_fock,
write_scf_guess,
write_scf_solver,
write_scf_properties,
write_scf_plot,
write_rsp_calc,
parse_wf_method
)
from .physical_constants import PhysicalConstants as PC
from .periodictable import PeriodicTable as PT, PeriodicTableByZ as PT_Z
from .validators import MoleculeValidator

Expand All @@ -35,7 +43,7 @@ def translate_input(user_dict):
# get the origin in the desired units of measure
origin = user_dict["world_origin"]
if user_dict["world_unit"] == "angstrom":
origin = [ANGSTROM_2_BOHR * r for r in origin]
origin = [PC.ANGSTROM_2_BOHR * r for r in origin]

# prepare bits and pieces
mol_dict = write_molecule(user_dict, origin)
Expand Down
10 changes: 3 additions & 7 deletions python/mrchem/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@
# <https://mrchem.readthedocs.io/>
#

from .CUBEparser import *
from .physical_constants import PhysicalConstants as PC
from .CUBEparser import write_cube_dict

BOHR_2_METER = 5.29177210903e-11
"""Conversion from atomic units of length (Bohr) to meter (CODATA 2018)"""
ANGSTROM_2_BOHR = 1e-10 / BOHR_2_METER
#ANGSTROM_2_BOHR = 1.889725989
"""Conversion factor from Angstrom to Bohr"""
# yapf: disable
SHORTHAND_FUNCTIONALS = [
'svwn3',
Expand Down Expand Up @@ -262,7 +258,7 @@ def write_scf_plot(user_dict):
plot_dict["plotter"] = user_dict["Plotter"]
if user_dict["world_unit"] == "angstrom":
plot_dict["plotter"] = {
k: [ANGSTROM_2_BOHR * r for r in plot_dict["plotter"][k]]
k: [PC.ANGSTROM_2_BOHR * r for r in plot_dict["plotter"][k]]
for k in plot_dict["plotter"].keys()
}
return plot_dict
Expand Down
28 changes: 28 additions & 0 deletions python/mrchem/physical_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# MRChem, a numerical real-space code for molecular electronic structure
# calculations within the self-consistent field (SCF) approximations of quantum
# chemistry (Hartree-Fock and Density Functional Theory).
# Copyright (C) 2022 Stig Rune Jensen, Luca Frediani, Peter Wind and contributors.
#
# This file is part of MRChem.
#
# MRChem is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MRChem is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with MRChem. If not, see <https://www.gnu.org/licenses/>.
#
# For information on the complete list of contributors to MRChem, see:
# <https://mrchem.readthedocs.io/>
#

class PhysicalConstants:
BOHR_2_METER = 5.29177210903e-11
ANGSTROM_2_BOHR = 1.0e-10 / BOHR_2_METER
8 changes: 4 additions & 4 deletions python/mrchem/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import itertools
import re

from .helpers import ANGSTROM_2_BOHR
from .physical_constants import PhysicalConstants as PC
from .periodictable import PeriodicTable, PeriodicTableByZ


Expand Down Expand Up @@ -127,7 +127,7 @@ def __init__(self, user_dict, origin):
self.atomic_coords = self.ang2bohr_array(self.atomic_coords)
self.cavity_coords = self.ang2bohr_array(self.cavity_coords)
self.cavity_radii = self.ang2bohr_vector(self.cavity_radii)
self.cavity_width *= ANGSTROM_2_BOHR
self.cavity_width *= PC.ANGSTROM_2_BOHR

def get_coords_in_program_syntax(self):
"""Convert nuclear coordinates from JSON syntax to program syntax."""
Expand Down Expand Up @@ -328,10 +328,10 @@ def euclidian_distance(a, b):
def ang2bohr_array(coords):
"""Helper function. Convert List[List[float]] from angstrom to bohr."""
return [
[c * ANGSTROM_2_BOHR for c in element] for element in coords
[c * PC.ANGSTROM_2_BOHR for c in element] for element in coords
]

@staticmethod
def ang2bohr_vector(vec):
"""Helper function. Convert List[float] from angstrom to bohr"""
return [el * ANGSTROM_2_BOHR for el in vec]
return [el * PC.ANGSTROM_2_BOHR for el in vec]

0 comments on commit 7960907

Please sign in to comment.