@@ -213,6 +213,7 @@ def __init__(
213213 self .config = config
214214 if self .force_mult_natoms :
215215 self .config ["model" ]["force_mult_natoms" ] = True
216+
216217 if self .include_stress :
217218 self .implemented_properties = ["energy" , "forces" , "stress" ]
218219 if (
@@ -226,6 +227,11 @@ def __init__(
226227
227228 else :
228229 self .implemented_properties = ["energy" , "forces" ]
230+ if (
231+ "calculate_gradient" in self .config ["model" ]
232+ and self .config ["model" ]["calculate_gradient" ]
233+ ):
234+ self .trained_stress = True
229235
230236 if (
231237 batch_stress is not None
@@ -296,24 +302,57 @@ def calculate(self, atoms, properties=None, system_changes=None):
296302 )
297303 else :
298304 result = self .model (
299- (g .to (self .device , torch .tensor (atoms .cell ).to (self .device ) ))
305+ (g .to (self .device ) , torch .tensor (atoms .cell ).to (self .device ))
300306 )
307+ # print("result",result)
301308 if "atomwise" in self .config ["model" ]["name" ]:
302309 forces = forces = (
303310 result ["grad" ].detach ().cpu ().numpy () * self .force_multiplier
304311 )
305312 else :
306313 forces = np .zeros ((3 , 3 ))
307- if "atomwise" in self .config ["model" ]["name" ] and self .trained_stress :
308- stress = (
309- full_3x3_to_voigt_6_stress (
310- result ["stresses" ][:3 ].reshape (3 , 3 ).detach ().cpu ().numpy ()
314+ # print("self.trained_stress",self.trained_stress)
315+ # if self.trained_stress:
316+ # # if "atomwise" in self.config["model"]["name"]
317+ # # and self.trained_stress:
318+ # stress = (
319+ # full_3x3_to_voigt_6_stress(
320+ # result["stresses"][:3].r
321+ # eshape(3, 3).detach().cpu().numpy()
322+ # )
323+ # * self.stress_wt
324+ # / 160.21766208
325+ # )
326+ # else:
327+ # stress = np.zeros((3, 3))
328+ if (
329+ "calculate_gradient" in self .config ["model" ]
330+ and self .config ["model" ]["calculate_gradient" ]
331+ ):
332+ try :
333+ stress = (
334+ full_3x3_to_voigt_6_stress (
335+ result ["stresses" ][:3 ]
336+ .reshape (3 , 3 )
337+ .detach ()
338+ .cpu ()
339+ .numpy ()
340+ )
341+ * self .stress_wt
342+ / 160.21766208
311343 )
312- * self . stress_wt
313- / 160.21766208
314- )
344+ except Exception :
345+ stress = np . zeros (( 3 , 3 ))
346+ pass
315347 else :
316348 stress = np .zeros ((3 , 3 ))
349+ # stress = (
350+ # full_3x3_to_voigt_6_stress(
351+ # result["stresses"][:3].reshape(3, 3).detach().cpu().numpy()
352+ # )
353+ # * self.stress_wt
354+ # / 160.21766208
355+ # )
317356 if "atomwise" in self .config ["model" ]["name" ]:
318357 energy = result ["out" ].detach ().cpu ().numpy ()
319358 else :
@@ -325,6 +364,7 @@ def calculate(self, atoms, properties=None, system_changes=None):
325364 if self .force_mult_batchsize :
326365 forces *= self .config ["batch_size" ]
327366
367+ # print("stress cal",stress)
328368 self .results = {
329369 "energy" : energy ,
330370 "forces" : forces ,
@@ -449,6 +489,7 @@ def calculate(
449489 atom_features = self .ff_config ["atom_features" ],
450490 use_canonize = self .ff_config ["use_canonize" ],
451491 )
492+ # print("config",self.ff_config)
452493 result_ff = self .ff_model (
453494 (
454495 g .to (self .device ),
@@ -459,6 +500,7 @@ def calculate(
459500 )
460501 )
461502 forces = forces = result_ff ["grad" ].detach ().cpu ().numpy ()
503+
462504 stress = (
463505 full_3x3_to_voigt_6_stress (
464506 result_ff ["stresses" ][:3 ].reshape (3 , 3 ).detach ().cpu ().numpy ()
0 commit comments