Commit 0a4e21aa authored by Lauri Himanen's avatar Lauri Himanen
Browse files

Added a few things, removed unnecessary utils.

parent c8fbaf28
#===============================================================================
def try_to_add_value(backend, section, source_name, destination_name):
original = section[source_name]
if original is not None:
backend.addValue(destination_name, original[0])
#===============================================================================
def try_to_add_array_values(backend, section, source_name, destination_name, unit=None):
original = section[source_name]
if original is not None:
backend.addArrayValues(destination_name, original[0])
......@@ -37,7 +37,7 @@ class CommonMatcher(object):
}
#=======================================================================
# Cached values
# Globally cached values
self.cache_service.add_cache_object("simulation_cell", single=False, update=False)
self.cache_service.add_cache_object("number_of_scf_iterations", 0)
self.cache_service.add_cache_object("atom_positions", single=False, update=True)
......@@ -175,7 +175,7 @@ class CommonMatcher(object):
]
)
# SimpleMatcher for an SCF wavefunction optimization
# SimpleMatcher the stuff that is done to initialize a quickstep calculation
def quickstep_header(self):
return SM(
" MODULE QUICKSTEP: ATOMIC COORDINATES IN angstrom",
......@@ -192,8 +192,9 @@ class CommonMatcher(object):
# onClose triggers
def onClose_x_cp2k_section_total_numbers(self, backend, gIndex, section):
"""Keep track of how many SCF iteration are made."""
number_of_atoms = section["x_cp2k_atoms"][0]
self.cache_service["number_of_atoms"] = number_of_atoms
number_of_atoms = section.get_latest_value("x_cp2k_atoms")
if number_of_atoms is not None:
self.cache_service["number_of_atoms"] = number_of_atoms
def onClose_section_run(self, backend, gIndex, section):
"""Information that is pushed regardless at the end of parsing.
......@@ -212,7 +213,7 @@ class CommonMatcher(object):
# Transform the CP2K self-interaction correction string to the NOMAD
# correspondent, and push directly to the superBackend to avoid caching
try:
sic_cp2k = section["self_interaction_correction_method"][0]
sic_cp2k = section.get_latest_value("self_interaction_correction_method")
sic_map = {
"NO": "",
"AD SIC": "SIC_AD",
......@@ -232,7 +233,7 @@ class CommonMatcher(object):
"""
"""
# If the input file is available, parse it
input_file = section["x_cp2k_input_filename"][0]
input_file = section.get_latest_value("x_cp2k_input_filename")
filepath = self.file_service.get_absolute_path_to_file(input_file)
if filepath is not None:
input_parser = CP2KInputParser(filepath, self.parser_context)
......@@ -245,19 +246,13 @@ class CommonMatcher(object):
let's get it dynamically just in case there's something wrong.
"""
self.section_system_index = gIndex
# if self.forces is not None:
# backend.addArrayValues("atom_forces", self.forces, unit="forceAu")
# self.forces = None
self.cache_service.push_value("number_of_atoms")
self.cache_service.push_array_values("simulation_cell", unit="angstrom")
self.cache_service.push_array_values("configuration_periodic_dimensions")
self.cache_service.push_array_values("atom_positions", unit="angstrom")
# self.cache_service.push_array_values("atom_forces", unit="forceAu")
self.cache_service.push_array_values("atom_labels")
def onClose_section_single_configuration_calculation(self, backend, gIndex, section):
"""
"""
# Write the references to section_method and section_system
backend.addValue('single_configuration_to_calculation_method_ref', self.section_method_index)
backend.addValue('single_configuration_calculation_to_system_ref', self.section_system_index)
......
......@@ -5,9 +5,6 @@ import cp2kparser.generic.configurationreading
import cp2kparser.generic.csvparsing
from nomadcore.caching_backend import CachingLevel
import logging
import ase.io
import numpy as np
import math
logger = logging.getLogger("nomad")
......@@ -24,9 +21,11 @@ class CP2KGeoOptParser(MainHierarchicalParser):
self.traj_iterator = None
#=======================================================================
# Cached values
# Globally cached values
self.cache_service.add_cache_object("number_of_frames_in_sequence", 0)
self.cache_service.add_cache_object("frame_sequence_potential_energy", [])
self.cache_service.add_cache_object("frame_sequence_local_frames_ref", [])
self.cache_service.add_cache_object("geometry_optimization_method")
#=======================================================================
# Cache levels
......@@ -74,7 +73,7 @@ class CP2KGeoOptParser(MainHierarchicalParser):
endReStr=" Conv. in RMS gradients =",
name="geooptstep",
repeats=True,
sections=["section_system"],
sections=["section_single_configuration_calculation", "section_system"],
subMatchers=[
SM( "",
forwardMatch=True,
......@@ -174,23 +173,33 @@ class CP2KGeoOptParser(MainHierarchicalParser):
def onClose_x_cp2k_section_geometry_optimization(self, backend, gIndex, section):
# Get the re-evaluated energy and add it to frame_sequence_potential_energy
reeval = section["x_cp2k_section_geometry_optimization_energy_reevaluation"][0]
quickstep = reeval["x_cp2k_section_quickstep_calculation"][0]
energy = quickstep["x_cp2k_energy_total"]
self.cache_service["frame_sequence_potential_energy"].append(energy[0])
energy = section.get_latest_value([
"x_cp2k_section_geometry_optimization_energy_reevaluation",
"x_cp2k_section_quickstep_calculation",
"x_cp2k_energy_total"]
)
if energy is not None:
self.cache_service["frame_sequence_potential_energy"].append(energy)
# Push values from cache
self.cache_service.push_value("number_of_frames_in_sequence")
self.cache_service.push_array_values("frame_sequence_potential_energy")
self.cache_service.push_array_values("frame_sequence_local_frames_ref")
self.cache_service.push_value("geometry_optimization_method")
self.backend.addValue("frame_sequence_to_sampling_ref", 0)
opt_section = section["x_cp2k_section_geometry_optimization_step"]
if opt_section is not None:
opt_section = opt_section[-1]
geo_limit = opt_section["x_cp2k_optimization_step_size_convergence_limit"]
if geo_limit is not None:
self.backend.addValue("geometry_optimization_geometry_change", geo_limit[0])
force_limit = opt_section["x_cp2k_optimization_gradient_convergence_limit"]
if force_limit is not None:
self.backend.addValue("geometry_optimization_threshold_force", force_limit[0])
# Get the optimization convergence criteria from the last optimization
# step
section.add_latest_value([
"x_cp2k_section_geometry_optimization_step",
"x_cp2k_optimization_step_size_convergence_limit"],
"geometry_optimization_geometry_change",
)
section.add_latest_value([
"x_cp2k_section_geometry_optimization_step",
"x_cp2k_optimization_gradient_convergence_limit"],
"geometry_optimization_threshold_force",
)
def onClose_section_sampling_method(self, backend, gIndex, section):
self.backend.addValue("sampling_method", "geometry_optimization")
......@@ -214,6 +223,9 @@ class CP2KGeoOptParser(MainHierarchicalParser):
except ValueError:
pass
def onClose_section_single_configuration_calculation(self, backend, gIndex, section):
self.cache_service["frame_sequence_local_frames_ref"].append(gIndex)
#===========================================================================
# adHoc functions
def adHoc_geo_opt_converged(self):
......@@ -234,14 +246,14 @@ class CP2KGeoOptParser(MainHierarchicalParser):
"""Called when conjugate gradient method is used.
"""
def wrapper(parser):
parser.backend.addValue("geometry_optimization_method", "conjugate_gradient")
self.cache_service["geometry_optimization_method"] = "conjugate_gradient"
return wrapper
def adHoc_bfgs(self):
"""Called when conjugate gradient method is used.
"""
def wrapper(parser):
parser.backend.addValue("geometry_optimization_method", "bfgs")
self.cache_service["geometry_optimization_method"] = "bfgs"
return wrapper
def adHoc_step(self):
......@@ -253,8 +265,6 @@ class CP2KGeoOptParser(MainHierarchicalParser):
# Get the next position from the trajectory file
if self.traj_iterator is not None:
# pos = next(self.traj_iterator)
# self.cache_service["atom_positions"] = pos
try:
pos = next(self.traj_iterator)
except StopIteration:
......@@ -263,13 +273,3 @@ class CP2KGeoOptParser(MainHierarchicalParser):
self.cache_service["atom_positions"] = pos
return wrapper
def adHoc_setup_traj_file(self):
def wrapper(parser):
pass
return wrapper
def debug(self):
def wrapper(parser):
print "FOUND"
return wrapper
......@@ -3,7 +3,6 @@ from nomadcore.baseclasses import MainHierarchicalParser
from singlepointforceparser import CP2KSinglePointForceParser
from nomadcore.caching_backend import CachingLevel
from commonmatcher import CommonMatcher
from cp2kparser.generic.utils import try_to_add_value, try_to_add_array_values
import logging
logger = logging.getLogger("nomad")
......@@ -68,22 +67,22 @@ class CP2KSinglePointParser(MainHierarchicalParser):
"""Keep track of how many SCF iteration are made."""
self.cache_service["number_of_scf_iterations"] += 1
gId = backend.openSection("section_scf_iteration")
try_to_add_value(backend, section, "x_cp2k_energy_total_scf_iteration", "energy_total_scf_iteration")
try_to_add_value(backend, section, "x_cp2k_energy_XC_scf_iteration", "energy_XC_scf_iteration")
try_to_add_value(backend, section, "x_cp2k_energy_change_scf_iteration", "energy_change_scf_iteration")
section.add_latest_value("x_cp2k_energy_total_scf_iteration", "energy_total_scf_iteration")
section.add_latest_value("x_cp2k_energy_XC_scf_iteration", "energy_XC_scf_iteration")
section.add_latest_value("x_cp2k_energy_change_scf_iteration", "energy_change_scf_iteration")
backend.closeSection("section_scf_iteration", gId)
def onClose_x_cp2k_section_quickstep_calculation(self, backend, gIndex, section):
""""""
try_to_add_value(backend, section, "x_cp2k_energy_total", "energy_total")
try_to_add_value(backend, section, "x_cp2k_electronic_kinetic_energy", "electronic_kinetic_energy")
try_to_add_value(backend, section, "x_cp2k_quickstep_converged", "single_configuration_calculation_converged")
try_to_add_array_values(backend, section, "x_cp2k_atom_forces", "atom_forces")
section.add_latest_value("x_cp2k_energy_total", "energy_total")
section.add_latest_value("x_cp2k_electronic_kinetic_energy", "electronic_kinetic_energy")
section.add_latest_value("x_cp2k_quickstep_converged", "single_configuration_calculation_converged")
section.add_latest_array_values("x_cp2k_atom_forces", "atom_forces")
def onClose_x_cp2k_section_stress_tensor(self, backend, gIndex, section):
""""""
gId = backend.openSection("section_stress_tensor")
try_to_add_array_values(backend, section, "x_cp2k_stress_tensor", "stress_tensor")
section.add_latest_array_values("x_cp2k_stress_tensor", "stress_tensor")
backend.closeSection("section_stress_tensor", gId)
#===========================================================================
......
......@@ -42,7 +42,6 @@ object Cp2kParser extends SimpleExternalParserGenerator(
"parser-cp2k/cp2kparser/generic/inputparsing.py",
"parser-cp2k/cp2kparser/generic/configurationreading.py",
"parser-cp2k/cp2kparser/generic/csvparsing.py",
"parser-cp2k/cp2kparser/generic/utils.py",
"parser-cp2k/cp2kparser/versions/__init__.py",
"parser-cp2k/cp2kparser/versions/versionsetup.py",
"parser-cp2k/cp2kparser/versions/cp2k262/__init__.py",
......
......@@ -462,6 +462,15 @@ class TestGeoOpt(unittest.TestCase):
result = self.results["number_of_frames_in_sequence"]
self.assertEqual(result, 7)
def test_frame_sequence_to_sampling_ref(self):
result = self.results["frame_sequence_to_sampling_ref"]
self.assertEqual(result, 0)
def test_frame_sequence_local_frames_ref(self):
result = self.results["frame_sequence_local_frames_ref"]
expected_result = np.array([0, 1, 2, 3, 4, 5])
self.assertTrue(np.array_equal(result, expected_result))
def test_sampling_method(self):
result = self.results["sampling_method"]
self.assertEqual(result, "geometry_optimization")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment