Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print("pyxtal.molecular_crystal")
reset()
try:
import pyxtal.crystal
except Exception as e:
fail(e)
print(" molecular_crystal")
try:
from pyxtal.molecular_crystal import molecular_crystal
except Exception as e:
fail(e)
if passed():
try:
c = molecular_crystal(1, ["H2O"], [1], 10.0)
if c.valid is True:
pass
else:
fail()
except Exception as e:
fail(e)
check()
print(" molecular_crystal_2D")
try:
from pyxtal.molecular_crystal import molecular_crystal_2D
except Exception as e:
fail(e)
if passed():
from pyxtal.molecular_crystal import molecular_crystal
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
slow = []
failed = []
print(" Spacegroup # |Generated (SPG)|Generated (PMG)| Time Elapsed")
skip = (
[]
) # [24, 183, 202, 203, 209, 210, 216, 219, 225, 226, 227, 228, 229, 230] #slow
for sg in range(1, 231):
if sg not in skip:
multiplicity = len(get_wyckoffs(sg)[0]) / cellsize(
sg
) # multiplicity of the general position
start = time()
rand_crystal = molecular_crystal(sg, ["H2O"], [multiplicity], 2.5)
end = time()
timespent = np.around((end - start), decimals=2)
t = str(timespent)
if len(t) == 3:
t += "0"
t += " s"
if timespent >= 1.0:
t += " ~"
if timespent >= 3.0:
t += "~"
if timespent >= 10.0:
t += "~"
if timespent >= 60.0:
t += "~"
slow.append(sg)
if rand_crystal.valid:
from time import time
from pymatgen.io.cif import CifWriter
import os
mols = ["CH4", "H2O", "NH3", "urea", "benzene", "roy", "aspirin", "pentacene", "C60"]
filename = "out.cif"
if os.path.isfile(filename):
os.remove(filename)
for mol in mols:
for i in range(10):
run = True
while run:
sg = randint(4, 191)
start = time()
rand_crystal = molecular_crystal(sg, [mol], [4], 1.0)
if rand_crystal.valid:
run = False
print(
"Mol:{0:10s} SG:{1:3d} Time:{2:4.2f} seconds, N_attempts:{3:4d} Vol: {4:6.2f}".format(
mol,
sg,
time() - start,
rand_crystal.numattempts,
rand_crystal.volume,
)
)
content = str(CifWriter(rand_crystal.struct, symprec=0.1))
with open(filename, "a+") as f:
f.writelines(content)
system.append(mol)
for x in number.split(","):
numMols.append(int(x))
else:
system = [molecule]
numMols = [int(number)]
orientations = None
if not os.path.exists(outdir):
os.mkdir(outdir)
for i in range(attempts):
start = time()
numMols0 = np.array(numMols)
if dimension == 3:
rand_crystal = molecular_crystal(
sg,
system,
numMols0,
factor,
check_atomic_distances=checkatoms,
allow_inversion=allowinversion,
)
elif dimension == 2:
rand_crystal = molecular_crystal_2D(
sg,
system,
numMols0,
thickness,
factor,
allow_inversion=allowinversion,
check_atomic_distances=checkatoms,
self.spg_struct = (lattice, self.frac_coords, final_number)
self.valid = True
return
else:
printx("Failed final distance check.", priority=3)
printx("Couldn't generate crystal after max attempts.", priority=1)
if degrees == 0:
printx("Note: Wyckoff positions have no degrees of freedom.", priority=2)
self.struct = None
self.valid = False
return
class molecular_crystal_2D(molecular_crystal):
"""
A 2d counterpart to molecular_crystal. Given a layer group, list of
molecule objects, molecular stoichiometry, and
a volume factor, generates a molecular crystal consistent with the given
constraints. This crystal is stored as a pymatgen struct via self.struct
Args:
group: the layer group number between 1 and 80. NOT equal to the
international space group number, which is between 1 and 230
OR, a pyxtal.symmetry.Group object
molecules: a list of pymatgen.core.structure.Molecule objects for
each type of molecule. Alternatively, you may supply a file path,
or give a string to convert (in which case fmt must be defined)
numMols: A list of the number of each type of molecule within the
primitive cell (NOT the conventioal cell)
thickness: the thickness, in Angstroms, of the unit cell in the 3rd
# para = Lattice.from_para(4.45, 7.70, 7.28, 90, 90, 90)
# Here we generate many random structures and optimize them
data = { # "ID":[],
"Sym": [],
"Eng": [],
"abc": [],
"Volume": [],
}
for i in range(100):
while True:
sg = randint(16, 191)
crystal = molecular_crystal(sg, ["H2O"], [4], 1.0) # , lattice=para)
if crystal.valid:
struc = Atoms(
crystal.spg_struct[2],
cell=crystal.spg_struct[0],
scaled_positions=crystal.spg_struct[1],
)
break
# struc = struc.repeat((2,2,2))
lammps = LAMMPSlib(lmp=lmp, lmpcmds=parameters, mol=True)
struc.set_calculator(lammps)
box = mushybox(struc)
dyn = FIRE(box)
dyn.run(fmax=0.01, steps=200)
dyn = BFGS(box)
dyn.run(fmax=0.01, steps=200)
Eng = struc.get_potential_energy() * 96 / len(struc) * 3
def _mp_mc(self, spg, q):
'''multiprocess part'''
# ---------- temporarily stdout --> devnull
with redirect_stdout(open(os.devnull, 'w')):
tmp_crystal = molecular_crystal(spg, self.mol_file,
self.nmol, self.vol_factor)
# ---------- queue
q.put(tmp_crystal.struct)
q.put(tmp_crystal.valid)
self.PBC = [1, 1, 0]
self.init_common(
molecules,
numMols,
volume_factor,
select_high,
allow_inversion,
orientations,
check_atomic_distances,
group,
lattice,
tm,
)
class molecular_crystal_1D(molecular_crystal):
"""
A 1d counterpart to molecular_crystal. Given a Rod group, list of
molecule objects, molecular stoichiometry, volume factor, and area,
generates a molecular crystal consistent with the given constraints.
The crystal is stored as a pymatgen struct via self.struct
Args:
group: the Rod group number between 1 and 80. NOT equal to the
international space group number, which is between 1 and 230
OR, a pyxtal.symmetry.Group object
molecules: a list of pymatgen.core.structure.Molecule objects for
each type of molecule. Alternatively, you may supply a file path,
or give a string to convert (in which case fmt must be defined)
numMols: A list of the number of each type of molecule within the
primitive cell (NOT the conventioal cell)
area: cross-sectional area of the unit cell in Angstroms squared. A
if mc.valid:
return Mstruct(mc.mol_generators, mc.lattice, mc.group)
else:
return None
if __name__ == "__main__":
from pyxtal.crystal import random_crystal
c = random_crystal(225, ["C"], [4], 1)
x = Xstruct.from_random_crystal(c)
print(x.group)
from pyxtal.molecular_crystal import molecular_crystal
m = molecular_crystal(20, ["H2O"], [8], 1)
x = Mstruct.from_molecular_crystal(m)
print(x.group)