def cmpt_vasp(jdata, conf_dir): fp_params = jdata['vasp_params'] kspacing = fp_params['kspacing'] kgamma = fp_params['kgamma'] conf_path = os.path.abspath(conf_dir) conf_poscar = os.path.join(conf_path, 'POSCAR') task_path = re.sub('confs', global_task_name, conf_path) if 'relax_incar' in jdata.keys(): vasp_str = 'vasp-relax_incar' else: vasp_str = 'vasp-k%.2f' % kspacing task_path = os.path.join(task_path, vasp_str) equi_stress = Stress(np.loadtxt(os.path.join(task_path, 'equi.stress.out'))) lst_dfm_path = glob.glob(os.path.join(task_path, 'dfm-*')) lst_strain = [] lst_stress = [] for ii in lst_dfm_path: strain = np.loadtxt(os.path.join(ii, 'strain.out')) stress = vasp.get_stress(os.path.join(ii, 'OUTCAR')) # convert from pressure in kB to stress stress *= -1000 lst_strain.append(Strain(strain)) lst_stress.append(Stress(stress)) et = ElasticTensor.from_independent_strains(lst_strain, lst_stress, eq_stress=equi_stress, vasp=False) # et = ElasticTensor.from_independent_strains(lst_strain, lst_stress, eq_stress = None) # bar to GPa # et = -et / 1e4 print_et(et)
def cmpt_deepmd_lammps(jdata, conf_dir, task_name): deepmd_model_dir = jdata['deepmd_model_dir'] deepmd_type_map = jdata['deepmd_type_map'] ntypes = len(deepmd_type_map) conf_path = os.path.abspath(conf_dir) conf_poscar = os.path.join(conf_path, 'POSCAR') task_path = re.sub('confs', global_task_name, conf_path) task_path = os.path.join(task_path, task_name) equi_stress = Stress(np.loadtxt(os.path.join(task_path, 'equi.stress.out'))) lst_dfm_path = glob.glob(os.path.join(task_path, 'dfm-*')) lst_strain = [] lst_stress = [] for ii in lst_dfm_path: strain = np.loadtxt(os.path.join(ii, 'strain.out')) stress = lammps.get_stress(os.path.join(ii, 'log.lammps')) # convert from pressure to stress stress = -stress lst_strain.append(Strain(strain)) lst_stress.append(Stress(stress)) et = ElasticTensor.from_independent_strains(lst_strain, lst_stress, eq_stress=equi_stress, vasp=False) # et = ElasticTensor.from_independent_strains(lst_strain, lst_stress, eq_stress = None) # bar to GPa # et = -et / 1e4 print_et(et)
def _fit_elastic_tensor(stresses, strains, strain_magnitudes, equilibrium_structure, equilibrium_stress, symmetric_strains_only=True): symm_eql_stresses = copy.deepcopy(stresses) symm_eql_strains = copy.deepcopy(strains) if symmetric_strains_only: all_deformations = _get_deformed_structures(equilibrium_structure, strain_magnitudes, symmetric_strains_only=False)[0] symmetry_operations_dict = symmetry_reduce(all_deformations, equilibrium_structure) deformations = [deformation for deformation in symmetry_operations_dict] for i in range(len(deformations)): deformation = deformations[i] symmetry_operations = [x for x in symmetry_operations_dict[deformation]] for symm_op in symmetry_operations: symm_eql_strains.append(strains[i].transform(symm_op)) symm_eql_stresses.append(stresses[i].transform(symm_op)) # Fit the elastic constants compliance_tensor = ElasticTensor.from_independent_strains( stresses=symm_eql_stresses, strains=symm_eql_strains, eq_stress=equilibrium_stress ) compliance_tensor = -1.0 * compliance_tensor # pymatgen has opposite sign convention return symm_eql_stresses, symm_eql_strains, compliance_tensor
def cmpt_deepmd_lammps(jdata, conf_dir, task_name) : conf_path = os.path.abspath(conf_dir) conf_poscar = os.path.join(conf_path, 'POSCAR') task_path = re.sub('confs', global_task_name, conf_path) task_path = os.path.join(task_path, task_name) equi_stress = Stress(np.loadtxt(os.path.join(task_path, 'equi.stress.out'))) lst_dfm_path = glob.glob(os.path.join(task_path, 'dfm-*')) lst_strain = [] lst_stress = [] for ii in lst_dfm_path : strain = np.loadtxt(os.path.join(ii, 'strain.out')) stress = lammps.get_stress(os.path.join(ii, 'log.lammps')) # convert from pressure to stress stress = -stress lst_strain.append(Strain(strain)) lst_stress.append(Stress(stress)) et = ElasticTensor.from_independent_strains(lst_strain, lst_stress, eq_stress = equi_stress, vasp = False) # et = ElasticTensor.from_independent_strains(lst_strain, lst_stress, eq_stress = None) # bar to GPa # et = -et / 1e4 print_et(et) result = os.path.join(task_path,'result') result_et(et,conf_dir,result) if 'upload_username' in jdata.keys() and task_name=='deepmd': upload_username=jdata['upload_username'] util.insert_data('elastic','deepmd',upload_username,result)
def test_from_independent_strains(self): strains = self.toec_dict["strains"] stresses = self.toec_dict["stresses"] with warnings.catch_warnings(record=True) as w: et = ElasticTensor.from_independent_strains(strains, stresses) self.assertArrayAlmostEqual(et.voigt, self.toec_dict["C2_raw"], decimal=-1)
def _compute_lower(self, output_file, all_tasks, all_res): output_file = os.path.abspath(output_file) res_data = {} ptr_data = output_file + '\n' equi_stress = Stress( np.loadtxt( os.path.join(os.path.dirname(output_file), 'equi.stress.out'))) lst_strain = [] lst_stress = [] for ii in all_tasks: with open(os.path.join(ii, 'inter.json')) as fp: idata = json.load(fp) inter_type = idata['type'] strain = np.loadtxt(os.path.join(ii, 'strain.out')) if inter_type == 'vasp': stress = vasp.get_stress(os.path.join(ii, 'OUTCAR')) # convert from pressure in kB to stress stress *= -1000 lst_strain.append(Strain(strain)) lst_stress.append(Stress(stress)) elif inter_type in ['deepmd', 'meam', 'eam_fs', 'eam_alloy']: stress = lammps.get_stress(os.path.join(ii, 'log.lammps')) # convert from pressure to stress stress = -stress lst_strain.append(Strain(strain)) lst_stress.append(Stress(stress)) et = ElasticTensor.from_independent_strains(lst_strain, lst_stress, eq_stress=equi_stress, vasp=False) res_data['elastic_tensor'] = [] for ii in range(6): for jj in range(6): res_data['elastic_tensor'].append(et.voigt[ii][jj] / 1e4) ptr_data += "%7.2f " % (et.voigt[ii][jj] / 1e4) ptr_data += '\n' BV = et.k_voigt / 1e4 GV = et.g_voigt / 1e4 EV = 9 * BV * GV / (3 * BV + GV) uV = 0.5 * (3 * BV - 2 * GV) / (3 * BV + GV) res_data['BV'] = BV res_data['GV'] = GV res_data['EV'] = EV res_data['uV'] = uV ptr_data += "# Bulk Modulus BV = %.2f GPa\n" % BV ptr_data += "# Shear Modulus GV = %.2f GPa\n" % GV ptr_data += "# Youngs Modulus EV = %.2f GPa\n" % EV ptr_data += "# Poission Ratio uV = %.2f " % uV with open(output_file, 'w') as fp: json.dump(res_data, fp, indent=4) return res_data, ptr_data
def legacy_fit(strains, stresses): """ Legacy fitting method for mpworks documents, intended to be temporary Args: strains: strains stresses: stresses Returns: elastic tensor fit using the legacy functionality """ strains = [s.zeroed(0.002) for s in strains] return ElasticTensor.from_independent_strains(strains, stresses)
def _compute_lower(self, output_file, all_tasks, all_res): output_file = os.path.abspath(output_file) res_data = {} ptr_data = os.path.dirname(output_file) + '\n' equi_stress = Stress( loadfn( os.path.join(os.path.dirname(output_file), 'equi.stress.json'))) lst_strain = [] lst_stress = [] for ii in all_tasks: strain = loadfn(os.path.join(ii, 'strain.json')) # stress, deal with unsupported stress in dpdata #with open(os.path.join(ii, 'result_task.json')) as fin: # task_result = json.load(fin) #stress = np.array(task_result['stress']['data'])[-1] stress = loadfn(os.path.join(ii, 'result_task.json'))['stress'][-1] lst_strain.append(strain) lst_stress.append(Stress(stress * -1000)) et = ElasticTensor.from_independent_strains(lst_strain, lst_stress, eq_stress=equi_stress, vasp=False) res_data['elastic_tensor'] = [] for ii in range(6): for jj in range(6): res_data['elastic_tensor'].append(et.voigt[ii][jj] / 1e4) ptr_data += "%7.2f " % (et.voigt[ii][jj] / 1e4) ptr_data += '\n' BV = et.k_voigt / 1e4 GV = et.g_voigt / 1e4 EV = 9 * BV * GV / (3 * BV + GV) uV = 0.5 * (3 * BV - 2 * GV) / (3 * BV + GV) res_data['BV'] = BV res_data['GV'] = GV res_data['EV'] = EV res_data['uV'] = uV ptr_data += "# Bulk Modulus BV = %.2f GPa\n" % BV ptr_data += "# Shear Modulus GV = %.2f GPa\n" % GV ptr_data += "# Youngs Modulus EV = %.2f GPa\n" % EV ptr_data += "# Poission Ratio uV = %.2f\n " % uV dumpfn(res_data, output_file, indent=4) return res_data, ptr_data
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 run_task(self, fw_spec): ref_struct = self['structure'] d = {"analysis": {}, "initial_structure": self['structure'].as_dict()} # Get optimized structure calc_locs_opt = [ cl for cl in fw_spec.get('calc_locs', []) if 'optimiz' in cl['name'] ] if calc_locs_opt: optimize_loc = calc_locs_opt[-1]['path'] logger.info("Parsing initial optimization directory: {}".format( optimize_loc)) drone = VaspDrone() optimize_doc = drone.assimilate(optimize_loc) opt_struct = Structure.from_dict( optimize_doc["calcs_reversed"][0]["output"]["structure"]) d.update({"optimized_structure": opt_struct.as_dict()}) ref_struct = opt_struct eq_stress = -0.1 * Stress(optimize_doc["calcs_reversed"][0] ["output"]["ionic_steps"][-1]["stress"]) else: eq_stress = None if self.get("fw_spec_field"): d.update({ self.get("fw_spec_field"): fw_spec.get(self.get("fw_spec_field")) }) # Get the stresses, strains, deformations from deformation tasks defo_dicts = fw_spec["deformation_tasks"].values() stresses, strains, deformations = [], [], [] for defo_dict in defo_dicts: stresses.append(Stress(defo_dict["stress"])) strains.append(Strain(defo_dict["strain"])) deformations.append(Deformation(defo_dict["deformation_matrix"])) # Add derived stresses and strains if symmops is present for symmop in defo_dict.get("symmops", []): stresses.append(Stress(defo_dict["stress"]).transform(symmop)) strains.append(Strain(defo_dict["strain"]).transform(symmop)) deformations.append( Deformation( defo_dict["deformation_matrix"]).transform(symmop)) stresses = [-0.1 * s for s in stresses] pk_stresses = [ stress.piola_kirchoff_2(deformation) for stress, deformation in zip(stresses, deformations) ] d['fitting_data'] = { 'cauchy_stresses': stresses, 'eq_stress': eq_stress, 'strains': strains, 'pk_stresses': pk_stresses, 'deformations': deformations } logger.info("Analyzing stress/strain data") # TODO: @montoyjh: what if it's a cubic system? don't need 6. -computron # TODO: Can add population method but want to think about how it should # be done. -montoyjh order = self.get('order', 2) if order > 2: method = 'finite_difference' else: method = self.get('fitting_method', 'finite_difference') if method == 'finite_difference': result = ElasticTensorExpansion.from_diff_fit(strains, pk_stresses, eq_stress=eq_stress, order=order) if order == 2: result = ElasticTensor(result[0]) elif method == 'pseudoinverse': result = ElasticTensor.from_pseudoinverse(strains, pk_stresses) elif method == 'independent': result = ElasticTensor.from_independent_strains( strains, pk_stresses, eq_stress=eq_stress) else: raise ValueError( "Unsupported method, method must be finite_difference, " "pseudoinverse, or independent") ieee = result.convert_to_ieee(ref_struct) d.update({ "elastic_tensor": { "raw": result.voigt, "ieee_format": ieee.voigt } }) if order == 2: d.update({ "derived_properties": ieee.get_structure_property_dict(ref_struct) }) else: soec = ElasticTensor(ieee[0]) d.update({ "derived_properties": soec.get_structure_property_dict(ref_struct) }) d["formula_pretty"] = ref_struct.composition.reduced_formula d["fitting_method"] = method d["order"] = order d = jsanitize(d) # Save analysis results in json or db db_file = env_chk(self.get('db_file'), fw_spec) if not db_file: with open("elasticity.json", "w") as f: f.write(json.dumps(d, default=DATETIME_HANDLER)) else: db = VaspCalcDb.from_db_file(db_file, admin=True) db.collection = db.db["elasticity"] db.collection.insert_one(d) logger.info("Elastic analysis complete.") return FWAction()
def run_task(self, fw_spec): ref_struct = self['structure'] d = { "analysis": {}, "initial_structure": self['structure'].as_dict() } # Get optimized structure calc_locs_opt = [cl for cl in fw_spec.get('calc_locs', []) if 'optimiz' in cl['name']] if calc_locs_opt: optimize_loc = calc_locs_opt[-1]['path'] logger.info("Parsing initial optimization directory: {}".format(optimize_loc)) drone = VaspDrone() optimize_doc = drone.assimilate(optimize_loc) opt_struct = Structure.from_dict(optimize_doc["calcs_reversed"][0]["output"]["structure"]) d.update({"optimized_structure": opt_struct.as_dict()}) ref_struct = opt_struct eq_stress = -0.1*Stress(optimize_doc["calcs_reversed"][0]["output"]["ionic_steps"][-1]["stress"]) else: eq_stress = None if self.get("fw_spec_field"): d.update({self.get("fw_spec_field"): fw_spec.get(self.get("fw_spec_field"))}) # Get the stresses, strains, deformations from deformation tasks defo_dicts = fw_spec["deformation_tasks"].values() stresses, strains, deformations = [], [], [] for defo_dict in defo_dicts: stresses.append(Stress(defo_dict["stress"])) strains.append(Strain(defo_dict["strain"])) deformations.append(Deformation(defo_dict["deformation_matrix"])) # Add derived stresses and strains if symmops is present for symmop in defo_dict.get("symmops", []): stresses.append(Stress(defo_dict["stress"]).transform(symmop)) strains.append(Strain(defo_dict["strain"]).transform(symmop)) deformations.append(Deformation(defo_dict["deformation_matrix"]).transform(symmop)) stresses = [-0.1*s for s in stresses] pk_stresses = [stress.piola_kirchoff_2(deformation) for stress, deformation in zip(stresses, deformations)] d['fitting_data'] = {'cauchy_stresses': stresses, 'eq_stress': eq_stress, 'strains': strains, 'pk_stresses': pk_stresses, 'deformations': deformations } logger.info("Analyzing stress/strain data") # TODO: @montoyjh: what if it's a cubic system? don't need 6. -computron # TODO: Can add population method but want to think about how it should # be done. -montoyjh order = self.get('order', 2) if order > 2: method = 'finite_difference' else: method = self.get('fitting_method', 'finite_difference') if method == 'finite_difference': result = ElasticTensorExpansion.from_diff_fit( strains, pk_stresses, eq_stress=eq_stress, order=order) if order == 2: result = ElasticTensor(result[0]) elif method == 'pseudoinverse': result = ElasticTensor.from_pseudoinverse(strains, pk_stresses) elif method == 'independent': result = ElasticTensor.from_independent_strains(strains, pk_stresses, eq_stress=eq_stress) else: raise ValueError("Unsupported method, method must be finite_difference, " "pseudoinverse, or independent") ieee = result.convert_to_ieee(ref_struct) d.update({ "elastic_tensor": { "raw": result.voigt, "ieee_format": ieee.voigt } }) if order == 2: d.update({"derived_properties": ieee.get_structure_property_dict(ref_struct)}) else: soec = ElasticTensor(ieee[0]) d.update({"derived_properties": soec.get_structure_property_dict(ref_struct)}) d["formula_pretty"] = ref_struct.composition.reduced_formula d["fitting_method"] = method d["order"] = order d = jsanitize(d) # Save analysis results in json or db db_file = env_chk(self.get('db_file'), fw_spec) if not db_file: with open("elasticity.json", "w") as f: f.write(json.dumps(d, default=DATETIME_HANDLER)) else: db = VaspCalcDb.from_db_file(db_file, admin=True) db.collection = db.db["elasticity"] db.collection.insert_one(d) logger.info("Elastic analysis complete.") return FWAction()
def test_from_independent_strains(self): strains = self.toec_dict["strains"] stresses = self.toec_dict["stresses"] with warnings.catch_warnings(record=True) as w: et = ElasticTensor.from_independent_strains(strains, stresses) self.assertArrayAlmostEqual(et.voigt, self.toec_dict["C2_raw"], decimal=-1)