-
Notifications
You must be signed in to change notification settings - Fork 45
/
interfacemethod.py
1347 lines (1186 loc) · 48.2 KB
/
interfacemethod.py
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
# Copyright (c) Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department
# Distributed under the terms of "New BSD License", see the LICENSE file.
"""
pyiron based implementation of the coexistence method. Currently this functionality is primarly used as part
of the melting point simulation protocol which is available at:
https://github.com/pyiron/pyiron_meltingpoint
"""
import json
import numpy as np
import operator
import os
import matplotlib.pylab as plt
import random
from sklearn.neighbors.kde import KernelDensity
__author__ = "Lifang Zhu, Jan Janssen"
__copyright__ = "Copyright 2020, Max-Planck-Institut für Eisenforschung GmbH " \
"- Computational Materials Design (CM) Department"
__version__ = "1.0"
__maintainer__ = "Jan Janssen"
__email__ = "janssen@mpie.de"
__status__ = "development"
__date__ = "Apr 24, 2020"
def freeze_one_half(basis):
"""
Split the structure into two parts along the z-axis and then freeze the position of the atoms
of the upper part (z>0.5) by setting selective dynamics to False.
Args:
basis (pyiron.structure.atoms.Atoms): Atomistic structure object
Returns:
pyiron.structure.atoms.Atoms: Atomistic structure object with half of the atoms fixed
"""
basis.add_tag(selective_dynamics=None)
_, _, z = basis.scaled_pos_xyz()
for selector, ind in zip(z < 0.5, range(len(basis))):
if selector:
basis.selective_dynamics[ind] = [True, True, True]
else:
basis.selective_dynamics[ind] = [False, False, False]
return basis
def remove_selective_dynamics(basis):
"""
If the selective dyanmics tag is set, allow all atoms to move by setting selective dynamics to True
Args:
basis (pyiron.structure.atoms.Atoms): Atomistic structure object
Returns:
Atoms: Atomistic structure object with selective dynamics set to True
"""
if 'selective_dynamics' in basis._tag_list.keys():
for ind in range(len(basis)):
basis.selective_dynamics[ind] = [True, True, True]
return basis
def set_server(job, project_parameter):
"""
Set the potential, queue and cpu_cores defined in the project_parameter dictionary to the job object.
Args:
job (GenericJob): Job object
project_parameter (dict): Dictionary with the keys potential and cpu_cores and optionally queue
Returns:
GenericJob: Updated job object
"""
job.potential = project_parameter['potential']
if 'queue' in project_parameter.keys():
job.server.queue = project_parameter['queue']
job.server.cores = project_parameter['cpu_cores']
return job
def create_job_template(job_name, structure, project_parameter):
"""
Create a job template using the project_parameter dictionary. The dictionary has to contain the following keys:
- job_type: Type of Simulation code to be used
- project: Project object used to create the job
- potential: Interatomic Potential
- queue (optional): HPC Job queue to be used
Args:
job_name (str): Name of the job object
structure (pyiron.structure.atoms.Atoms): Atomistic Structure object to be set to the job as input sturcture
project_parameter (dict): Dictionary with the project parameters
Returns:
GenericJob: New job object
"""
pr = project_parameter['project']
job = pr.create_job(project_parameter['job_type'], job_name)
job.structure = structure
return set_server(job=job, project_parameter=project_parameter)
def fix_iso(job):
"""
Add couple xyz to the fix_ensemble inside LAMMPS
Args:
job (LAMMPS): Lammps job object
Returns:
LAMMPS: Return updated job object
"""
job.input.control['fix___ensemble'] = job.input.control['fix___ensemble'] + ' couple xyz'
return job
def fix_z_dir(job):
"""
Rather than fixing all directions only fix the z-direction during an NPT simulation
Args:
job (LAMMPS): Lammps job object
Returns:
LAMMPS: Return updated job object
"""
job.input.control['fix___ensemble'] = \
job.input.control['fix___ensemble'].replace('x 0.0 0.0 1.0 y 0.0 0.0 1.0 z 0.0 0.0 1.0', 'z 0.0 0.0 1.0')
return job
def half_velocity(job, temperature):
"""
Rather than setting twice the kinetic energy at the beginning of a molecular dynamics simulation reduce the
velocity to half the initial velocity. This is required to continue MD claculation.
Args:
job (LAMMPS): Lammps job object
temperature (float): Temperature of the molecular dynamics calculation in K
Returns:
LAMMPS: Return updated job object
"""
job.input.control['velocity'] = job.input.control['velocity'].replace(str(temperature * 2), str(temperature))
return job
def minimize_pos(structure, project_parameter, max_iter=1000):
"""
Minimize the positions in a given structure using the job type defined in the project_parameters, which
contains the following keys:
- job_type: Type of Simulation code to be used
- project: Project object used to create the job
- potential: Interatomic Potential
- queue (optional): HPC Job queue to be used
Args:
structure (pyiron.structure.atoms.Atoms): Atomistic Structure object to be set to the job as input sturcture
project_parameter (dict): Dictionary wtih the project parameters
max_iter (int): Maximum number of steps during minimization
Returns:
job object used to execute the calculation
"""
ham_minimize_pos = create_job_template(
job_name='minimize_pos',
structure=structure,
project_parameter=project_parameter
)
ham_minimize_pos.calc_minimize(
max_iter=max_iter,
e_tol=1.0e-9,
f_tol=1.0e-8,
n_print=max_iter
)
ham_minimize_pos.run()
ham_minimize_pos.project.wait_for_job(
ham_minimize_pos,
interval_in_s=100,
max_iterations=100000
)
return ham_minimize_pos
def minimize_vol(structure, project_parameter, max_iter=1000):
"""
Minimize the volume for a given structure using the job type defined in the project_parameters, which
contains the following keys:
- job_type: Type of Simulation code to be used
- project: Project object used to create the job
- potential: Interatomic Potential
- queue (optional): HPC Job queue to be used
Args:
structure (pyiron.structure.atoms.Atoms): Atomistic Structure object to be set to the job as input sturcture
project_parameter (dict): Dictionary with the project parameters
max_iter (int): Maximum number of steps during minimization
Returns:
job object used to execute the calculation
"""
ham_minimize_vol = create_job_template(
job_name='minimize_vol',
structure=structure,
project_parameter=project_parameter
)
ham_minimize_vol.calc_minimize(
max_iter=max_iter,
e_tol=1.0e-9,
f_tol=1.0e-8,
pressure=0.0,
n_print=max_iter
)
ham_minimize_vol.input.control['fix___ensemble'] += ' vmax 0.001'
ham_minimize_vol.run()
ham_minimize_vol.project.wait_for_job(
ham_minimize_vol,
interval_in_s=100,
max_iterations=100000
)
return ham_minimize_vol
def next_calc(structure, temperature, project_parameter, run_time_steps=10000):
"""
Calculate NPT ensemble at a given temperature using the job defined in the project parameters:
- job_type: Type of Simulation code to be used
- project: Project object used to create the job
- potential: Interatomic Potential
- queue (optional): HPC Job queue to be used
Args:
structure (pyiron.structure.atoms.Atoms): Atomistic Structure object to be set to the job as input sturcture
temperature (float): Temperature of the Molecular dynamics calculation
project_parameter (dict): Dictionary with the project parameters
run_time_steps (int): Number of Molecular dynamics steps
Returns:
Final Atomistic Structure object
"""
ham_temp = create_job_template(
job_name='temp_heating_' + str(temperature).replace('.', '_'),
structure=structure,
project_parameter=project_parameter
)
ham_temp.calc_md(
temperature=temperature,
temperature_damping_timescale=100.0,
pressure=0.0,
pressure_damping_timescale=1000.0,
n_print=run_time_steps,
n_ionic_steps=run_time_steps,
seed=project_parameter['seed'],
)
ham_temp = fix_iso(job=ham_temp)
ham_temp = half_velocity(
job=ham_temp,
temperature=temperature
)
ham_temp.run()
ham_temp.project.wait_for_job(
ham_temp,
interval_in_s=100,
max_iterations=100000
)
return ham_temp.get_structure()
def npt_solid(temperature, basis, project_parameter, timestep=1.0):
"""
Calculate NPT ensemble at a given temperature using the job defined in the project parameters:
- job_type: Type of Simulation code to be used
- project: Project object used to create the job
- potential: Interatomic Potential
- queue (optional): HPC Job queue to be used
Args:
temperature (float): Temperature of the Molecular dynamics calculation
basis (pyiron.structure.atoms.Atoms): Atomistic Structure object to be set to the job as input sturcture
project_parameter (dict): Dictionary with the project parameters
timestep (float): Molecular dynamics time step
Returns:
job object used to execute the calculation
"""
ham_npt_solid = create_job_template(
job_name='ham_npt_solid_' + str(temperature).replace('.', '_'),
structure=basis,
project_parameter=project_parameter
)
ham_npt_solid.calc_md(
temperature=temperature,
temperature_damping_timescale=100.0,
time_step=timestep,
pressure=0.0,
pressure_damping_timescale=1000.0,
n_print=project_parameter['run_time_steps'],
n_ionic_steps=project_parameter['run_time_steps'],
seed=project_parameter['seed'],
)
ham_npt_solid = half_velocity(
job=ham_npt_solid,
temperature=temperature
)
ham_npt_solid = fix_iso(job=ham_npt_solid)
ham_npt_solid.run()
ham_npt_solid.project.wait_for_job(
ham_npt_solid,
interval_in_s=100,
max_iterations=100000
)
return ham_npt_solid
def setup_liquid_job(job_name, basis, temperature, project_parameter, timestep=1.0):
"""
Calculate NPT ensemble at a given temperature while freezing the position of the atoms
of the upper part (z>0.5) amd the using the job defined in the project parameters:
- job_type: Type of Simulation code to be used
- project: Project object used to create the job
- potential: Interatomic Potential
- queue (optional): HPC Job queue to be used
Args:
job_name (str): Job name for the liquid calculation
basis (pyiron.structure.atoms.Atoms): Atomistic Structure object to be set to the job as input sturcture
temperature (float): Temperature of the Molecular dynamics calculation
project_parameter (dict): Dictionary with the project parameters
timestep (float): Molecular dynamics time step
Returns:
job object used to execute the calculation
"""
ham_npt_liquid_high = create_job_template(
job_name=job_name,
structure=freeze_one_half(basis),
project_parameter=project_parameter
)
ham_npt_liquid_high.calc_md(
temperature=temperature,
temperature_damping_timescale=100.0,
time_step=timestep,
pressure=[0.0, 0.0, 0.0],
pressure_damping_timescale=1000.0,
n_print=project_parameter['run_time_steps'],
n_ionic_steps=project_parameter['run_time_steps'],
seed=project_parameter['seed'],
)
ham_npt_liquid_high = half_velocity(
job=ham_npt_liquid_high,
temperature=temperature
)
ham_npt_liquid_high = fix_z_dir(
job=ham_npt_liquid_high
)
ham_npt_liquid_high.run()
ham_npt_liquid_high.project.wait_for_job(
ham_npt_liquid_high,
interval_in_s=100,
max_iterations=100000
)
return ham_npt_liquid_high
def npt_liquid(temperature_solid, temperature_liquid, basis, project_parameter, timestep=1.0):
"""
Calculate NPT ensemble at a given temperature while initally freezing the position of the atoms
of the upper part (z>0.5) and afterwards calculating the full sample at a lower temperature.
These steps are used to construct the solid liquid interface as part of the coexistence approach.
For the calculations the job object is defined in the project parameters:
- job_type: Type of Simulation code to be used
- project: Project object used to create the job
- potential: Interatomic Potential
- queue (optional): HPC Job queue to be used
Args:
temperature_solid (flaot): Temperature to simulate the whole structure
temperature_liquid (float): Temperature to simulate the upper half of the structure
basis (pyiron.structure.atoms.Atoms): Atomistic Structure object to be set to the job as input sturcture
project_parameter (dict): Dictionary with the project parameters
timestep (float): Molecular dynamics time step
Returns:
job object used to execute the calculation
"""
ham_npt_liquid_high = setup_liquid_job(
job_name='ham_npt_liquid_high_' + str(temperature_liquid).replace('.', '_'),
basis=basis,
temperature=temperature_liquid,
project_parameter=project_parameter,
timestep=timestep
)
ham_npt_liquid_low = setup_liquid_job(
job_name='ham_npt_liquid_low_' + str(temperature_solid).replace('.', '_'),
basis=ham_npt_liquid_high.get_structure(iteration_step=-1),
temperature=temperature_solid,
project_parameter=project_parameter,
timestep=timestep
)
return ham_npt_liquid_low
def check_diamond(structure):
"""
Utility function to check if the structure is fcc, bcc, hcp or diamond
Args:
structure (pyiron.structure.atoms.Atoms): Atomistic Structure object to check
Returns:
bool: true if diamond else false
"""
cna_dict = structure.analyse_pyscal_cna_adaptive(
mode="total",
ovito_compatibility=True
)
dia_dict = structure.analyse_pyscal_diamond_structure(
mode="total",
ovito_compatibility=True
)
return cna_dict['CommonNeighborAnalysis.counts.OTHER'] > dia_dict['IdentifyDiamond.counts.OTHER']
def analyse_structure(structure, mode="total", diamond=False):
"""
Use either common neighbor analysis or the diamond structure detector
Args:
structure (pyiron.structure.atoms.Atoms): The structure to analyze.
mode ("total"/"numeric"/"str"): Controls the style and level
of detail of the output.
- total : return number of atoms belonging to each structure
- numeric : return a per atom list of numbers- 0 for unknown,
1 fcc, 2 hcp, 3 bcc and 4 icosa
- str : return a per atom string of sructures
diamond (bool): Flag to either use the diamond structure detector or
the common neighbor analysis.
Returns:
(depends on `mode`)
"""
if not diamond:
return structure.analyse_pyscal_cna_adaptive(
mode=mode,
ovito_compatibility=True
)
else:
return structure.analyse_pyscal_diamond_structure(
mode=mode,
ovito_compatibility=True
)
def next_step_funct(number_of_atoms,
key_max,
structure_left,
structure_right,
temperature_left,
temperature_right,
distribution_initial_half,
structure_after_minimization,
run_time_steps,
project_parameter):
"""
Args:
number_of_atoms:
key_max:
structure_left:
structure_right:
temperature_left:
temperature_right:
distribution_initial_half:
structure_after_minimization:
run_time_steps:
project_parameter:
Returns:
"""
structure_left_dict = analyse_structure(
structure=structure_left,
mode="total",
diamond=project_parameter['crystalstructure'].lower() == 'diamond'
)
structure_right_dict = analyse_structure(
structure=structure_right,
mode="total",
diamond=project_parameter['crystalstructure'].lower() == 'diamond'
)
temperature_diff = temperature_right - temperature_left
if structure_left_dict[key_max] / number_of_atoms > distribution_initial_half and \
structure_right_dict[key_max] / number_of_atoms > distribution_initial_half:
structure_left = structure_right.copy()
temperature_left = temperature_right
temperature_right += temperature_diff
structure_right = next_calc(
structure=structure_after_minimization,
temperature=temperature_right,
project_parameter=project_parameter,
run_time_steps=run_time_steps
)
elif structure_left_dict[key_max] / number_of_atoms > distribution_initial_half > \
structure_right_dict[key_max] / number_of_atoms:
temperature_diff /= 2
temperature_left += temperature_diff
structure_left = next_calc(
structure=structure_after_minimization,
temperature=temperature_left,
project_parameter=project_parameter,
run_time_steps=run_time_steps
)
elif structure_left_dict[key_max] / number_of_atoms < distribution_initial_half and \
structure_right_dict[key_max] / number_of_atoms < distribution_initial_half:
temperature_diff /= 2
temperature_right = temperature_left
temperature_left -= temperature_diff
structure_right = structure_left.copy()
structure_left = next_calc(
structure=structure_after_minimization,
temperature=temperature_left,
project_parameter=project_parameter,
run_time_steps=run_time_steps
)
else:
raise ValueError('We should never reach this point!')
return structure_left, structure_right, temperature_left, temperature_right
def round_temperature_next(temperature_next):
"""
Round temperature to the last two dicits
Args:
temperature_next (float): Temperature
Returns:
float: rounded temperature
"""
return np.round(temperature_next, 2)
def strain_circle(basis_relative, temperature_next, nve_run_time_steps, project_parameter, timestep=1.0,
strain_result_lst=None, pressure_result_lst=None, center=None, fit_range=0.02):
"""
Args:
basis_relative:
temperature_next:
nve_run_time_steps:
project_parameter:
timestep:
strain_result_lst:
pressure_result_lst:
center:
fit_range:
Returns:
"""
strain_lst, pressure_lst, temperature_lst, pressure_std_lst, temperature_std_lst = [], [], [], [], []
ovito_dict_lst, ham_nvt_lst, ham_nve_lst = [], [], []
strain_value_lst = get_strain_lst(
fit_range=fit_range,
points=project_parameter['points'],
strain_result_lst=strain_result_lst,
pressure_result_lst=pressure_result_lst,
center=center
)
temperature_next = round_temperature_next(temperature_next)
for strain in strain_value_lst:
job_name = get_nve_job_name(
temperature_next=temperature_next,
strain=strain,
steps_lst=project_parameter['nve_run_time_steps_lst'],
nve_run_time_steps=nve_run_time_steps
)
ham_nve = project_parameter['project'].load(job_name)
if ham_nve is None:
basis_strain = basis_relative.copy()
cell = basis_strain.cell.copy()
cell[2, 2] *= strain
basis_strain.set_cell(cell=cell, scale_atoms=True)
ham_nvt = create_job_template(job_name=job_name.replace('nve', 'nvt'),
structure=basis_strain,
project_parameter=project_parameter)
ham_nvt.calc_md(
temperature=temperature_next,
time_step=timestep,
temperature_damping_timescale=100.0,
n_print=project_parameter['nvt_run_time_steps'],
n_ionic_steps=project_parameter['nvt_run_time_steps'],
seed=project_parameter['seed'],
)
ham_nvt.input.control['fix___ensemble'] += ' drag 1'
ham_nvt = half_velocity(
job=ham_nvt,
temperature=temperature_next
)
ham_nvt.write_restart_file()
ham_nvt.run()
ham_nvt_lst.append(ham_nvt)
for ham_nvt in ham_nvt_lst:
ham_nvt.project.wait_for_job(
ham_nvt,
interval_in_s=100,
max_iterations=100000
)
ham_nve = ham_nvt.restart()
ham_nve.job_name = ham_nvt.job_name.replace('nvt', 'nve')
ham_nve.calc_md(
n_ionic_steps=nve_run_time_steps,
time_step=timestep,
n_print=nve_run_time_steps / 100,
seed=project_parameter['seed'],
)
ham_nve = set_server(
job=ham_nve,
project_parameter=project_parameter
)
ham_nve.input.control['dump___1'] = \
ham_nve.input.control['dump___1'].replace('${dumptime}', str(nve_run_time_steps))
ham_nve.run()
ham_nve_lst.append(ham_nve)
for ham_nve in ham_nve_lst:
ham_nve.project.wait_for_job(
ham_nve,
interval_in_s=100,
max_iterations=100000
)
for strain in strain_value_lst:
job_name = get_nve_job_name(
temperature_next=temperature_next,
strain=strain,
steps_lst=project_parameter['nve_run_time_steps_lst'],
nve_run_time_steps=nve_run_time_steps
)
ham_nve = project_parameter['project'].load(job_name)
press, temperature, press_std, temperature_std, ovito_dict = [
np.mean(get_press(ham=ham_nve, step=-20)),
np.mean(ham_nve['output/generic/temperature'][-20:]),
np.std(get_press(ham=ham_nve, step=-20)),
np.std(ham_nve['output/generic/temperature'][-20:]),
analyse_structure(
structure=ham_nve.get_structure(iteration_step=-1),
mode="total",
diamond=project_parameter['crystalstructure'].lower() == 'diamond'
)
]
strain_lst.append(strain)
pressure_lst.append(press)
temperature_lst.append(temperature)
pressure_std_lst.append(press_std)
temperature_std_lst.append(temperature_std)
ovito_dict_lst.append(ovito_dict)
return strain_lst, pressure_lst, temperature_lst, pressure_std_lst, temperature_std_lst, ovito_dict_lst
def analyse_minimized_structure(ham):
"""
Args:
ham (GenericJob):
Returns:
"""
final_structure = ham.get_structure(
iteration_step=-1
)
diamond_flag = check_diamond(structure=final_structure)
final_structure_dict = analyse_structure(
structure=final_structure,
mode="total",
diamond=diamond_flag
)
key_max = max(final_structure_dict.items(), key=operator.itemgetter(1))[0]
number_of_atoms = len(final_structure)
distribution_initial = final_structure_dict[key_max] / number_of_atoms
distribution_initial_half = distribution_initial / 2
return final_structure, key_max, number_of_atoms, distribution_initial_half, final_structure_dict
def get_press(ham, step=20):
"""
Args:
ham:
step:
Returns:
"""
return np.mean(ham['output/generic/pressures'][step:, :, :].diagonal(0, 2), axis=1)
def get_center_point(strain_result_lst=None, pressure_result_lst=None, center=None):
"""
Args:
strain_result_lst:
pressure_result_lst:
center:
Returns:
"""
if strain_result_lst is not None and len(strain_result_lst) != 0 and \
pressure_result_lst is not None and len(pressure_result_lst) != 0:
center_point = np.round(np.roots(np.polyfit(strain_result_lst, pressure_result_lst, 1))[0], 2)
elif center is not None:
center_point = center
else:
center_point = 1.0
return center_point
def get_strain_lst(fit_range=0.02, points=21, strain_result_lst=None, pressure_result_lst=None, center=None):
"""
Args:
fit_range:
points:
strain_result_lst:
pressure_result_lst:
center:
Returns:
"""
center_point = get_center_point(
strain_result_lst=strain_result_lst,
pressure_result_lst=pressure_result_lst,
center=center
)
return [np.round(s, 3) for s in np.linspace(center_point-fit_range, center_point+fit_range, points)]
def get_nve_job_name(temperature_next, strain, steps_lst, nve_run_time_steps):
"""
Args:
temperature_next:
strain:
steps_lst:
nve_run_time_steps:
Returns:
"""
temperature_next = round_temperature_next(temperature_next)
temp_str = str(temperature_next).replace('.', '_')
strain_str = str(strain).replace('.', '_')
steps_str = str(steps_lst.index(nve_run_time_steps))
return 'ham_nve_' + strain_str + '_' + temp_str + '_' + steps_str
def plot_solid_liquid_ratio(temperature_next, strain_lst, nve_run_time_steps, project_parameter, debug_plot=True):
"""
Args:
temperature_next:
strain_lst:
nve_run_time_steps:
project_parameter:
debug_plot:
Returns:
"""
cna_str = project_parameter['crystalstructure'].upper()
ratio_lst = []
for strain in strain_lst:
job_name = get_nve_job_name(
temperature_next=temperature_next,
strain=strain,
steps_lst=project_parameter['nve_run_time_steps_lst'],
nve_run_time_steps=nve_run_time_steps
)
ham_nve = project_parameter['project'].load(job_name)
struct = ham_nve.get_structure().center_coordinates_in_unit_cell()
cna = analyse_structure(
structure=struct,
mode="str",
diamond=project_parameter['crystalstructure'].lower() == 'diamond'
)
if not project_parameter['crystalstructure'].lower() == 'diamond':
bcc_count = sum(cna == 'BCC')
fcc_count = sum(cna == 'FCC')
hcp_count = sum(cna == 'HCP')
cond = (cna_str == 'BCC' and bcc_count > fcc_count and bcc_count > hcp_count) or \
(cna_str == 'FCC' and fcc_count > bcc_count and fcc_count > hcp_count) or \
(cna_str == 'HCP' and hcp_count > bcc_count and hcp_count > fcc_count)
else:
cna_str = 'Cubic diamond'
cond = sum(cna == cna_str) > 0.05 * len(struct)
if cond:
# plt.figure(figsize=(16,12))
bandwidth = (struct.get_volume()/len(struct))**(1.0/3.0)
kde = KernelDensity(kernel='gaussian',
bandwidth=bandwidth).fit(struct.positions[:, 2][cna == cna_str].reshape(-1, 1))
z_range = np.linspace(struct.positions[:, 2].min(), struct.positions[:, 2].max(), 1000)
sample = kde.score_samples(z_range.reshape(-1, 1))
gaussian_funct = np.exp(sample)/np.exp(sample).max()
z_range_above_limit = z_range[np.where(gaussian_funct > 0.1)]
z_range_below_limit = z_range[np.where(gaussian_funct < 0.1)]
if len(z_range_above_limit) != 0:
ratio_above = (np.max(z_range_above_limit)-np.min(z_range_above_limit)) / \
(np.max(z_range)-np.min(z_range))
else:
ratio_above = 1.0
if len(z_range_below_limit) != 0:
ratio_below = 1 - (np.max(z_range_below_limit)-np.min(z_range_below_limit)) / \
(np.max(z_range)-np.min(z_range))
else:
ratio_below = 0.0
if ratio_below == 0.0:
ratio = ratio_above
elif ratio_above == 1.0:
ratio = ratio_below
else:
ratio = np.min([ratio_below, ratio_above])
ratio_lst.append(ratio)
else:
z_range = None
gaussian_funct = None
z_range_above_limit = None
ratio = None
ratio_lst.append(0.0)
if debug_plot:
plt.title('strain: ' + str(strain))
plt.xlabel('position z')
plt.ylabel('position x')
plt.plot(struct.positions[:, 2], struct.positions[:, 0], 'o', label='all')
if not project_parameter['crystalstructure'].lower() == 'diamond':
plt.plot(struct.positions[:, 2][cna == 'BCC'], struct.positions[:, 0][cna == 'BCC'], 'x', label='BCC')
plt.plot(struct.positions[:, 2][cna == 'FCC'], struct.positions[:, 0][cna == 'FCC'], 'x', label='FCC')
plt.plot(struct.positions[:, 2][cna == 'HCP'], struct.positions[:, 0][cna == 'HCP'], 'x', label='HCP')
else:
plt.plot(
struct.positions[:, 2][cna == 'Cubic diamond'],
struct.positions[:, 0][cna == 'Cubic diamond'],
'x',
label='Cubic diamond'
)
plt.plot(
struct.positions[:, 2][cna == 'Cubic diamond (1st neighbor)'],
struct.positions[:, 0][cna == 'Cubic diamond (1st neighbor)'],
'x',
label='Cubic diamond (1st neighbor)'
)
plt.plot(
struct.positions[:, 2][cna == 'Cubic diamond (2nd neighbor)'],
struct.positions[:, 0][cna == 'Cubic diamond (2nd neighbor)'],
'x',
label='Cubic diamond (2nd neighbor)'
)
plt.plot(
struct.positions[:, 2][cna == 'Hexagonal diamond'],
struct.positions[:, 0][cna == 'Hexagonal diamond'],
'x',
label='Hexagonal diamond'
)
plt.plot(
struct.positions[:, 2][cna == 'Hexagonal diamond (1st neighbor)'],
struct.positions[:, 0][cna == 'Hexagonal diamond (1st neighbor)'],
'x',
label='Hexagonal diamond (1st neighbor)'
)
plt.plot(
struct.positions[:, 2][cna == 'Hexagonal diamond (2nd neighbor)'],
struct.positions[:, 0][cna == 'Hexagonal diamond (2nd neighbor)'],
'x',
label='Hexagonal diamond (2nd neighbor)'
)
cna_str_lst = struct.positions[:, 2][cna == cna_str]
if len(cna_str_lst) != 0:
plt.axvline(cna_str_lst.max(), color='red')
plt.axvline(cna_str_lst.min(), color='red')
plt.legend()
plt.show()
plt.xlabel('Position in z')
plt.ylabel('kernel density score')
plt.title('strain: ' + str(strain))
if z_range is not None:
plt.plot(z_range, gaussian_funct, label=cna_str)
plt.axvline(np.min(z_range_above_limit), color='black', linestyle='--', label='ratio: ' + str(ratio))
plt.axvline(np.max(z_range_above_limit), color='black', linestyle='--')
plt.axhline(0.1, color='red')
plt.legend()
plt.show()
return ratio_lst
def ratio_selection(strain_lst, ratio_lst, pressure_lst, temperature_lst, ratio_boundary, debug_plot=True):
"""
Args:
strain_lst:
ratio_lst:
pressure_lst:
temperature_lst:
ratio_boundary:
debug_plot:
Returns:
"""
if debug_plot:
plt.plot(strain_lst, ratio_lst)
plt.axhline(0.5 + ratio_boundary, color='red', linestyle='--')
plt.axhline(0.5, color='black', linestyle='--')
plt.axhline(0.5 - ratio_boundary, color='red', linestyle='--')
plt.xlabel('Strain')
plt.ylabel('ratio solid vs. liquid')
rat_lst, rat_col_lst = [], []
for rat in ratio_lst:
if (0.5 - ratio_boundary) < rat < (0.5 + ratio_boundary):
rat_lst.append(rat)
elif len(rat_lst) != 0:
rat_col_lst.append(rat_lst)
rat_lst = []
if len(rat_lst) != 0:
rat_col_lst.append(rat_lst)
if len(rat_col_lst) != 0:
rat_max_ind = np.argmax([len(l) for l in rat_col_lst])
ratio_ind = [r in rat_col_lst[rat_max_ind] for r in ratio_lst]
strain_value_lst = np.array(strain_lst)[ratio_ind]
ratio_value_lst = np.array(ratio_lst)[ratio_ind]
pressure_value_lst = np.array(pressure_lst)[ratio_ind]
temperature_value_lst = np.array(temperature_lst)[ratio_ind]
if debug_plot:
plt.axvline(np.min(strain_value_lst), color='blue', linestyle='--')
plt.axvline(np.max(strain_value_lst), color='blue', linestyle='--')
plt.show()
if np.mean(ratio_value_lst) > 0.5:
return strain_value_lst, ratio_value_lst, pressure_value_lst, temperature_value_lst, 1
else:
return strain_value_lst, ratio_value_lst, pressure_value_lst, temperature_value_lst, -1
else:
if np.mean(ratio_lst) > 0.5:
return [], [], [], [], 1
else:
return [], [], [], [], -1
def plot_equilibration(temperature_next, strain_lst, nve_run_time_steps, project_parameter, debug_plot=True):
"""
Args:
temperature_next:
strain_lst:
nve_run_time_steps:
project_parameter:
debug_plot:
Returns:
"""
if debug_plot:
for strain in strain_lst:
job_name = get_nve_job_name(
temperature_next=temperature_next,
strain=strain,
steps_lst=project_parameter['nve_run_time_steps_lst'],
nve_run_time_steps=nve_run_time_steps
)
ham_nve = project_parameter['project'].load(job_name)
plt.plot(ham_nve['output/generic/temperature'], label='strain: ' + str(strain))
plt.axhline(np.mean(ham_nve['output/generic/temperature'][-20:]), linestyle='--', color='red')
plt.axvline(range(len(ham_nve['output/generic/temperature']))[-20], linestyle='--', color='black')
plt.legend()
plt.xlabel('timestep')
plt.ylabel('Temperature K')
plt.legend()
plt.show()
def plot_melting_point_prediction(strain_value_lst, pressure_value_lst, temperature_value_lst, boundary_value=0.25,
debug_plot=True):
"""
Args:
strain_value_lst:
pressure_value_lst:
temperature_value_lst:
boundary_value:
debug_plot:
Returns:
"""
fit_press = np.poly1d(np.polyfit(strain_value_lst, pressure_value_lst, 1))
fit_temp = np.poly1d(np.polyfit(strain_value_lst, temperature_value_lst, 1))
fit_temp_from_press = np.poly1d(np.polyfit(pressure_value_lst, temperature_value_lst, 1))
fit_combined = np.poly1d(np.polyfit(fit_press(strain_value_lst), fit_temp(strain_value_lst), 1))
if debug_plot: