-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculationNodes.py
More file actions
397 lines (345 loc) · 16.2 KB
/
CalculationNodes.py
File metadata and controls
397 lines (345 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import pyiron_workflow as pwf
from Helper_functions import _get_structure
from DataClassNode import WorkflowState, DataInputs
from typing import Union, Optional, Any
@pwf.as_function_node
def GeometryOptimization(I,
return_as_dataclass:bool = True):
"""
Perform complete geometry optimization (cell and atomic positions) using LAMMPS or VASP.
[... keep your existing docstring ...]
Returns
-------
Output : WorkflowState or dict
If return_as_dataclass=True:
WorkflowState with fields:
- RelaxedStructure : Structure
Relaxed atomic structure
- acell_relaxed : ndarray
Relaxed lattice constants (Å)
- Cell_RelaxedStructure : ndarray
Relaxed simulation cell vectors (Å)
- Energy : float
Total energy of relaxed structure (eV)
- Pressures : ndarray
Pressure tensor components (GPa)
- ForceMax : float
Maximum force magnitude on any atom (eV/Å)
If return_as_dataclass=False:
Dictionary with keys: 'FinalEnergy', 'FinalPressures', 'ForceMax',
'FinalStructure', 'LatticeConstants'
"""
import numpy as np
import pandas as pd
import os
name = 'CompleteGeometryOptimization'
if I.calcengine.upper()=='LAMMPS':
assert I.MinimizerForCompleteRelaxation not in ['quickmin', 'fire', 'hftn','cg/kk'], \
"The quickmin, fire, hftn, and cg/kk styles do not yet support the use of the fix box/relax command or minimizations involving the electron radius in eFF models."
from pyiron_atomistics.lammps.lammps import lammps_function
if isinstance(I.InteratomicPotential, pd.DataFrame):
executable_version = 'conda'
PotentialName = I.InteratomicPotential['Name']
else:
executable_version = None
print("###############################################################################")
print(f"Using the Interatomic potential defined in: {I.InteratomicPotential}")
print("###############################################################################")
_, parsed_output, job_crashed = lammps_function(
working_directory=os.path.abspath(os.path.join(I.ProjectName, name)),
structure=I.InputStructure,
potential=I.InteratomicPotential,
calc_mode="minimize",
calc_kwargs={"style":I.MinimizerForCompleteRelaxation,
"pressure": [0.0,0.0,0.0,0.0,0.0,0.0],
"ionic_energy_tolerance":I.etol,
"ionic_force_tolerance":I.ftol,
"n_print": 100000},
cutoff_radius=None,
units="metal",
bonds_kwargs={},
enable_h5md=False,
executable_version=executable_version
)
parsed_output_red = parsed_output.get('generic')
RelaxedStructure = _get_structure(
structure=I.InputStructure,
cell=parsed_output_red.get('cells')[-1],
indices=parsed_output_red.get('indices')[-1],
positions=parsed_output_red.get('positions')[-1],
unwrapped_positions=parsed_output_red.get('unwrapped_positions')[-1]
)
elif I.calcengine.upper()=='VASP':
from pyiron_atomistics.vasp.vasp import vasp_function
print("###############################################################################")
print(f"Setting Encut to {I.Encut} and using a Kmesh of {I.Kmesh}")
print("###############################################################################")
_, parsed_output, job_crashed = vasp_function(
working_directory=os.path.abspath(os.path.join(I.ProjectName, name)),
structure=I.InputStructure,
plane_wave_cutoff=I.Encut,
kpoints_kwargs={"mesh":I.Kmesh},
calc_mode="minimize",
calc_kwargs={"pressure":0.0,
"ionic_steps":100,
"electronic_steps":60,
"ionic_energy_tolerance":I.etol,
"ionic_force_tolerance":I.ftol
},
occupancy_smearing_kwargs={"smearing":'MP',
"width":0.6},
convergence_precision_kwargs={"electronic_energy":I.etol},
)
parsed_output_red = parsed_output.get('generic')
RelaxedStructure = _get_structure(
structure=I.InputStructure,
cell=parsed_output.get('generic').get('cells')[-1],
indices=parsed_output.get('structure').get('indices'),
positions=parsed_output.get('generic').get('positions')[-1],
unwrapped_positions=parsed_output.get('generic').get('positions')[-1]
)
else:
raise ValueError(f"Unknown calcengine: {I.calcengine}. Must be 'LAMMPS' or 'VASP'")
# Extract results
Cell_RelaxedStructure = parsed_output_red.get('cells')[-1]
Energy_RelaxedStructure = parsed_output_red.get('energy_tot')[-1]
Pressures_RelaxedStructure = parsed_output_red.get('pressures')[-1]
ForceMax_RelaxedStructure = max([np.linalg.norm(ele) for ele in parsed_output_red.get('forces')[-1]])
# Compute relaxed lattice constants
if I.MillerIndices in [(0,0,1), (1,0,0), (0,1,0)] or not I.MillerIndices:
key = '001'
elif I.MillerIndices in [(0,1,1), (1,1,0), (1,0,1)]:
key = '110'
else:
key = ''.join(str(i) for i in I.MillerIndices)
dir_dict = {'001':[[1,0,0],[0,1,0],[0,0,1]],
'110':[[0,0,1],[-1,1,0],[1,1,0]],
'111':[[1,-1,0],[1,1,-2],[1,1,1]]}
millermod = [np.linalg.norm(ele) for ele in dir_dict.get(key)]
div_matrix = np.eye(3) * I.SuperCellDimensions * millermod
acell_relaxed = Cell_RelaxedStructure @ np.linalg.inv(div_matrix)
volume_per_atom = RelaxedStructure.get_volume() / len(RelaxedStructure)
acell_calculated = (4 * volume_per_atom)**(1/3)
# Return appropriate format
if return_as_dataclass:
Output = WorkflowState.dataclass(
ProjectName=I.ProjectName,
calcengine=I.calcengine,
etol=I.etol,
ftol=I.ftol,
verbose=I.verbose,
vacuum=I.vacuum,
calctype=I.calctype,
InteratomicPotential=I.InteratomicPotential,
MinimizerForSurfaces=I.MinimizerForSurfaces,
Encut=I.Encut,
Kmesh=I.Kmesh,
RelaxedStructure=RelaxedStructure,
acell_relaxed=acell_calculated,
Cell_RelaxedStructure=Cell_RelaxedStructure,
Energy=Energy_RelaxedStructure,
Pressures=Pressures_RelaxedStructure,
ForceMax=ForceMax_RelaxedStructure,
Cell=Cell_RelaxedStructure
)
if hasattr(I, 'DeformationMode'):
Output.DeformationMode = I.DeformationMode
# Output = create_workflow_state_from_geometry_optimization(
# I=I,
# RelaxedStructure=RelaxedStructure,
# acell_relaxed=acell_relaxed,
# Cell_RelaxedStructure=Cell_RelaxedStructure,
# Energy=Energy_RelaxedStructure,
# Pressures=Pressures_RelaxedStructure,
# ForceMax=ForceMax_RelaxedStructure
# )
else:
Output = {
'FinalEnergy': Energy_RelaxedStructure,
'FinalPressures': Pressures_RelaxedStructure,
'ForceMax': ForceMax_RelaxedStructure,
'FinalStructure': RelaxedStructure,
'LatticeConstants': acell_relaxed
}
return Output
# parsed_output, RelaxedStructure, Energy_RelaxedStructure, Pressures_RelaxedStructure, ForceMax_RelaxedStructure, acell
@pwf.as_function_node
def ForceMinimization(I: Union[DataInputs.dataclass, WorkflowState.dataclass],
Structure_variable_name: str,
comment: str = '0',
poisson_relaxation: bool = False,
return_as_dataclass: bool = True):
"""
Perform force minimization or static calculation on a structure.
[... keep existing docstring but update Parameters section ...]
Parameters
----------
I : DataInputs or WorkflowState
Input dataclass containing calculation parameters and structure
[... rest of parameters ...]
Returns
-------
Output : WorkflowState or dict
If return_as_dataclass=True:
WorkflowState with updated fields
If return_as_dataclass=False:
Dictionary with keys: 'FinalEnergy', 'FinalPressures', 'ForceMax',
'FinalStructure'
"""
import pandas as pd
import numpy as np
import os
import copy
name = f'Minimization_{Structure_variable_name}_{comment}'
Structure = getattr(I, Structure_variable_name)
Structure = copy.deepcopy(Structure)
print(f'Performing Calculation: {name}')
if I.calcengine.upper() == 'LAMMPS':
###############################################################################
# LAMMPS Stuff #
###############################################################################
from pyiron_atomistics.lammps.lammps import lammps_function
if isinstance(I.InteratomicPotential, pd.DataFrame):
executable_version = 'conda'
PotentialName = I.InteratomicPotential['Name']
else:
executable_version = None
if I.calctype.upper() in ['RELAX', 'RELAXED']:
if poisson_relaxation:
assert I.MinimizerForSurfaces not in ['quickmin', 'fire', 'hftn','cg/kk'], \
"The quickmin, fire, hftn, and cg/kk styles do not yet support the use of the fix box/relax command or minimizations involving the electron radius in eFF models."
calc_kwargs = {
"style": I.MinimizerForSurfaces,
"pressure": [None, 0.0, 0.0, None, None, None],
"ionic_energy_tolerance": I.etol,
"ionic_force_tolerance": I.ftol,
"n_print": 100000
}
else:
calc_kwargs = {
"style": I.MinimizerForSurfaces,
"ionic_energy_tolerance": I.etol,
"ionic_force_tolerance": I.ftol,
"n_print": 100000
}
_, parsed_output, job_crashed = lammps_function(
working_directory=os.path.abspath(os.path.join(I.ProjectName, name)),
structure=Structure,
potential=I.InteratomicPotential,
calc_mode="minimize",
calc_kwargs=calc_kwargs,
cutoff_radius=None,
units="metal",
bonds_kwargs={},
enable_h5md=False,
executable_version=executable_version
)
elif I.calctype.upper() in ['STATIC', 'UNRELAXED']:
_, parsed_output, job_crashed = lammps_function(
working_directory=os.path.abspath(os.path.join(I.ProjectName, name)),
structure=Structure,
potential=I.InteratomicPotential,
calc_mode="static",
calc_kwargs={},
cutoff_radius=None,
units="metal",
bonds_kwargs={},
enable_h5md=False,
executable_version=executable_version
)
parsed_output_red = parsed_output.get('generic')
Structure_Minimized = _get_structure(
structure=Structure,
cell=parsed_output_red.get('cells')[-1],
indices=parsed_output_red.get('indices')[-1],
positions=parsed_output_red.get('positions')[-1],
unwrapped_positions=parsed_output_red.get('unwrapped_positions')[-1]
)
elif I.calcengine.upper() == 'VASP':
from pyiron_atomistics.vasp.vasp import vasp_function
###############################################################################
# VASP Stuff #
###############################################################################
if I.calctype.upper() in ['RELAX', 'RELAXED']:
_, parsed_output, job_crashed = vasp_function(
working_directory=os.path.abspath(os.path.join(I.ProjectName, name)),
structure=Structure,
plane_wave_cutoff=I.Encut,
kpoints_kwargs={"mesh": I.Kmesh},
calc_mode="minimize",
calc_kwargs={
"ionic_steps": 100,
"electronic_steps": 60,
"ionic_energy_tolerance": I.etol,
"ionic_force_tolerance": I.ftol
},
occupancy_smearing_kwargs={"smearing": 'MP', "width": 0.6},
convergence_precision_kwargs={"electronic_energy": I.etol},
)
elif I.calctype.upper() in ['STATIC', 'UNRELAXED']:
_, parsed_output, job_crashed = vasp_function(
working_directory=os.path.abspath(os.path.join(I.ProjectName, name)),
structure=Structure,
plane_wave_cutoff=I.Encut,
kpoints_kwargs={"mesh": I.Kmesh},
calc_mode="static",
calc_kwargs={},
occupancy_smearing_kwargs={"smearing": 'MP', "width": 0.6},
convergence_precision_kwargs={"electronic_energy": I.etol},
)
parsed_output_red = parsed_output.get('generic')
Structure_Minimized = _get_structure(
structure=Structure,
cell=parsed_output_red.get('cells')[-1],
indices=parsed_output.get('structure').get('indices'),
positions=parsed_output_red.get('positions')[-1],
unwrapped_positions=parsed_output_red.get('positions')[-1]
)
else:
raise ValueError(f"Unknown calcengine: {I.calcengine}. Must be 'LAMMPS' or 'VASP'")
Energy_Minimization = parsed_output_red.get('energy_tot')[-1]
Pressures_Minimization = parsed_output_red.get('pressures')[-1]
ForceMax_Minimization = max([np.linalg.norm(ele) for ele in parsed_output_red.get('forces')[-1]])
Cell_Minimization = parsed_output_red.get('cells')[-1]
if return_as_dataclass:
# Check if input is WorkflowState or DataInputs
if isinstance(I, WorkflowState.dataclass):
# If WorkflowState, just copy and update
Output = I.copy()
Output.Structure_Minimized = Structure_Minimized
Output.Energy = Energy_Minimization
Output.Pressures = Pressures_Minimization
Output.ForceMax = ForceMax_Minimization
Output.Cell = Cell_Minimization
else:
# If DataInputs, create a new WorkflowState
Output = WorkflowState.dataclass(
ProjectName=I.ProjectName,
calcengine=I.calcengine,
etol=I.etol,
ftol=I.ftol,
verbose=I.verbose,
vacuum=I.vacuum,
calctype=I.calctype,
InteratomicPotential=I.InteratomicPotential,
MinimizerForSurfaces=I.MinimizerForSurfaces,
Encut=I.Encut,
Kmesh=I.Kmesh,
Structure_Minimized=Structure_Minimized,
Energy=Energy_Minimization,
Pressures=Pressures_Minimization,
ForceMax=ForceMax_Minimization,
Cell=Cell_Minimization
)
# After creating your output dataclass instance
if hasattr(I, 'DeformationMode'):
Output.DeformationMode = I.DeformationMode
else:
Output = {
'FinalEnergy': Energy_Minimization,
'FinalPressures': Pressures_Minimization,
'ForceMax': ForceMax_Minimization,
'FinalStructure': Structure_Minimized
}
return Output
# I am making this change now and now