Giter Club home page Giter Club logo

Comments (4)

eudoxos avatar eudoxos commented on May 28, 2024

I added the simplified options as default branches to the code; feel free to delete the low-level variants if you don't see them as necessary to keep

def solveStep(self, tstep, stageID=0, runInBackground=False):
# (1) read source grain state, into new state, then (2) replace a molecule with dopant (different molecule)
print(self.inputGrainState)
#
# (1) copy old state into a new one
#
if 1:
# transfer via file copy of the underlying storage, very fast (local access only!)
fd,h5=tempfile.mkstemp(prefix='mupif-example11-',suffix='.h5')
shutil.copy(self.inputGrainState.h5path,h5)
self.outputGrainState=mp.HeavyDataHandle(h5path=h5,id=mp.dataid.MiscID.ID_GrainState)
inGrains = self.inputGrainState.getData(mode='readonly')
outGrains=self.outputGrainState.getData(mode='readwrite')
elif 1:
# transfer via inject (serializes into RAM, network-transparent)
inGrains = self.inputGrainState.getData(mode='readonly')
self.outputGrainState=mp.HeavyDataHandle(id=mp.dataid.MiscID.ID_GrainState)
log.warning(f'Created temporary {self.outputGrainState.h5path}')
outGrains = self.outputGrainState.getData(mode='create',schemaName='org.mupif.sample.grain',schemasJson=mp.heavydata.sampleSchemas_json)
outGrains.inject(inGrains)
else:
# transfer via explicit loop over data
inGrains = self.inputGrainState.getData(mode='readonly')
self.outputGrainState=mp.HeavyDataHandle(id=mp.dataid.MiscID.ID_GrainState)
log.warning(f'Created temporary {self.outputGrainState.h5path}')
outGrains = self.outputGrainState.getData(mode='create',schemaName='org.mupif.sample.grain',schemasJson=mp.heavydata.sampleSchemas_json)
t0=time.time()
atomCounter=0
outGrains.resize(size=len(inGrains))
#todo: copy inGrains to outGrains (check for more elegant way)
for igNum,ig in enumerate(inGrains):
outGrains[igNum].getMolecules().resize(size=len(ig.getMolecules()))
for imNum, im in enumerate(ig.getMolecules()):
om = outGrains[igNum].getMolecules()[imNum]
om.getIdentity().setMolecularWeight(im.getIdentity().getMolecularWeight())
om.getAtoms().resize(size=len(im.getAtoms()))
for iaNum, ia in enumerate(im.getAtoms()):
oa = om.getAtoms()[iaNum]
oa.getIdentity().setElement(ia.getIdentity().getElement())
oa.getProperties().getTopology().setPosition(ia.getProperties().getTopology().getPosition())
oa.getProperties().getTopology().setVelocity(ia.getProperties().getTopology().getVelocity())
oa.getProperties().getTopology().setStructure(ia.getProperties().getTopology().getStructure())
atomCounter+=1
t1=time.time()
print(f'{atomCounter} atoms created in {t1-t0:g} sec ({atomCounter/(t1-t0):g}/sec).')
#
# replace one molecule in outGrains with a different molecule
#
if 1:
# use inject to replace outGrains/1/molecules/2 with inGrains/0/molecules/3
outGrains[1].getMolecules()[2].inject(inGrains[0].getMolecules()[3])
else:
# explicit loop over data, random molecule choice
# select random grain and molecule
t0=time.time()
atomCounter = 0
rgNum = random.randint(0,len(outGrains)-1)
rmNum = random.randint(0,len(outGrains[rgNum].getMolecules())-1)
repMol = outGrains[rgNum].getMolecules()[rmNum]
# replace this molecule
repMol.getIdentity().setMolecularWeight(random.randint(1,10)*u.yg)
if (1):
#print(repMol.getAtoms()[0]) # call _T_assertDataset()
#print (repMol.getAtoms())
#print("Deleting "+repMol.getAtoms().ctx.h5group.name+'/'+repMol.getAtoms()[0].datasetName)
##todo: make a method to solve this
#del self.outputGrainState._h5obj[repMol.getAtoms().ctx.h5group.name+'/'+repMol.getAtoms()[0].datasetName]
repMol.getAtoms().resize(size=random.randint(30,60),reset=True)
#print (repMol.getAtoms()[0])
for a in repMol.getAtoms():
a.getIdentity().setElement(random.choice(['H','N','Cl','Na','Fe']))
a.getProperties().getTopology().setPosition((1,2,3)*u.nm)
a.getProperties().getTopology().setVelocity((24,5,77)*u.m/u.s)
struct=np.array([random.randint(1,20) for i in range(random.randint(5,20))],dtype='l')
a.getProperties().getTopology().setStructure(struct)
atomCounter+=1
t1=time.time()
print(f'{atomCounter} atoms replaced in {t1-t0:g} sec ({atomCounter/(t1-t0):g}/sec).')

from mupif.

bpatzak avatar bpatzak commented on May 28, 2024

Hi, I think the first option (copy old state into a new one) should be avoided, as the use needs knowledge about internal representation (needs to copy file). The code at this level should abstract from this and rely solely on methods provided by a datatype.
The second option is ok. But what is the difference between inject and set method?

from mupif.

eudoxos avatar eudoxos commented on May 28, 2024

Hi, I think the first option (copy old state into a new one) should be avoided, as the use needs knowledge about internal representation (needs to copy file).

The file copy could be hidden by a method or perhaps by an additional data access mode, e.g. getData(mode='clone',...). It is by far the most efficient way of cloning the data.

But what is the difference between inject and set method?

inject takes another context as parameter (the data pass through serialization/deserizalization in RAM) and replaces the whole subtree. set* methods are only defined for leaf nodes (such as setMolecularWeight(…)) , not for a whole hierarchy of data (there is no setMolecules(…), for instance).

from mupif.

bpatzak avatar bpatzak commented on May 28, 2024

If abstracted by method (like copy()) then the first approach is ok. My objections were mainly to the direct file copying in workflow.
Related to the second, from a perspective of the user, who has no knowledge about the internals, the meaning of inject and set methods is quite close. Can we merge them together? So there will be jut set method defined for leaf nodes as well as for sub trees.

from mupif.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.