def setUp(self): lattice = Lattice([[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]]) self.structure = Structure(lattice, ["Si", "Si"], [[0, 0, 0], [0.75, 0.5, 0.75]]) self.default_dss = DeformedStructureSet(self.structure)
def test_init(self): with self.assertRaises(ValueError): DeformedStructureSet(self.structure, num_norm=5) with self.assertRaises(ValueError): DeformedStructureSet(self.structure, num_shear=5) self.assertEqual(self.structure, self.default_dss.undeformed_structure) # Test symmetry dss_symm = DeformedStructureSet(self.structure, symmetry=True) # Should be 4 strains for normal, 2 for shear (since +/- shear # are symmetrically equivalent) self.assertEqual(len(dss_symm), 6)
class DeformedStructureSetTest(PymatgenTest): def setUp(self): self.structure = self.get_structure("Sn") self.default_dss = DeformedStructureSet(self.structure) def test_init(self): with self.assertRaises(ValueError): DeformedStructureSet(self.structure, num_norm=5) with self.assertRaises(ValueError): DeformedStructureSet(self.structure, num_shear=5) self.assertEqual(self.structure, self.default_dss.undeformed_structure) # Test symmetry dss_symm = DeformedStructureSet(self.structure, symmetry=True) # Should be 4 strains for normal, 2 for shear (since +/- shear # are symmetrically equivalent) self.assertEqual(len(dss_symm), 6) def test_as_strain_dict(self): strain_dict = self.default_dss.as_strain_dict() for i, def_struct in enumerate(self.default_dss): test_strain = IndependentStrain(self.default_dss.deformations[i]) strain_keys = [ strain for strain in list(strain_dict.keys()) if (strain == test_strain).all() ] self.assertEqual(len(strain_keys), 1) self.assertEqual(self.default_dss.def_structs[i], strain_dict[strain_keys[0]])
class DeformedStructureSetTest(PymatgenTest): def setUp(self): self.structure = self.get_structure("Sn") self.default_dss = DeformedStructureSet(self.structure) def test_init(self): with self.assertRaises(ValueError): DeformedStructureSet(self.structure, num_norm=5) with self.assertRaises(ValueError): DeformedStructureSet(self.structure, num_shear=5) self.assertEqual(self.structure, self.default_dss.undeformed_structure) # Test symmetry dss_symm = DeformedStructureSet(self.structure, symmetry=True) # Should be 4 strains for normal, 2 for shear (since +/- shear # are symmetrically equivalent) self.assertEqual(len(dss_symm), 6) def test_as_strain_dict(self): strain_dict = self.default_dss.as_strain_dict() for i, def_struct in enumerate(self.default_dss): test_strain = IndependentStrain(self.default_dss.deformations[i]) strain_keys = [strain for strain in list(strain_dict.keys()) if (strain == test_strain).all()] self.assertEqual(len(strain_keys), 1) self.assertEqual(self.default_dss.def_structs[i], strain_dict[strain_keys[0]])
class DeformedStructureSetTest(PymatgenTest): def setUp(self): lattice = Lattice([[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]]) self.structure = Structure(lattice, ["Si", "Si"], [[0, 0, 0], [0.75, 0.5, 0.75]]) self.default_dss = DeformedStructureSet(self.structure) def test_init(self): with self.assertRaises(ValueError): DeformedStructureSet(self.structure, num_norm=5) DeformedStructureSet(self.structure, num_shear=5) self.assertEqual(self.structure, self.default_dss.undeformed_structure) def test_as_strain_dict(self): strain_dict = self.default_dss.as_strain_dict() for i, def_struct in enumerate(self.default_dss): test_strain = IndependentStrain(self.default_dss.deformations[i]) strain_keys = [ strain for strain in list(strain_dict.keys()) if (strain == test_strain).all() ] self.assertEqual(len(strain_keys), 1) self.assertEqual(self.default_dss.def_structs[i], strain_dict[strain_keys[0]])
def test_init(self): self.assertEqual(self.structure, self.default_dss.undeformed_structure) # Test symmetry dss_symm = DeformedStructureSet(self.structure, symmetry=True) # Should be 4 strains for normal, 2 for shear (since +/- shear # are symmetrically equivalent) self.assertEqual(len(dss_symm), 6)
def run_task(self, fw_spec): # Read structure from previous relaxation relaxed_struct = fw_spec['output']['crystal'] # Generate deformed structures d_struct_set = DeformedStructureSet(relaxed_struct, ns=0.06) wf=[] for i, d_struct in enumerate(d_struct_set.def_structs): fws=[] connections={} f = Composition(d_struct.formula).alphabetical_formula snl = StructureNL(d_struct, 'Joseph Montoya <*****@*****.**>', projects=["Elasticity"]) tasks = [AddSNLTask()] snl_priority = fw_spec.get('priority', 1) spec = {'task_type': 'Add Deformed Struct to SNL database', 'snl': snl.as_dict(), '_queueadapter': QA_DB, '_priority': snl_priority} if 'snlgroup_id' in fw_spec and isinstance(snl, MPStructureNL): spec['force_mpsnl'] = snl.as_dict() spec['force_snlgroup_id'] = fw_spec['snlgroup_id'] del spec['snl'] fws.append(Firework(tasks, spec, name=get_slug(f + '--' + spec['task_type']), fw_id=-1000+i*10)) connections[-1000+i*10] = [-999+i*10] spec = snl_to_wf._snl_to_spec(snl, parameters={'exact_structure':True}) spec = update_spec_force_convergence(spec) spec['deformation_matrix'] = d_struct_set.deformations[i].tolist() spec['original_task_id'] = fw_spec["task_id"] spec['_priority'] = fw_spec['_priority']*2 #Turn off dupefinder for deformed structure del spec['_dupefinder'] spec['task_type'] = "Optimize deformed structure" fws.append(Firework([VaspWriterTask(), SetupElastConstTask(), get_custodian_task(spec)], spec, name=get_slug(f + '--' + spec['task_type']), fw_id=-999+i*10)) priority = fw_spec['_priority']*3 spec = {'task_type': 'VASP db insertion', '_priority': priority, '_allow_fizzled_parents': True, '_queueadapter': QA_DB, 'elastic_constant':"deformed_structure", 'clean_task_doc':True, 'deformation_matrix':d_struct_set.deformations[i].tolist(), 'original_task_id':fw_spec["task_id"]} fws.append(Firework([VaspToDBTask(), AddElasticDataToDBTask()], spec, name=get_slug(f + '--' + spec['task_type']), fw_id=-998+i*10)) connections[-999+i*10] = [-998+i*10] wf.append(Workflow(fws, connections)) return FWAction(additions=wf)
class DeformedStructureSetTest(PymatgenTest): def setUp(self): lattice = Lattice( [[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]] ) self.structure = Structure(lattice, ["Si", "Si"], [[0, 0, 0], [0.75, 0.5, 0.75]]) self.default_dss = DeformedStructureSet(self.structure) def test_init(self): with self.assertRaises(ValueError): DeformedStructureSet(self.structure, num_norm=5) with self.assertRaises(ValueError): DeformedStructureSet(self.structure, num_shear=5) self.assertEqual(self.structure, self.default_dss.undeformed_structure) def test_as_strain_dict(self): strain_dict = self.default_dss.as_strain_dict() for i, def_struct in enumerate(self.default_dss): test_strain = IndependentStrain(self.default_dss.deformations[i]) strain_keys = [strain for strain in list(strain_dict.keys()) if (strain == test_strain).all()] self.assertEqual(len(strain_keys), 1) self.assertEqual(self.default_dss.def_structs[i], strain_dict[strain_keys[0]])
def test_process_elastic_calcs(self): test_struct = PymatgenTest.get_structure('Sn') # use cubic test struct dss = DeformedStructureSet(test_struct) # Construct test task set opt_task = { "output": { "structure": test_struct.as_dict() }, "input": { "structure": test_struct.as_dict() } } defo_tasks = [] for n, (struct, defo) in enumerate(zip(dss, dss.deformations)): strain = defo.green_lagrange_strain defo_task = { "output": { "structure": struct.as_dict(), "stress": (strain * 5).tolist() }, "input": None, "task_id": n, "completed_at": datetime.utcnow() } defo_task.update({ "transmuter": { "transformation_params": [{ "deformation": defo }] } }) defo_tasks.append(defo_task) defo_tasks.pop(0) explicit, derived = process_elastic_calcs(opt_task, defo_tasks) self.assertEqual(len(explicit), 23) self.assertEqual(len(derived), 1)
def runSimulation(self): if self.args["pymatgen"] == True: starting_struct = singleFromFile(self.args) info = runCalc("all_relax", ["structures","stresses"], [starting_struct], self.args) relaxed_struct = info["structures"][0] relaxed_stress = info["stresses"][0] strains = [] deformed_set = DeformedStructureSet(relaxed_struct, symmetry=True) deformed_structs = deformed_set.deformed_structures symmetry_dict = deformed_set.sym_dict deformations = deformed_set.deformations stresses = runCalc("atom_relax", ["stresses"], deformed_structs, self.args)["stresses"] for i in range(len(deformations)): strains.append(Strain.from_deformation(deformations[i])) for i in range(len(deformations)): for symm in symmetry_dict[deformations[i]]: strains.append(strains[i].transform(symm)) stresses.append(stresses[i].transform(symm)) self.elastic_tensor = ElasticTensor.from_independent_strains(strains,stresses,eq_stress=relaxed_stress,vasp=True).zeroed().voigt
def make_vasp(jdata, conf_dir): default_norm_def = 2e-3 default_shear_def = 5e-3 norm_def = jdata.get('norm_deform', default_norm_def) shear_def = jdata.get('shear_deform', default_shear_def) conf_path = os.path.abspath(conf_dir) conf_poscar = os.path.join(conf_path, 'POSCAR') if 'relax_incar' in jdata.keys(): vasp_str = 'vasp-relax_incar' else: kspacing = jdata['vasp_params']['kspacing'] vasp_str = 'vasp-k%.2f' % kspacing # get equi poscar equi_path = re.sub('confs', global_equi_name, conf_path) equi_path = os.path.join(equi_path, vasp_str) equi_contcar = os.path.join(equi_path, 'CONTCAR') task_path = re.sub('confs', global_task_name, conf_path) task_path = os.path.join(task_path, vasp_str) os.makedirs(task_path, exist_ok=True) cwd = os.getcwd() os.chdir(task_path) if os.path.isfile('POSCAR'): os.remove('POSCAR') os.symlink(os.path.relpath(equi_contcar), 'POSCAR') os.chdir(cwd) task_poscar = os.path.join(task_path, 'POSCAR') # stress equi_outcar = os.path.join(equi_path, 'OUTCAR') stress = vasp.get_stress(equi_outcar) np.savetxt(os.path.join(task_path, 'equi.stress.out'), stress) # gen strcture ss = Structure.from_file(task_poscar) # gen defomations norm_strains = [-norm_def, -0.5 * norm_def, 0.5 * norm_def, norm_def] shear_strains = [-shear_def, -0.5 * shear_def, 0.5 * shear_def, shear_def] dfm_ss = DeformedStructureSet(ss, symmetry=False, norm_strains=norm_strains, shear_strains=shear_strains) n_dfm = len(dfm_ss) # gen incar if 'relax_incar' in jdata.keys(): relax_incar_path = jdata['relax_incar'] assert (os.path.exists(relax_incar_path)) relax_incar_path = os.path.abspath(relax_incar_path) incar = incar_upper(Incar.from_file(relax_incar_path)) if incar.get('ISIF') != 2: dlog.info("%s:%s setting ISIF to 2" % (__file__, make_vasp.__name__)) incar['ISIF'] = 2 fc = incar.get_string() kspacing = incar['KSPACING'] kgamma = incar['KGAMMA'] else: fp_params = jdata['vasp_params'] ecut = fp_params['ecut'] ediff = fp_params['ediff'] npar = fp_params['npar'] kpar = fp_params['kpar'] kspacing = fp_params['kspacing'] kgamma = fp_params['kgamma'] fc = vasp.make_vasp_relax_incar(ecut, ediff, True, False, False, npar=npar, kpar=kpar, kspacing=kspacing, kgamma=kgamma) with open(os.path.join(task_path, 'INCAR'), 'w') as fp: fp.write(fc) # gen potcar with open(task_poscar, 'r') as fp: lines = fp.read().split('\n') ele_list = lines[5].split() potcar_map = jdata['potcar_map'] potcar_list = [] for ii in ele_list: assert os.path.exists(os.path.abspath( potcar_map[ii])), "No POTCAR in the potcar_map of %s" % (ii) potcar_list.append(os.path.abspath(potcar_map[ii])) with open(os.path.join(task_path, 'POTCAR'), 'w') as outfile: for fname in potcar_list: with open(fname) as infile: outfile.write(infile.read()) # gen kpoints fc = vasp.make_kspacing_kpoints(task_poscar, kspacing, kgamma) with open(os.path.join(task_path, 'KPOINTS'), 'w') as fp: fp.write(fc) # gen tasks cwd = os.getcwd() for ii in range(n_dfm): # make dir dfm_path = os.path.join(task_path, 'dfm-%03d' % ii) os.makedirs(dfm_path, exist_ok=True) os.chdir(dfm_path) for jj in ['POSCAR', 'POTCAR', 'INCAR', 'KPOINTS']: if os.path.isfile(jj): os.remove(jj) # make conf dfm_ss.deformed_structures[ii].to('POSCAR', 'POSCAR') # record strain strain = Strain.from_deformation(dfm_ss.deformations[ii]) np.savetxt('strain.out', strain) # link incar, potcar, kpoints os.symlink(os.path.relpath(os.path.join(task_path, 'INCAR')), 'INCAR') os.symlink(os.path.relpath(os.path.join(task_path, 'POTCAR')), 'POTCAR') os.symlink(os.path.relpath(os.path.join(task_path, 'KPOINTS')), 'KPOINTS') #copy cvasp if ('cvasp' in jdata) and (jdata['cvasp'] == True): shutil.copyfile(cvasp_file, os.path.join(dfm_path, 'cvasp.py')) os.chdir(cwd)
def setUp(self): self.structure = self.get_structure("Sn") self.default_dss = DeformedStructureSet(self.structure)
def make_confs(self, path_to_work, path_to_equi, refine=False): path_to_work = os.path.abspath(path_to_work) if os.path.exists(path_to_work): dlog.warning('%s already exists' % path_to_work) else: os.makedirs(path_to_work) path_to_equi = os.path.abspath(path_to_equi) if 'start_confs_path' in self.parameter and os.path.exists( self.parameter['start_confs_path']): init_path_list = glob.glob( os.path.join(self.parameter['start_confs_path'], '*')) struct_init_name_list = [] for ii in init_path_list: struct_init_name_list.append(ii.split('/')[-1]) struct_output_name = path_to_work.split('/')[-2] assert struct_output_name in struct_init_name_list path_to_equi = os.path.abspath( os.path.join(self.parameter['start_confs_path'], struct_output_name, 'relaxation', 'relax_task')) task_list = [] cwd = os.getcwd() equi_contcar = os.path.join(path_to_equi, 'CONTCAR') os.chdir(path_to_work) if os.path.isfile('POSCAR'): os.remove('POSCAR') if os.path.islink('POSCAR'): os.remove('POSCAR') os.symlink(os.path.relpath(equi_contcar), 'POSCAR') # task_poscar = os.path.join(output, 'POSCAR') # stress, deal with unsupported stress in dpdata #with open(os.path.join(path_to_equi, 'result.json')) as fin: # equi_result = json.load(fin) #equi_stress = np.array(equi_result['stress']['data'])[-1] equi_result = loadfn(os.path.join(path_to_equi, 'result.json')) equi_stress = equi_result['stress'][-1] dumpfn(equi_stress, 'equi.stress.json', indent=4) os.chdir(cwd) if refine: print('elastic refine starts') task_list = make_refine(self.parameter['init_from_suffix'], self.parameter['output_suffix'], path_to_work) # record strain # df = Strain.from_deformation(dfm_ss.deformations[idid]) # dumpfn(df.as_dict(), 'strain.json', indent=4) init_from_path = re.sub(self.parameter['output_suffix'][::-1], self.parameter['init_from_suffix'][::-1], path_to_work[::-1], count=1)[::-1] task_list_basename = list(map(os.path.basename, task_list)) for ii in task_list_basename: init_from_task = os.path.join(init_from_path, ii) output_task = os.path.join(path_to_work, ii) os.chdir(output_task) if os.path.isfile('strain.json'): os.remove('strain.json') copyfile(os.path.join(init_from_task, 'strain.json'), 'strain.json') #os.symlink(os.path.relpath( # os.path.join((re.sub(self.parameter['output_suffix'], self.parameter['init_from_suffix'], ii)), # 'strain.json')), # 'strain.json') os.chdir(cwd) else: norm_def = self.norm_deform shear_def = self.shear_deform norm_strains = [ -norm_def, -0.5 * norm_def, 0.5 * norm_def, norm_def ] shear_strains = [ -shear_def, -0.5 * shear_def, 0.5 * shear_def, shear_def ] if not os.path.exists(equi_contcar): raise RuntimeError("please do relaxation first") ss = Structure.from_file(equi_contcar) dfm_ss = DeformedStructureSet(ss, symmetry=False, norm_strains=norm_strains, shear_strains=shear_strains) n_dfm = len(dfm_ss) print('gen with norm ' + str(norm_strains)) print('gen with shear ' + str(shear_strains)) for ii in range(n_dfm): output_task = os.path.join(path_to_work, 'task.%06d' % ii) os.makedirs(output_task, exist_ok=True) os.chdir(output_task) for jj in [ 'INCAR', 'POTCAR', 'POSCAR', 'conf.lmp', 'in.lammps' ]: if os.path.exists(jj): os.remove(jj) task_list.append(output_task) dfm_ss.deformed_structures[ii].to('POSCAR', 'POSCAR') # record strain df = Strain.from_deformation(dfm_ss.deformations[ii]) dumpfn(df.as_dict(), 'strain.json', indent=4) os.chdir(cwd) return task_list
def test_init(self): with self.assertRaises(ValueError): DeformedStructureSet(self.structure, num_norm=5) DeformedStructureSet(self.structure, num_shear=5) self.assertEqual(self.structure, self.default_dss.undeformed_structure)
def make_deepmd_lammps(jdata, conf_dir) : deepmd_model_dir = jdata['deepmd_model_dir'] deepmd_type_map = jdata['deepmd_type_map'] ntypes = len(deepmd_type_map) deepmd_model_dir = os.path.abspath(deepmd_model_dir) deepmd_models = glob.glob(os.path.join(deepmd_model_dir, '*pb')) deepmd_models_name = [os.path.basename(ii) for ii in deepmd_models] norm_def = jdata['norm_deform'] shear_def = jdata['shear_deform'] conf_path = os.path.abspath(conf_dir) conf_poscar = os.path.join(conf_path, 'POSCAR') # get equi poscar equi_path = re.sub('confs', global_equi_name, conf_path) equi_path = os.path.join(equi_path, 'deepmd') equi_dump = os.path.join(equi_path, 'dump.relax') task_path = re.sub('confs', global_task_name, conf_path) task_path = os.path.join(task_path, 'deepmd') os.makedirs(task_path, exist_ok=True) task_poscar = os.path.join(task_path, 'POSCAR') lammps.poscar_from_last_dump(equi_dump, task_poscar, deepmd_type_map) # get equi stress equi_log = os.path.join(equi_path, 'log.lammps') stress = lammps.get_stress(equi_log) np.savetxt(os.path.join(task_path, 'equi.stress.out'), stress) # gen strcture # ss = Structure.from_file(conf_poscar) # print(ss) # ss = ss.from_file(task_poscar) # print(ss) ss = Structure.from_file(task_poscar) # gen defomations norm_strains = [-norm_def, -0.5*norm_def, 0.5*norm_def, norm_def] shear_strains = [-shear_def, -0.5*shear_def, 0.5*shear_def, shear_def] print('gen with norm '+str(norm_strains)) print('gen with shear '+str(shear_strains)) dfm_ss = DeformedStructureSet(ss, symmetry = False, norm_strains = norm_strains, shear_strains = shear_strains) n_dfm = len(dfm_ss) # gen tasks cwd = os.getcwd() # make lammps.in fc = lammps.make_lammps_elastic('conf.lmp', ntypes, lammps.inter_deepmd, deepmd_models_name) f_lammps_in = os.path.join(task_path, 'lammps.in') with open(f_lammps_in, 'w') as fp : fp.write(fc) cwd = os.getcwd() for ii in range(n_dfm) : # make dir dfm_path = os.path.join(task_path, 'dfm-%03d' % ii) os.makedirs(dfm_path, exist_ok=True) os.chdir(dfm_path) for jj in ['conf.lmp', 'lammps.in'] + deepmd_models_name : if os.path.isfile(jj): os.remove(jj) # make conf dfm_ss.deformed_structures[ii].to('POSCAR', 'POSCAR') lammps.cvt_lammps_conf('POSCAR', 'conf.lmp') ptypes = vasp.get_poscar_types('POSCAR') lammps.apply_type_map('conf.lmp', deepmd_type_map, ptypes) # record strain strain = Strain.from_deformation(dfm_ss.deformations[ii]) np.savetxt('strain.out', strain) # link lammps.in os.symlink(os.path.relpath(f_lammps_in), 'lammps.in') # link models for (ii,jj) in zip(deepmd_models, deepmd_models_name) : os.symlink(os.path.relpath(ii), jj) cwd = os.getcwd()
def make_vasp(jdata, conf_dir, norm_def = 2e-3, shear_def = 5e-3) : norm_def = jdata['norm_deform'] shear_def = jdata['shear_deform'] conf_path = os.path.abspath(conf_dir) conf_poscar = os.path.join(conf_path, 'POSCAR') # get equi poscar if 'relax_incar' in jdata.keys(): vasp_str='vasp-relax_incar' else: kspacing = jdata['vasp_params']['kspacing'] vasp_str='vasp-k%.2f' % kspacing equi_path = re.sub('confs', global_equi_name, conf_path) equi_path = os.path.join(equi_path, vasp_str) equi_contcar = os.path.join(equi_path, 'CONTCAR') task_path = re.sub('confs', global_task_name, conf_path) task_path = os.path.join(task_path, vasp_str) os.makedirs(task_path, exist_ok=True) cwd = os.getcwd() os.chdir(task_path) if os.path.isfile('POSCAR') : os.remove('POSCAR') os.symlink(os.path.relpath(equi_contcar), 'POSCAR') os.chdir(cwd) task_poscar = os.path.join(task_path, 'POSCAR') # stress equi_outcar = os.path.join(equi_path, 'OUTCAR') stress = vasp.get_stress(equi_outcar) np.savetxt(os.path.join(task_path, 'equi.stress.out'), stress) # gen strcture ss = Structure.from_file(task_poscar) # gen defomations norm_strains = [-norm_def, -0.5*norm_def, 0.5*norm_def, norm_def] shear_strains = [-shear_def, -0.5*shear_def, 0.5*shear_def, shear_def] dfm_ss = DeformedStructureSet(ss, symmetry = False, norm_strains = norm_strains, shear_strains = shear_strains) n_dfm = len(dfm_ss) # gen incar if 'relax_incar' in jdata.keys(): relax_incar_path = jdata['relax_incar'] assert(os.path.exists(relax_incar_path)) relax_incar_path = os.path.abspath(relax_incar_path) fc = open(relax_incar_path).read() kspacing =float(re.findall((r"KSPACING(.+?)\n"),fc)[0].replace('=','')) kgamma =('T' in re.findall((r"KGAMMA(.+?)\n"),fc)[0]) else : fp_params = jdata['vasp_params'] ecut = fp_params['ecut'] ediff = fp_params['ediff'] npar = fp_params['npar'] kpar = fp_params['kpar'] kspacing = fp_params['kspacing'] kgamma = fp_params['kgamma'] fc = vasp.make_vasp_relax_incar(ecut, ediff, True, False, False, npar=npar,kpar=kpar, kspacing = kspacing, kgamma = kgamma) with open(os.path.join(task_path, 'INCAR'), 'w') as fp : fp.write(fc) # gen potcar with open(task_poscar,'r') as fp : lines = fp.read().split('\n') ele_list = lines[5].split() potcar_map = jdata['potcar_map'] potcar_list = [] for ii in ele_list : assert os.path.exists(os.path.abspath(potcar_map[ii])),"No POTCAR in the potcar_map of %s"%(ii) potcar_list.append(os.path.abspath(potcar_map[ii])) with open(os.path.join(task_path,'POTCAR'), 'w') as outfile: for fname in potcar_list: with open(fname) as infile: outfile.write(infile.read()) # gen kpoints fc = vasp.make_kspacing_kpoints(task_poscar, kspacing, kgamma) with open(os.path.join(task_path,'KPOINTS'), 'w') as fp: fp.write(fc) # gen tasks cwd = os.getcwd() for ii in range(n_dfm) : # make dir dfm_path = os.path.join(task_path, 'dfm-%03d' % ii) os.makedirs(dfm_path, exist_ok=True) os.chdir(dfm_path) for jj in ['POSCAR', 'POTCAR', 'INCAR', 'KPOINTS'] : if os.path.isfile(jj): os.remove(jj) # make conf dfm_ss.deformed_structures[ii].to('POSCAR', 'POSCAR') # record strain strain = Strain.from_deformation(dfm_ss.deformations[ii]) np.savetxt('strain.out', strain) # link incar, potcar, kpoints os.symlink(os.path.relpath(os.path.join(task_path, 'INCAR')), 'INCAR') os.symlink(os.path.relpath(os.path.join(task_path, 'POTCAR')), 'POTCAR') os.symlink(os.path.relpath(os.path.join(task_path, 'KPOINTS')), 'KPOINTS') cwd = os.getcwd()
def make_lammps(jdata, conf_dir,task_type) : fp_params = jdata['lammps_params'] model_dir = fp_params['model_dir'] type_map = fp_params['type_map'] model_dir = os.path.abspath(model_dir) model_name =fp_params['model_name'] if not model_name and task_type =='deepmd': models = glob.glob(os.path.join(model_dir, '*pb')) model_name = [os.path.basename(ii) for ii in models] assert len(model_name)>0,"No deepmd model in the model_dir" else: models = [os.path.join(model_dir,ii) for ii in model_name] model_param = {'model_name' : fp_params['model_name'], 'param_type': fp_params['model_param_type']} ntypes = len(type_map) norm_def = jdata['norm_deform'] shear_def = jdata['shear_deform'] conf_path = os.path.abspath(conf_dir) conf_poscar = os.path.join(conf_path, 'POSCAR') # get equi poscar equi_path = re.sub('confs', global_equi_name, conf_path) equi_path = os.path.join(equi_path, task_type) equi_dump = os.path.join(equi_path, 'dump.relax') task_path = re.sub('confs', global_task_name, conf_path) task_path = os.path.join(task_path, task_type) os.makedirs(task_path, exist_ok=True) task_poscar = os.path.join(task_path, 'POSCAR') lammps.poscar_from_last_dump(equi_dump, task_poscar, type_map) # get equi stress equi_log = os.path.join(equi_path, 'log.lammps') stress = lammps.get_stress(equi_log) np.savetxt(os.path.join(task_path, 'equi.stress.out'), stress) # gen strcture # ss = Structure.from_file(conf_poscar) # print(ss) # ss = ss.from_file(task_poscar) # print(ss) ss = Structure.from_file(task_poscar) # gen defomations norm_strains = [-norm_def, -0.5*norm_def, 0.5*norm_def, norm_def] shear_strains = [-shear_def, -0.5*shear_def, 0.5*shear_def, shear_def] print('gen with norm '+str(norm_strains)) print('gen with shear '+str(shear_strains)) dfm_ss = DeformedStructureSet(ss, symmetry = False, norm_strains = norm_strains, shear_strains = shear_strains) n_dfm = len(dfm_ss) # gen tasks cwd = os.getcwd() # make lammps.in if task_type=='deepmd': fc = lammps.make_lammps_elastic('conf.lmp', ntypes, lammps.inter_deepmd, model_name) elif task_type=='meam': fc = lammps.make_lammps_elastic('conf.lmp', ntypes, lammps.inter_meam, model_param) f_lammps_in = os.path.join(task_path, 'lammps.in') with open(f_lammps_in, 'w') as fp : fp.write(fc) cwd = os.getcwd() os.chdir(task_path) for ii in model_name : if os.path.exists(ii) : os.remove(ii) for (ii,jj) in zip(models, model_name) : os.symlink(os.path.relpath(ii), jj) share_models = [os.path.join(task_path,ii) for ii in model_name] for ii in range(n_dfm) : # make dir dfm_path = os.path.join(task_path, 'dfm-%03d' % ii) os.makedirs(dfm_path, exist_ok=True) os.chdir(dfm_path) for jj in ['conf.lmp', 'lammps.in'] + model_name : if os.path.isfile(jj): os.remove(jj) # make conf dfm_ss.deformed_structures[ii].to('POSCAR', 'POSCAR') lammps.cvt_lammps_conf('POSCAR', 'conf.lmp') ptypes = vasp.get_poscar_types('POSCAR') lammps.apply_type_map('conf.lmp', type_map, ptypes) # record strain strain = Strain.from_deformation(dfm_ss.deformations[ii]) np.savetxt('strain.out', strain) # link lammps.in os.symlink(os.path.relpath(f_lammps_in), 'lammps.in') # link models for (ii,jj) in zip(share_models, model_name) : os.symlink(os.path.relpath(ii), jj) cwd = os.getcwd()
#!/home/nicholas/CODE/git/aiida/bin/python import pymatgen as mg from pymatgen.analysis.elasticity.strain import DeformedStructureSet import os structure = mg.Structure.from_file("POSCAR") def_set = DeformedStructureSet(structure) count = 1 for struct in def_set: struct.to(fmt="POSCAR", filename="./POSCAR%s" % count) count += 1
def make_confs(self, path_to_work, path_to_equi, refine=False): path_to_work = os.path.abspath(path_to_work) path_to_equi = os.path.abspath(path_to_equi) task_list = [] cwd = os.getcwd() norm_def = self.norm_deform shear_def = self.shear_deform norm_strains = [-norm_def, -0.5 * norm_def, 0.5 * norm_def, norm_def] shear_strains = [ -shear_def, -0.5 * shear_def, 0.5 * shear_def, shear_def ] print('gen with norm ' + str(norm_strains)) print('gen with shear ' + str(shear_strains)) equi_contcar = os.path.join(path_to_equi, 'CONTCAR') if not os.path.exists(equi_contcar): raise RuntimeError("please do relaxation first") ss = Structure.from_file(equi_contcar) dfm_ss = DeformedStructureSet(ss, symmetry=False, norm_strains=norm_strains, shear_strains=shear_strains) n_dfm = len(dfm_ss) os.chdir(path_to_work) if os.path.isfile('POSCAR'): os.remove('POSCAR') os.symlink(os.path.relpath(equi_contcar), 'POSCAR') # task_poscar = os.path.join(output, 'POSCAR') # stress equi_outcar = os.path.join(path_to_equi, 'OUTCAR') equi_log = os.path.join(path_to_equi, 'log.lammps') if os.path.exists(equi_outcar): stress = vasp.get_stress(equi_outcar) np.savetxt('equi.stress.out', stress) elif os.path.exists(equi_log): stress = lammps.get_stress(equi_log) np.savetxt('equi.stress.out', stress) os.chdir(cwd) if refine: task_list = make_refine(self.parameter['init_from_suffix'], self.parameter['output_suffix'], path_to_work, n_dfm) os.chdir(cwd) else: for ii in range(n_dfm): output_task = os.path.join(path_to_work, 'task.%06d' % ii) os.makedirs(output_task, exist_ok=True) os.chdir(output_task) for jj in [ 'INCAR', 'POTCAR', 'POSCAR', 'conf.lmp', 'in.lammps' ]: if os.path.exists(jj): os.remove(jj) task_list.append(output_task) dfm_ss.deformed_structures[ii].to('POSCAR', 'POSCAR') # record strain strain = Strain.from_deformation(dfm_ss.deformations[ii]) np.savetxt('strain.out', strain) os.chdir(cwd) return task_list
import pymatgen as mg from pymatgen.analysis.elasticity.strain import DeformedStructureSet import os from shutil import copyfile from pymatgen.io.vasp.outputs import Vasprun from pymatgen.analysis.elasticity.stress import Stress from pymatgen.analysis.elasticity.elastic import ElasticTensor structure = mg.Structure.from_file("POSCAR") def_set = DeformedStructureSet(structure) strains = def_set.as_strain_dict() calculations = [] for x in range(1, 25): calculations.append('poscar%s' % x) match_dict = {} for calc in calculations: struct = mg.Structure.from_file(calc + "/POSCAR") vrun = Vasprun(calc + '/vasprun.xml', parse_dos=False, parse_eigen=False) stress = Stress(vrun.ionic_steps[-1]['stress']) for strain in strains: if strains[strain].lattice == struct.lattice: match_dict[strain] = stress elastics = ElasticTensor.from_stress_dict(match_dict) with open("elasts.txt", 'w') as f: f.write(str(elastics.voigt) + '\n') f.write(str(elastics.k_voigt) + '\n')