Skip to content

Instantly share code, notes, and snippets.

@eudoxos
Last active March 3, 2022 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eudoxos/8bed015bdcc4f7fd866911ceb5c8cd8f to your computer and use it in GitHub Desktop.
Save eudoxos/8bed015bdcc4f7fd866911ceb5c8cd8f to your computer and use it in GitHub Desktop.
uoi-heavystruct
import mupif as mp
import tqdm
# schemata must be passed at string to HeavyStruct constructor
# they are only needed when creating the container for the first time
# any subsequent access uses schemata stored in the container itself
schemata=open('schemata.json').read()
# schemaName specifies the top-level schema
# to create molecule-only container, say schemaName='gr.uoi.kostis.molecule', as in the second example
# context manger wraps opening/closing the data and is roughly equivalent to this:
#
# hs=mp.HeavyStruct(...)
# grains=hs.openData()
# ...
# hs.closeData()
with mp.HeavyStruct(h5path='test-grain.h5',mode='overwrite',schemaName='gr.uoi.kostis.grain',schemasJson=schemata) as grains:
grains.resize(20)
for ig,g in enumerate(tqdm.tqdm(grains)):
g.getProperties().setFreeElectrons(100+ig)
molecules=g.getMolecules()
molecules.resize(20)
for m in molecules:
atoms=m.getAtoms()
atoms.resize(20)
for a in atoms:
a.getGeneralInfo().setElement('H')
with mp.HeavyStruct(h5path='test-molecule.h5',mode='overwrite',schemaName='gr.uoi.kostis.molecule',schemasJson=schemata) as molecules:
molecules.resize(20)
for im,m in enumerate(molecules):
m.getProperties().setHOMO(im*5*mp.U.eV)
atoms=m.getAtoms()
atoms.resize(20)
for a in atoms:
a.getGeneralInfo().setElement('Si')
import mupif as mp
import subprocess
# open input data as read-only, extract whatever data is needed
with mp.HeavyStruct(h5path='test-grain.h5',mode='readonly') as grains:
for ig,g in enumerate(grains): print(f'grain #{ig}: {g.getProperties().getFreeElectrons()} free electrons')
with mp.HeavyStruct(h5path='test-molecule.h5',mode='readonly') as molecules:
for im,m in enumerate(molecules):
print(f'molecule #{im}: HOMO is {m.getProperties().getHOMO()}')
# generate files for external code and call it
subprocess.run(['/bin/true','some','other','args','don\'t use os.system(), it stinks'],check=True)
# write back modified copy of the original container
# this is the same as copying the container (h5path) to a temporary and opening it for writing
with (copy:=mp.HeavyStruct(h5path='test-grain.h5',mode='copy-readwrite')) as grains:
for ig,g in enumerate(grains): g.getProperties().setFreeElectrons(200+ig)
# just to check the temporary contains the updated container
with mp.HeavyStruct(h5path=copy.h5path,mode='readonly') as grains:
for ig,g in enumerate(grains): print(f'grain #{ig}: {g.getProperties().getFreeElectrons()} free electrons')
[
{
"_schema":{
"name": "gr.uoi.kostis.grain",
"version": "0.5"
},
"_datasetName": "grains",
"generalInfo":{
"material":{ "dtype": "a" }
},
"computationalInfo":{
"boundaryCondition": { "dtype": "a" }
},
"structure":{
"cellSize":{ "dtype":"d", "shape":[3],"unit":"m"}
},
"link":{
"other":{ "dtype":"l", "shape": "variable" }
},
"properties":{
"freeElectrons":{ "dtype":"l" },
"freeHoles":{ "dtype":"l" },
"reogranizationEnergyExternal":{ "dtype":"d", "unit":"J" }
},
"molecules": { "schema":"gr.uoi.kostis.molecule" }
},
{
"_schema":{
"name": "gr.uoi.kostis.molecule",
"version": "0.5"
},
"_datasetName": "molecules",
"generalInfo":{
"chemicalName":{ "dtype":"a" },
"molecularWeight":{ "dtype":"d", "unit":"g/mol"},
"type":{ "dtype":"a2"}
},
"computationalInfo":{
"forceFieldType": {"dtype":"l" }
},
"structure":{
"centerOfMass":{ "dtype":"d", "shape": [3], "unit":"m" },
"nearestNeighbors":{ "dtype":"l", "shape": "variable" },
"symmetryAxis": { "dtype":"d", "shape": [3] }
},
"link":{
"other":{ "dtype":"l", "shape": "variable" }
},
"properties":{
"HOMO":{ "dtype":"d", "unit": "eV" },
"LUMO":{ "dtype":"d", "unit": "eV" },
"polarizability":{
"neutral": { "dtype":"d", "shape":[3,3], "unit":"AA^2 s^4 kg^-1"},
"positive":{ "dtype":"d", "shape":[3,3], "unit":"AA^2 s^4 kg^-1"},
"negative":{ "dtype":"d", "shape":[3,3], "unit":"AA^2 s^4 kg^-1"}
},
"reorganizationEnergyInternal":{
"neutral": { "dtype":"d", "unit":"eV" },
"anion": { "dtype":"d", "unit":"eV" },
"cation": { "dtype":"d", "unit":"eV" }
},
"siteEnergy":{
"orbital": { "dtype":"d", "unit":"eV" },
"electrostatic":{ "dtype":"d", "unit":"eV" },
"polarization": { "dtype":"d", "unit":"eV" }
},
"transferIntegrals":{ "dtype":"d", "shape":"variable" }
},
"atoms": { "schema":"gr.uoi.kostis.atom" }
},
{
"_schema":{
"name": "gr.uoi.kostis.atom",
"version": "0.5"
},
"_datasetName": "atoms",
"generalInfo":{
"element":{ "dtype":"a2" },
"atomicNumber": {
"dtype": "l",
"key": "generalInfo.element",
"lookup": {
"H": 1,
"C": 6,
"N": 7,
"Na": 11,
"Cl": 17,
"Fe": 26
}
},
"atomicMass": {
"dtype": "f",
"key": "generalInfo.element",
"unit": "Dalton",
"lookup": {
"H": 1.0079,
"C": 12.0107,
"N": 14.0067,
"Na": 22.9897,
"Cl": 35.453,
"Fe": 55.845
}
}
},
"structure":{
"bondNeighbors": { "dtype":"l", "shape":"variable" },
"bondTypes": { "dtype":"l", "shape":"variable" },
"name": { "dtype":"a10" },
"type": { "dtype":"l" },
"position": { "dtype":"d", "shape":[3],"unit":"AA"},
"velocity": { "dtype":"d", "shape":[3],"unit":"AA/ps"},
"residueId": {"dtype":"l" }
},
"properties":{
"partialCharge":{
"neutral": { "dtype":"d", "unit":"e" },
"anion": { "dtype":"d", "unit":"e" },
"cation": { "dtype":"d", "unit":"e" }
},
"polarizability":{
"neutral": { "dtype":"d", "unit":"AA^2 s^4 kg^-1" },
"anion": { "dtype":"d", "unit":"AA^2 s^4 kg^-1" },
"cation": { "dtype":"d", "unit":"AA^2 s^4 kg^-1" }
}
},
"link":{
"other":{ "dtype":"l", "shape": "variable" }
}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment