Commit ef632322 authored by Berk Onat's avatar Berk Onat

Added dictionary support for unit convertion

parent 8ab0c666
......@@ -5,7 +5,7 @@ from nomadcore.unit_conversion.unit_conversion import convert_unit
from nomadcore.caching_backend import CachingLevel
from nomadcore.simple_parser import mainFunction
from nomadcore.simple_parser import SimpleMatcher as SM
from AMBERDictionary import get_nameListDict, get_fileListDict, set_excludeList, set_includeList
from AMBERDictionary import get_unitDict, get_nameListDict, get_fileListDict, set_excludeList, set_includeList
from MetaInfoStorage import COMMON_META_INFO_PATH, PUBLIC_META_INFO_PATH
import MetaInfoStorage as mStore
import logging
......@@ -121,6 +121,7 @@ class AMBERParserBase(object):
)
self.metaStorage.build(jsonmetadata, 'section_run', exclude_dict)
self.re_program_name = re_program_name
self.unitDict = get_unitDict('si')
self.fileDict = get_fileListDict()
self.cntrlDict = get_nameListDict('cntrl')
self.ewaldDict = get_nameListDict('ewald')
......
......@@ -162,6 +162,47 @@ class MapDictionary(dict):
def get_keys(self):
return [val.metaName for val in self.__dict__.values()]
def get_unitDict(keyname):
unitDict = {
"si" : {
"meter" : "1.0",
"kilo" : "1.0e3",
"gram" : "1.0e-3",
"second" : "1.0",
"joule" : "1.0",
"newton" : "1.0",
"kelvin" : "1.0",
"pascal" : "1.0",
"coulomb" : "1.0",
"volt" : "1.0",
"centi" : "1.0e-2",
"milli" : "1.0e-3",
"micro" : "1.0e-6",
"nano" : "1.0e-9",
"pico" : "1.0e-12",
"femto" : "1.0e-15",
"atto" : "1.0e-18",
"erg" : "1.0e-7",
"dyne" : "1.0e-5",
"barye" : "1.0e-1",
"bar" : "1.0e-1",
"angstrom" : "1.0e-10",
"kcal" : "4184.096739614824",
"mole" : "0.602213737699784e24",
"atmosphere" : "1.01325e5",
"electron" : "1.602176565e-19",
"atomicmassunit" : "1.66054e-27",
"amu" : "1.66054e-27",
"bohr" : "5.29177249e-11",
"hartree" : "4.35974e-18",
"pascal" : "1.0",
}}
if keyname == "amber":
resDict = unitDict["si"]
else:
resDict = unitDict["si"]
return resDict
def metaNameConverter(keyName):
newName = keyName.lower().replace(" ", "").replace("-", "")
newName = newName.replace("(", "").replace(")", "")
......@@ -789,6 +830,8 @@ def get_updateDictionary(self, defname):
'energy_current' : MetaInfoMap(startpage),
'energy_electrostatic' : MetaInfoMap(startpage,
depends=[{'value' : 'EELEC'}],
unitdict=self.unitDict,
unit='electron-volt',
lookupdict=self.mddataDict
),
'energy_free_per_atom' : MetaInfoMap(startpage),
......@@ -802,6 +845,8 @@ def get_updateDictionary(self, defname):
'energy_total_T0' : MetaInfoMap(startpage),
'energy_total' : MetaInfoMap(startpage,
depends=[{'value' : 'Etot'}],
unitdict=self.unitDict,
unit='electron-volt',
lookupdict=self.mddataDict
),
'hessian_matrix' : MetaInfoMap(startpage),
......@@ -824,6 +869,8 @@ def get_updateDictionary(self, defname):
'energy_van_der_Waals_value' : MetaInfoMap(startpage,
depends=[{'value' : 'VDWAALS'}],
lookupdict=self.mddataDict,
unitdict=self.unitDict,
unit='electron-volt',
#autoSections=True,
activeSections=['section_energy_van_der_Waals']
),
......@@ -835,73 +882,102 @@ def get_updateDictionary(self, defname):
frameseq = {
'frame_sequence_conserved_quantity_frames' : MetaInfoMap(startpage,
depends=[{'store' : 'NSTEP'}],
valtype='int',
lookupdict=self.mddataDict
),
'frame_sequence_conserved_quantity_stats' : MetaInfoMap(startpage),
'frame_sequence_conserved_quantity' : MetaInfoMap(startpage,
depends=[{'store' : 'RESTRAINT'}],
valtype='float',
unitdict=self.unitDict,
unit='electron-volt',
lookupdict=self.mddataDict
),
'frame_sequence_continuation_kind' : MetaInfoMap(startpage),
'frame_sequence_external_url' : MetaInfoMap(startpage),
'frame_sequence_kinetic_energy_frames' : MetaInfoMap(startpage,
depends=[{'store' : 'NSTEP'}],
valtype='int',
lookupdict=self.mddataDict
),
'frame_sequence_kinetic_energy_stats' : MetaInfoMap(startpage),
'frame_sequence_kinetic_energy' : MetaInfoMap(startpage,
depends=[{'store' : 'EKtot'}],
valtype='float',
unitdict=self.unitDict,
unit='electron-volt',
lookupdict=self.mddataDict
),
'frame_sequence_local_frames_ref' : MetaInfoMap(startpage),
'frame_sequence_potential_energy_frames' : MetaInfoMap(startpage,
depends=[{'store' : 'NSTEP'}],
valtype='int',
lookupdict=self.mddataDict
),
'frame_sequence_potential_energy_stats' : MetaInfoMap(startpage),
'frame_sequence_potential_energy' : MetaInfoMap(startpage,
depends=[{'store' : 'EPtot'}],
valtype='float',
unitdict=self.unitDict,
unit='electron-volt',
lookupdict=self.mddataDict
),
'frame_sequence_pressure_frames' : MetaInfoMap(startpage,
depends=[{'store' : 'NSTEP'}],
valtype='int',
lookupdict=self.mddataDict
),
'frame_sequence_pressure_stats' : MetaInfoMap(startpage),
'frame_sequence_pressure' : MetaInfoMap(startpage,
depends=[{'store' : 'PRESS'}],
valtype='float',
lookupdict=self.mddataDict
),
'frame_sequence_temperature_frames' : MetaInfoMap(startpage,
depends=[{'store' : 'NSTEP'}],
valtype='int',
lookupdict=self.mddataDict
),
'frame_sequence_temperature_stats' : MetaInfoMap(startpage),
'frame_sequence_temperature' : MetaInfoMap(startpage,
depends=[{'store' : 'TEMP\(K\)'}],
valtype='float',
unitdict=self.unitDict,
unit='Kelvin',
lookupdict=self.mddataDict
),
'frame_sequence_time' : MetaInfoMap(startpage,
depends=[{'store' : 'TIME\(PS\)'}],
valtype='float',
unitdict=self.unitDict,
unit='pico-second',
lookupdict=self.mddataDict
),
'x_amber_frame_sequence_volume_frames' : MetaInfoMap(startpage,
depends=[{'store' : 'NSTEP'}],
valtype='int',
lookupdict=self.mddataDict
),
'x_amber_frame_sequence_volume' : MetaInfoMap(startpage,
depends=[{'store' : 'VOLUME'}],
valtype='float',
unitdict=self.unitDict,
unit='Angstrom**3',
lookupdict=self.mddataDict
),
'x_amber_frame_sequence_density_frames' : MetaInfoMap(startpage,
depends=[{'store' : 'NSTEP'}],
valtype='int',
lookupdict=self.mddataDict
),
'x_amber_frame_sequence_density' : MetaInfoMap(startpage,
depends=[{'store' : 'Density'}],
valtype='float',
unitdict=self.unitDict,
unit='1.0/Angstrom**3',
lookupdict=self.mddataDict
),
'frame_sequence_to_sampling_ref' : MetaInfoMap(startpage),
#'frame_sequence_to_sampling_ref' : MetaInfoMap(startpage),
'geometry_optimization_converged' : MetaInfoMap(startpage,
value=self.minConverged
),
......@@ -947,7 +1023,7 @@ def get_updateDictionary(self, defname):
)['frame_sequence_temperature_frames']
])
),
'previous_sequence_ref' : MetaInfoMap(startpage)
#'previous_sequence_ref' : MetaInfoMap(startpage)
}
# ----------------------------------
......
......@@ -200,7 +200,8 @@ class Container(object):
updateValue = None
storeValue = False
if "prefunction" in item:
storeValue, updateValue, item = item.functionbase.eval(item.prefunction % item)
prefunc = item.prefunction
storeValue, updateValue, item = prefunc(item)
if "depends" in item:
firstdepend = item["depends"][0]
if "lookupdict" in item:
......@@ -257,7 +258,29 @@ class Container(object):
elif "value" in item:
updateValue = item['value']
if "postfunction" in item:
storeValue, updateValue, item = item.functionbase.eval(item.postfunction % item)
postfunc = item.postfunction
storeValue, updateValue, item = postfunc(item)
if "valtype" in item:
if(isinstance(updateValue, list) or isinstance(updateValue, tuple)):
newUpdateValue = [eval(
item["valtype"]+"("+str(ival)+")"
) for ival in updateValue]
elif isinstance(updateValue, str):
newUpdateValue = eval(item["valtype"]+"("+str(updateValue)+")")
else:
newUpdateValue = updateValue
if("unit" in item and "unitdict" in item):
if(isinstance(updateValue, list) or isinstance(updateValue, tuple)):
updateValue = [self.unitConverter(
ival, item["unit"], item["unitdict"]
) for ival in updateValue]
elif(isinstance(updateValue, str) and float(updateValue)):
updateValue = float(updateValue)
updateValue = self.unitConverter(
updateValue, item["unit"], item["unitdict"])
else:
updateValue = self.unitConverter(
updateValue, item["unit"], item["unitdict"])
return storeValue, updateValue, localdict
def checkTestsDicts(self, item, localdict):
......@@ -319,6 +342,24 @@ class Container(object):
return checkval, localdict
return None, localdict
def unitConverter(self, updateValue, unit, unitdict):
""" Unit converter using definitions of units explicitly
The unit names are converted to numbers and the resulting
expression will be evaluated by python.
Ex.: unit = 'electron-volt/Angstrom^2'
will be converted to
unit = '1.602176565e-19*1.0/1.0e-10**2'
factor = eval(unit) = 16.02176565 Joule/meter^2
in SI units and the result will be calculated as follows:
output_value = input_value * factor
"""
newunit = unit.lower()
newunit = newunit.replace('-','*').replace(' ', '*').replace('^', "**")
for key,value in unitdict.items():
newunit = newunit.replace(str(key), str(value))
return float(updateValue) * float(eval(newunit))
def findNameInLookupDict(self, metaname, lookupdict):
for item in lookupdict:
itemMap = lookupdict[item]
......@@ -349,11 +390,18 @@ class Container(object):
#If we need to store the updated values
if self.Storage.__dict__[itemk]["val"] is None:
#If not initialized, initialize with a list to store
self.Storage.__dict__[itemk]["val"] = [updateValue]
if isinstance(updateValue, str) and float(updateValue):
self.Storage.__dict__[itemk]["val"] = [float(updateValue)]
else:
self.Storage.__dict__[itemk]["val"] = [updateValue]
else:
#Append to the stored list if there is a list
if type(self.Storage.__dict__[itemk]["val"]) is list:
self.Storage.__dict__[itemk]["val"].append(updateValue)
#self.Storage.__dict__[itemk]["val"].append(updateValue)
if isinstance(updateValue, str) and float(updateValue):
self.Storage.__dict__[itemk]["val"].append(float(updateValue))
else:
self.Storage.__dict__[itemk]["val"].append(updateValue)
else:
#Convert the prevoius update to list and append update
preValue = self.Storage.__dict__[itemk]["val"]
......@@ -643,14 +691,14 @@ if __name__ == "__main__":
'startSection' : [['section_topology']],
'muteSections' : [['section_interaction']],
'dictionary' : {
'molecule_constraint_atoms' : {'value' : 100, 'depends' : {}},
'interaction_atoms' : {'value' : 10, 'depends' : {}},
'topology_force_field_name' : {'value' : "ReaxFF", 'depends' : {}}
'molecule_constraint_atoms' : {'depends' : [[]], 'assign' : 100},
'interaction_atoms' : {'depends' : [[]], 'assign' : 10},
'topology_force_field_name' : {'depends' : [[]], 'assign' : "ReaxFF"}
}
}
run.populate(jsonmetadata, 'section_run', exclude_dict, updateDict)
#run.Color = 4
run.Color = 4
for container in run.Containers:
if 'section_topology' in container.Name:
select = container
......
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