def write_outputdata(self, uri, driver='txt', options=None): """Writes VASP data to `uri` using `driver` Valid drivers are all drivers supported by SOFT in addition to "txt". Additional options to the driver can be provided via `options`. The "txt" driver is decoupled from SOFT and uses `uri` as the output base name. If `options` is "short", less details are written. """ if driver == 'txt': if options is None: self.write_outputdata_txt(uri) elif options == 'short': self.write_outputdata_txt_short(uri) else: raise ValueError( 'Only option supported by the txt driver is "short", ' 'got %r' % (options, )) else: with softpy.Storage(driver, uri, options) as s: s.save(self)
# Create new Structure entity based on the metadata Structure = softpy.entity(metadata) # Create a empty Structure instance s = Structure() s.structure = 'water' s.symbols = ['H', 'H', 'O'] s.spacegroup = 1 s.total_energy = 446.32 s.masses = np.array([1., 1., 16.]) s.soft_internal_check_dimension_sizes() # Write the structure to file with softpy.Storage('hdf5', 'softpy-test-factory.h5') as storage: storage.save(s) # Create new Structure instance initiated from file s2 = Structure(uuid=s.soft_get_id(), driver='hdf5', uri='softpy-test-factory.h5') # Check that the entities are equals for name in 'id', 'meta_name', 'meta_version', 'meta_namespace': f = getattr(softpy, 'get_' + name) assert f(s) == f(s2) assert s.soft_get_property_names() == s2.soft_get_property_names()
# Test to add another collection to coll subcoll = softpy.Collection() anna = Person(name='Anna', age=78, skills=['piano']) subcoll.add('Anna', anna) coll.add('relatives', subcoll) # Test to add metadata to coll coll.add('Person', Person) # Save the collection to hdf5 # Note that we must set append=yes to also store the instances. This will # cause trouble with existing names the second time we run the test, so # we remove the h5 file first. if os.path.exists('softpy-test-collection.h5'): os.remove('softpy-test-collection.h5') with softpy.Storage('hdf5', 'softpy-test-collection.h5', 'append=yes') as s: coll.save(s) # Create a metadata database with all metadata defined in json files # in the current directory - needed in order to instanciate entities # and metadata read from the stored hdf5 file #softpy.register_metadb(softpy.JSONDirMetaDB(thisdir)) softpy.register_jsondir(thisdir) # Create a new collection, loaded from hdf5 coll2 = softpy.Collection(uuid=coll.uuid, driver='hdf5', uri='softpy-test-collection.h5') assert coll2.uuid == coll.uuid
with open(os.path.join(thisdir, 'metadata', 'simphony_metadata.yml')) as f: cuds = yaml.load(f.read()) # Crate CUDS entities and relations entities, relations = generate_cuds_entities(cuds, cuba) # Write CUDS entities and relations write_cuds_entities(os.path.join(thisdir, 'metadata', 'cuds_entities')) import softpy # Create a collection with all CUDS entities and relations c = get_cuds_collection() with softpy.Storage( driver='hdf5', uri='softcuds.h5', #options='append=yes', ) as s: c.save(s) # Create a collection of CUDS instances ci = get_cuds_instance_collection( cuds_collection=c, name='CRYSTAL_STRUCTURE', dimensions={'CRYSTAL_STRUCTURE.ATOM_SITES.ATOM_SITE': [2]}, initial_values={}) print('labels:\n', ci.get_labels()) crystal_structure = ci.get_instance('CRYSTAL_STRUCTURE') #with softpy.Storage(driver='hdf5',
assert softpy.entity_get_meta_name(e) == 'MyStructure' assert softpy.entity_get_meta_version(e) == '0.1.1' assert softpy.entity_get_meta_namespace(e) == 'http://sintef.no/meta/soft' assert softpy.entity_get_dimensions(e) == ['I', 'J'] assert softpy.entity_get_dimension_size(e, 'I') == 3 assert softpy.entity_get_dimension_size(e, 'J') == 4 assert softpy.entity_get_id(e) == e.id assert e.name == 'MyStructure' assert e.version == '0.1.1' assert e.namespace == 'http://sintef.no/meta/soft' assert e.dimensions == ['I', 'J'] assert e.dimension_sizes == [3, 4] s = softpy.Storage('hdf5', 'softpy-test-entity.h5') s.save(e) s.load(e) d = e.user_data assert d.name == 'NaCl' assert d.spgr == 225 assert d.latt == 5.64 assert np.allclose( d.posi, np.array([(0.0, 0.0, 0.0), (0.5, 0.5, 0.5)]).flatten()) del e class Person(object): def __init__(self, name='', age=0.0, distances=(), uuid=None):
def _unwrap(s, constants=None, functions=()): """Returns unwrapped object from its string representation `s`.""" try: return softpy.arithmetic_eval(s, constants, functions) except softpy.ArithmeticError: try: return ast.literal_eval(s) except ValueError: warnings.warn('converted to string: %r' % s) return s a = 4.6 c = 2.95 rutile = crystal(['Ti', 'O'], basis=[(0, 0, 0), (0.3, 0.3, 0.0)], spacegroup=136, cellpar=[a, a, c, 90, 90, 90]) rutile.set_constraint(ase.constraints.FixAtoms(indices=(1, 3))) r = SoftAtoms(rutile) s = softpy.Storage(driver='hdf5', uri='atoms.h5') s.save(r) s.close() rr = SoftAtoms(driver='hdf5', uri='atoms.h5', uuid=r.soft_get_id()) rr.write('rutile.cif')
import softpy Initial = softpy.entity(open('initial.json')) i = Initial(dimension_sizes=[50,50]) i.dt = 2.0e-3 i.startt = 0.0 i.endt = 2.0 i.nu = 1.0e-2 i.sample_interval = 1.0 s = softpy.Storage(driver='hdf5', uri='init.h5') s.save(i) s.close() print(softpy.get_id(i));