-
Notifications
You must be signed in to change notification settings - Fork 1
/
force_env_methods.F
2092 lines (1887 loc) · 106 KB
/
force_env_methods.F
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
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Interface for the force calculations
!> \par History
!> cjm, FEB-20-2001: pass variable box_ref
!> cjm, SEPT-12-2002: major reorganization
!> fawzi, APR-12-2003: introduced force_env (based on the work by CJM&JGH)
!> fawzi, NOV-3-2004: reorganized interface for f77 interface
!> \author fawzi
! **************************************************************************************************
MODULE force_env_methods
USE atprop_types, ONLY: atprop_init,&
atprop_type
USE bibliography, ONLY: Heaton_Burgess2007,&
Huang2011,&
cite_reference
USE cell_methods, ONLY: cell_create,&
init_cell
USE cell_types, ONLY: cell_clone,&
cell_release,&
cell_sym_triclinic,&
cell_type,&
real_to_scaled,&
scaled_to_real
USE constraint_fxd, ONLY: fix_atom_control
USE constraint_vsite, ONLY: vsite_force_control
USE cp_control_types, ONLY: dft_control_type
USE cp_fm_basic_linalg, ONLY: cp_fm_scale_and_add
USE cp_fm_types, ONLY: cp_fm_copy_general
USE cp_iter_types, ONLY: cp_iteration_info_copy_iter
USE cp_log_handling, ONLY: cp_add_default_logger,&
cp_get_default_logger,&
cp_logger_type,&
cp_rm_default_logger,&
cp_to_string
USE cp_output_handling, ONLY: cp_print_key_finished_output,&
cp_print_key_unit_nr,&
low_print_level
USE cp_result_methods, ONLY: cp_results_erase,&
cp_results_mp_bcast,&
get_results,&
test_for_result
USE cp_result_types, ONLY: cp_result_copy,&
cp_result_create,&
cp_result_p_type,&
cp_result_release,&
cp_result_type
USE cp_subsys_types, ONLY: cp_subsys_get,&
cp_subsys_p_type,&
cp_subsys_set,&
cp_subsys_type
USE cp_units, ONLY: cp_unit_from_cp2k
USE eip_environment_types, ONLY: eip_environment_type
USE eip_silicon, ONLY: eip_bazant,&
eip_lenosky
USE embed_types, ONLY: embed_env_type,&
opt_dmfet_pot_type,&
opt_embed_pot_type
USE external_potential_methods, ONLY: add_external_potential
USE fist_environment_types, ONLY: fist_environment_type
USE fist_force, ONLY: fist_calc_energy_force
USE force_env_types, ONLY: &
force_env_get, force_env_get_natom, force_env_p_type, force_env_set, force_env_type, &
use_eip_force, use_embed, use_fist_force, use_ipi, use_mixed_force, use_nnp_force, &
use_prog_name, use_pwdft_force, use_qmmm, use_qmmmx, use_qs_force
USE force_env_utils, ONLY: rescale_forces,&
write_atener,&
write_forces
USE force_fields_util, ONLY: get_generic_info
USE fp_methods, ONLY: fp_eval
USE fparser, ONLY: EvalErrType,&
evalf,&
evalfd,&
finalizef,&
initf,&
parsef
USE global_types, ONLY: global_environment_type,&
globenv_retain
USE grrm_utils, ONLY: write_grrm
USE input_constants, ONLY: &
debug_run, dfet, dmfet, mix_cdft, mix_coupled, mix_generic, mix_linear_combination, &
mix_minimum, mix_restrained, mixed_cdft_serial, use_bazant_eip, use_lenosky_eip
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_retain,&
section_vals_type,&
section_vals_val_get
USE ipi_environment_types, ONLY: ipi_environment_type
USE ipi_server, ONLY: request_forces
USE kahan_sum, ONLY: accurate_sum
USE kinds, ONLY: default_path_length,&
default_string_length,&
dp
USE machine, ONLY: m_memory
USE mathlib, ONLY: abnormal_value
USE message_passing, ONLY: mp_para_env_type
USE metadynamics_types, ONLY: meta_env_type
USE mixed_cdft_methods, ONLY: mixed_cdft_build_weight,&
mixed_cdft_calculate_coupling,&
mixed_cdft_init
USE mixed_energy_types, ONLY: mixed_energy_type,&
mixed_force_type
USE mixed_environment_types, ONLY: get_mixed_env,&
mixed_environment_type
USE mixed_environment_utils, ONLY: get_subsys_map_index,&
mixed_map_forces
USE molecule_kind_list_types, ONLY: molecule_kind_list_type
USE molecule_kind_types, ONLY: get_molecule_kind,&
molecule_kind_type
USE nnp_environment_types, ONLY: nnp_type
USE nnp_force, ONLY: nnp_calc_energy_force
USE optimize_dmfet_potential, ONLY: build_full_dm,&
check_dmfet,&
prepare_dmfet_opt,&
release_dmfet_opt,&
subsys_spin
USE optimize_embedding_potential, ONLY: &
Coulomb_guess, calculate_embed_pot_grad, conv_check_embed, get_max_subsys_diff, &
get_prev_density, init_embed_pot, make_subsys_embed_pot, opt_embed_step, &
prepare_embed_opt, print_emb_opt_info, print_embed_restart, print_pot_simple_grid, &
print_rho_diff, print_rho_spin_diff, read_embed_pot, release_opt_embed, step_control, &
understand_spin_states
USE particle_list_types, ONLY: particle_list_p_type,&
particle_list_type
USE physcon, ONLY: debye
USE pw_env_types, ONLY: pw_env_get,&
pw_env_type
USE pw_methods, ONLY: pw_axpy,&
pw_copy,&
pw_integral_ab,&
pw_zero
USE pw_pool_types, ONLY: pw_pool_type
USE pw_types, ONLY: pw_r3d_rs_type
USE pwdft_environment, ONLY: pwdft_calc_energy_force
USE pwdft_environment_types, ONLY: pwdft_environment_type
USE qmmm_force, ONLY: qmmm_calc_energy_force
USE qmmm_types, ONLY: qmmm_env_type
USE qmmm_util, ONLY: apply_qmmm_translate
USE qmmmx_force, ONLY: qmmmx_calc_energy_force
USE qmmmx_types, ONLY: qmmmx_env_type
USE qs_energy_types, ONLY: qs_energy_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type,&
set_qs_env
USE qs_force, ONLY: qs_calc_energy_force
USE qs_rho_types, ONLY: qs_rho_get,&
qs_rho_type
USE restraint, ONLY: restraint_control
USE scine_utils, ONLY: write_scine
USE string_utilities, ONLY: compress
USE virial_methods, ONLY: write_stress_tensor,&
write_stress_tensor_components
USE virial_types, ONLY: symmetrize_virial,&
virial_p_type,&
virial_type,&
zero_virial
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'force_env_methods'
PUBLIC :: force_env_create, &
force_env_calc_energy_force, &
force_env_calc_num_pressure
INTEGER, SAVE, PRIVATE :: last_force_env_id = 0
CONTAINS
! **************************************************************************************************
!> \brief Interface routine for force and energy calculations
!> \param force_env the force_env of which you want the energy and forces
!> \param calc_force if false the forces *might* be left unchanged
!> or be invalid, no guarantees can be given. Defaults to true
!> \param consistent_energies Performs an additional qs_ks_update_qs_env, so
!> that the energies are appropriate to the forces, they are in the
!> non-selfconsistent case not consistent to each other! [08.2005, TdK]
!> \param skip_external_control ...
!> \param eval_energy_forces ...
!> \param require_consistent_energy_force ...
!> \param linres ...
!> \param calc_stress_tensor ...
!> \author CJM & fawzi
! **************************************************************************************************
RECURSIVE SUBROUTINE force_env_calc_energy_force(force_env, calc_force, &
consistent_energies, skip_external_control, eval_energy_forces, &
require_consistent_energy_force, linres, calc_stress_tensor)
TYPE(force_env_type), POINTER :: force_env
LOGICAL, INTENT(IN), OPTIONAL :: calc_force, consistent_energies, skip_external_control, &
eval_energy_forces, require_consistent_energy_force, linres, calc_stress_tensor
REAL(kind=dp), PARAMETER :: ateps = 1.0E-6_dp
CHARACTER(LEN=default_string_length) :: unit_string
INTEGER :: ikind, nat, ndigits, nfixed_atoms, &
nfixed_atoms_total, nkind, &
output_unit, print_forces, print_grrm, &
print_scine
LOGICAL :: calculate_forces, calculate_stress_tensor, energy_consistency, eval_ef, &
linres_run, my_skip, print_components
REAL(KIND=dp) :: checksum, e_entropy, e_gap, e_pot, &
fconv, sum_energy
REAL(KIND=dp), DIMENSION(3) :: grand_total_force, total_force
TYPE(atprop_type), POINTER :: atprop_env
TYPE(cell_type), POINTER :: cell
TYPE(cp_logger_type), POINTER :: logger
TYPE(cp_subsys_type), POINTER :: subsys
TYPE(molecule_kind_list_type), POINTER :: molecule_kinds
TYPE(molecule_kind_type), DIMENSION(:), POINTER :: molecule_kind_set
TYPE(molecule_kind_type), POINTER :: molecule_kind
TYPE(particle_list_type), POINTER :: core_particles, particles, &
shell_particles
TYPE(virial_type), POINTER :: virial
NULLIFY (logger, virial, subsys, atprop_env, cell)
logger => cp_get_default_logger()
eval_ef = .TRUE.
my_skip = .FALSE.
calculate_forces = .TRUE.
energy_consistency = .FALSE.
linres_run = .FALSE.
e_gap = -1.0_dp
e_entropy = -1.0_dp
unit_string = ""
IF (PRESENT(eval_energy_forces)) eval_ef = eval_energy_forces
IF (PRESENT(skip_external_control)) my_skip = skip_external_control
IF (PRESENT(calc_force)) calculate_forces = calc_force
IF (PRESENT(calc_stress_tensor)) THEN
calculate_stress_tensor = calc_stress_tensor
ELSE
calculate_stress_tensor = calculate_forces
END IF
IF (PRESENT(consistent_energies)) energy_consistency = consistent_energies
IF (PRESENT(linres)) linres_run = linres
CPASSERT(ASSOCIATED(force_env))
CPASSERT(force_env%ref_count > 0)
CALL force_env_get(force_env, subsys=subsys)
CALL force_env_set(force_env, additional_potential=0.0_dp)
CALL cp_subsys_get(subsys, virial=virial, atprop=atprop_env, cell=cell)
IF (virial%pv_availability) CALL zero_virial(virial, reset=.FALSE.)
nat = force_env_get_natom(force_env)
CALL atprop_init(atprop_env, nat)
IF (eval_ef) THEN
SELECT CASE (force_env%in_use)
CASE (use_fist_force)
CALL fist_calc_energy_force(force_env%fist_env)
CASE (use_qs_force)
CALL qs_calc_energy_force(force_env%qs_env, calculate_forces, energy_consistency, linres_run)
CASE (use_pwdft_force)
IF (virial%pv_availability .AND. calculate_stress_tensor) THEN
CALL pwdft_calc_energy_force(force_env%pwdft_env, calculate_forces,.NOT. virial%pv_numer)
ELSE
CALL pwdft_calc_energy_force(force_env%pwdft_env, calculate_forces, .FALSE.)
END IF
e_gap = force_env%pwdft_env%energy%band_gap
e_entropy = force_env%pwdft_env%energy%entropy
CASE (use_eip_force)
IF (force_env%eip_env%eip_model == use_lenosky_eip) THEN
CALL eip_lenosky(force_env%eip_env)
ELSE IF (force_env%eip_env%eip_model == use_bazant_eip) THEN
CALL eip_bazant(force_env%eip_env)
END IF
CASE (use_qmmm)
CALL qmmm_calc_energy_force(force_env%qmmm_env, &
calculate_forces, energy_consistency, linres=linres_run)
CASE (use_qmmmx)
CALL qmmmx_calc_energy_force(force_env%qmmmx_env, &
calculate_forces, energy_consistency, linres=linres_run, &
require_consistent_energy_force=require_consistent_energy_force)
CASE (use_mixed_force)
CALL mixed_energy_forces(force_env, calculate_forces)
CASE (use_nnp_force)
CALL nnp_calc_energy_force(force_env%nnp_env, &
calculate_forces)
CASE (use_embed)
CALL embed_energy(force_env)
CASE (use_ipi)
CALL request_forces(force_env%ipi_env)
CASE default
CPABORT("")
END SELECT
END IF
! In case it is requested, we evaluate the stress tensor numerically
IF (virial%pv_availability) THEN
IF (virial%pv_numer .AND. calculate_stress_tensor) THEN
! Compute the numerical stress tensor
CALL force_env_calc_num_pressure(force_env)
ELSE
IF (calculate_forces) THEN
! Symmetrize analytical stress tensor
CALL symmetrize_virial(virial)
ELSE
IF (calculate_stress_tensor) THEN
CALL cp_warn(__LOCATION__, "The calculation of the stress tensor "// &
"requires the calculation of the forces")
END IF
END IF
END IF
END IF
!sample peak memory
CALL m_memory()
! Some additional tasks..
IF (.NOT. my_skip) THEN
! Flexible Partitioning
IF (ASSOCIATED(force_env%fp_env)) THEN
IF (force_env%fp_env%use_fp) THEN
CALL fp_eval(force_env%fp_env, subsys, cell)
END IF
END IF
! Constraints ONLY of Fixed Atom type
CALL fix_atom_control(force_env)
! All Restraints
CALL restraint_control(force_env)
! Virtual Sites
CALL vsite_force_control(force_env)
! External Potential
CALL add_external_potential(force_env)
! Rescale forces if requested
CALL rescale_forces(force_env)
END IF
CALL force_env_get(force_env, potential_energy=e_pot)
! Print energy always in the same format for all methods
output_unit = cp_print_key_unit_nr(logger, force_env%force_env_section, "PRINT%PROGRAM_RUN_INFO", &
extension=".Log")
IF (output_unit > 0) THEN
CALL section_vals_val_get(force_env%force_env_section, "PRINT%PROGRAM_RUN_INFO%ENERGY_UNIT", &
c_val=unit_string)
fconv = cp_unit_from_cp2k(1.0_dp, TRIM(ADJUSTL(unit_string)))
WRITE (UNIT=output_unit, FMT="(/,T2,A,T55,F26.15)") &
"ENERGY| Total FORCE_EVAL ( "//TRIM(ADJUSTL(use_prog_name(force_env%in_use)))// &
" ) energy ["//TRIM(ADJUSTL(unit_string))//"]", e_pot*fconv
IF (e_gap > -0.1_dp) THEN
WRITE (UNIT=output_unit, FMT="(/,T2,A,T55,F26.15)") &
"ENERGY| Total FORCE_EVAL ( "//TRIM(ADJUSTL(use_prog_name(force_env%in_use)))// &
" ) gap ["//TRIM(ADJUSTL(unit_string))//"]", e_gap*fconv
END IF
IF (e_entropy > -0.1_dp) THEN
WRITE (UNIT=output_unit, FMT="(/,T2,A,T55,F26.15)") &
"ENERGY| Total FORCE_EVAL ( "//TRIM(ADJUSTL(use_prog_name(force_env%in_use)))// &
" ) free energy ["//TRIM(ADJUSTL(unit_string))//"]", (e_pot - e_entropy)*fconv
END IF
END IF
CALL cp_print_key_finished_output(output_unit, logger, force_env%force_env_section, &
"PRINT%PROGRAM_RUN_INFO")
! terminate the run if the value of the potential is abnormal
IF (abnormal_value(e_pot)) &
CPABORT("Potential energy is an abnormal value (NaN/Inf).")
! Print forces, if requested
print_forces = cp_print_key_unit_nr(logger, force_env%force_env_section, "PRINT%FORCES", &
extension=".xyz")
IF ((print_forces > 0) .AND. calculate_forces) THEN
CALL force_env_get(force_env, subsys=subsys)
CALL cp_subsys_get(subsys, &
core_particles=core_particles, &
particles=particles, &
shell_particles=shell_particles)
! Variable precision output of the forces
CALL section_vals_val_get(force_env%force_env_section, "PRINT%FORCES%NDIGITS", &
i_val=ndigits)
CALL section_vals_val_get(force_env%force_env_section, "PRINT%FORCES%FORCE_UNIT", &
c_val=unit_string)
IF (ASSOCIATED(core_particles) .OR. ASSOCIATED(shell_particles)) THEN
CALL write_forces(particles, print_forces, "Atomic", ndigits, unit_string, &
total_force, zero_force_core_shell_atom=.TRUE.)
grand_total_force(1:3) = total_force(1:3)
IF (ASSOCIATED(core_particles)) THEN
CALL write_forces(core_particles, print_forces, "Core particle", ndigits, &
unit_string, total_force, zero_force_core_shell_atom=.FALSE.)
grand_total_force(:) = grand_total_force(:) + total_force(:)
END IF
IF (ASSOCIATED(shell_particles)) THEN
CALL write_forces(shell_particles, print_forces, "Shell particle", ndigits, &
unit_string, total_force, zero_force_core_shell_atom=.FALSE., &
grand_total_force=grand_total_force)
END IF
ELSE
CALL write_forces(particles, print_forces, "Atomic", ndigits, unit_string, total_force)
END IF
END IF
CALL cp_print_key_finished_output(print_forces, logger, force_env%force_env_section, "PRINT%FORCES")
! Write stress tensor
IF (virial%pv_availability) THEN
! If the virial is defined but we are not computing forces let's zero the
! virial for consistency
IF (calculate_forces .AND. calculate_stress_tensor) THEN
output_unit = cp_print_key_unit_nr(logger, force_env%force_env_section, "PRINT%STRESS_TENSOR", &
extension=".stress_tensor")
IF (output_unit > 0) THEN
CALL section_vals_val_get(force_env%force_env_section, "PRINT%STRESS_TENSOR%COMPONENTS", &
l_val=print_components)
CALL section_vals_val_get(force_env%force_env_section, "PRINT%STRESS_TENSOR%STRESS_UNIT", &
c_val=unit_string)
IF (print_components) THEN
IF ((.NOT. virial%pv_numer) .AND. (force_env%in_use == use_qs_force)) THEN
CALL write_stress_tensor_components(virial, output_unit, cell, unit_string)
END IF
END IF
CALL write_stress_tensor(virial%pv_virial, output_unit, cell, unit_string, virial%pv_numer)
END IF
CALL cp_print_key_finished_output(output_unit, logger, force_env%force_env_section, &
"PRINT%STRESS_TENSOR")
ELSE
CALL zero_virial(virial, reset=.FALSE.)
END IF
ELSE
output_unit = cp_print_key_unit_nr(logger, force_env%force_env_section, "PRINT%STRESS_TENSOR", &
extension=".stress_tensor")
IF (output_unit > 0) THEN
CALL cp_warn(__LOCATION__, "To print the stress tensor switch on the "// &
"virial evaluation with the keyword: STRESS_TENSOR")
END IF
CALL cp_print_key_finished_output(output_unit, logger, force_env%force_env_section, &
"PRINT%STRESS_TENSOR")
END IF
! Atomic energy
output_unit = cp_print_key_unit_nr(logger, force_env%force_env_section, "PRINT%PROGRAM_RUN_INFO", &
extension=".Log")
IF (atprop_env%energy) THEN
CALL force_env%para_env%sum(atprop_env%atener)
CALL force_env_get(force_env, potential_energy=e_pot)
IF (output_unit > 0) THEN
IF (logger%iter_info%print_level >= low_print_level) THEN
CALL cp_subsys_get(subsys=subsys, particles=particles)
CALL write_atener(output_unit, particles, atprop_env%atener, "Mulliken Atomic Energies")
END IF
sum_energy = accurate_sum(atprop_env%atener(:))
checksum = ABS(e_pot - sum_energy)
WRITE (UNIT=output_unit, FMT="(/,(T2,A,T56,F25.13))") &
"Potential energy (Atomic):", sum_energy, &
"Potential energy (Total) :", e_pot, &
"Difference :", checksum
CPASSERT((checksum < ateps*ABS(e_pot)))
END IF
CALL cp_print_key_finished_output(output_unit, logger, force_env%force_env_section, &
"PRINT%PROGRAM_RUN_INFO")
END IF
! Print GRMM interface file
print_grrm = cp_print_key_unit_nr(logger, force_env%force_env_section, "PRINT%GRRM", &
file_position="REWIND", extension=".rrm")
IF (print_grrm > 0) THEN
CALL force_env_get(force_env, subsys=subsys)
CALL cp_subsys_get(subsys=subsys, particles=particles, &
molecule_kinds=molecule_kinds)
! Count the number of fixed atoms
nfixed_atoms_total = 0
nkind = molecule_kinds%n_els
molecule_kind_set => molecule_kinds%els
DO ikind = 1, nkind
molecule_kind => molecule_kind_set(ikind)
CALL get_molecule_kind(molecule_kind, nfixd=nfixed_atoms)
nfixed_atoms_total = nfixed_atoms_total + nfixed_atoms
END DO
!
CALL write_grrm(print_grrm, force_env, particles%els, e_pot, fixed_atoms=nfixed_atoms_total)
END IF
CALL cp_print_key_finished_output(print_grrm, logger, force_env%force_env_section, "PRINT%GRRM")
print_scine = cp_print_key_unit_nr(logger, force_env%force_env_section, "PRINT%SCINE", &
file_position="REWIND", extension=".scine")
IF (print_scine > 0) THEN
CALL force_env_get(force_env, subsys=subsys)
CALL cp_subsys_get(subsys=subsys, particles=particles)
!
CALL write_scine(print_scine, force_env, particles%els, e_pot)
END IF
CALL cp_print_key_finished_output(print_scine, logger, force_env%force_env_section, "PRINT%SCINE")
END SUBROUTINE force_env_calc_energy_force
! **************************************************************************************************
!> \brief Evaluates the stress tensor and pressure numerically
!> \param force_env ...
!> \param dx ...
!> \par History
!> 10.2005 created [JCS]
!> 05.2009 Teodoro Laino [tlaino] - rewriting for general force_env
!>
!> \author JCS
! **************************************************************************************************
SUBROUTINE force_env_calc_num_pressure(force_env, dx)
TYPE(force_env_type), POINTER :: force_env
REAL(KIND=dp), INTENT(IN), OPTIONAL :: dx
REAL(kind=dp), PARAMETER :: default_dx = 0.001_dp
CHARACTER(LEN=default_string_length) :: unit_string
INTEGER :: i, ip, iq, j, k, natom, ncore, nshell, &
output_unit, symmetry_id
REAL(KIND=dp) :: dx_w
REAL(KIND=dp), DIMENSION(2) :: numer_energy
REAL(KIND=dp), DIMENSION(3) :: s
REAL(KIND=dp), DIMENSION(3, 3) :: numer_stress
REAL(KIND=dp), DIMENSION(:, :), POINTER :: ref_pos_atom, ref_pos_core, ref_pos_shell
TYPE(cell_type), POINTER :: cell, cell_local
TYPE(cp_logger_type), POINTER :: logger
TYPE(cp_subsys_type), POINTER :: subsys
TYPE(global_environment_type), POINTER :: globenv
TYPE(particle_list_type), POINTER :: core_particles, particles, &
shell_particles
TYPE(virial_type), POINTER :: virial
NULLIFY (cell_local)
NULLIFY (core_particles)
NULLIFY (particles)
NULLIFY (shell_particles)
NULLIFY (ref_pos_atom)
NULLIFY (ref_pos_core)
NULLIFY (ref_pos_shell)
natom = 0
ncore = 0
nshell = 0
numer_stress = 0.0_dp
logger => cp_get_default_logger()
dx_w = default_dx
IF (PRESENT(dx)) dx_w = dx
CALL force_env_get(force_env, subsys=subsys, globenv=globenv)
CALL cp_subsys_get(subsys, &
core_particles=core_particles, &
particles=particles, &
shell_particles=shell_particles, &
virial=virial)
output_unit = cp_print_key_unit_nr(logger, force_env%force_env_section, "PRINT%STRESS_TENSOR", &
extension=".stress_tensor")
IF (output_unit > 0) THEN
WRITE (output_unit, "(/A,A/)") " **************************** ", &
"NUMERICAL STRESS ********************************"
END IF
! Save all original particle positions
natom = particles%n_els
ALLOCATE (ref_pos_atom(natom, 3))
DO i = 1, natom
ref_pos_atom(i, :) = particles%els(i)%r
END DO
IF (ASSOCIATED(core_particles)) THEN
ncore = core_particles%n_els
ALLOCATE (ref_pos_core(ncore, 3))
DO i = 1, ncore
ref_pos_core(i, :) = core_particles%els(i)%r
END DO
END IF
IF (ASSOCIATED(shell_particles)) THEN
nshell = shell_particles%n_els
ALLOCATE (ref_pos_shell(nshell, 3))
DO i = 1, nshell
ref_pos_shell(i, :) = shell_particles%els(i)%r
END DO
END IF
CALL force_env_get(force_env, cell=cell)
! Save cell symmetry (distorted cell has no symmetry)
symmetry_id = cell%symmetry_id
cell%symmetry_id = cell_sym_triclinic
!
CALL cell_create(cell_local)
CALL cell_clone(cell, cell_local)
! First change box
DO ip = 1, 3
DO iq = 1, 3
IF (virial%pv_diagonal .AND. (ip /= iq)) CYCLE
DO k = 1, 2
cell%hmat(ip, iq) = cell_local%hmat(ip, iq) - (-1.0_dp)**k*dx_w
CALL init_cell(cell)
! Scale positions
DO i = 1, natom
CALL real_to_scaled(s, ref_pos_atom(i, 1:3), cell_local)
CALL scaled_to_real(particles%els(i)%r, s, cell)
END DO
DO i = 1, ncore
CALL real_to_scaled(s, ref_pos_core(i, 1:3), cell_local)
CALL scaled_to_real(core_particles%els(i)%r, s, cell)
END DO
DO i = 1, nshell
CALL real_to_scaled(s, ref_pos_shell(i, 1:3), cell_local)
CALL scaled_to_real(shell_particles%els(i)%r, s, cell)
END DO
! Compute energies
CALL force_env_calc_energy_force(force_env, &
calc_force=.FALSE., &
consistent_energies=.TRUE., &
calc_stress_tensor=.FALSE.)
CALL force_env_get(force_env, potential_energy=numer_energy(k))
! Reset cell
cell%hmat(ip, iq) = cell_local%hmat(ip, iq)
END DO
CALL init_cell(cell)
numer_stress(ip, iq) = 0.5_dp*(numer_energy(1) - numer_energy(2))/dx_w
IF (output_unit > 0) THEN
IF (globenv%run_type_id == debug_run) THEN
WRITE (UNIT=output_unit, FMT="(/,T2,A,T19,A,F7.4,A,T44,A,F7.4,A,T69,A)") &
"DEBUG|", "E("//ACHAR(119 + ip)//ACHAR(119 + iq)//" +", dx_w, ")", &
"E("//ACHAR(119 + ip)//ACHAR(119 + iq)//" -", dx_w, ")", &
"f(numerical)"
WRITE (UNIT=output_unit, FMT="(T2,A,2(1X,F24.8),1X,F22.8)") &
"DEBUG|", numer_energy(1:2), numer_stress(ip, iq)
ELSE
WRITE (UNIT=output_unit, FMT="(/,T7,A,F7.4,A,T27,A,F7.4,A,T49,A)") &
"E("//ACHAR(119 + ip)//ACHAR(119 + iq)//" +", dx_w, ")", &
"E("//ACHAR(119 + ip)//ACHAR(119 + iq)//" -", dx_w, ")", &
"f(numerical)"
WRITE (UNIT=output_unit, FMT="(3(1X,F19.8))") &
numer_energy(1:2), numer_stress(ip, iq)
END IF
END IF
END DO
END DO
! Reset positions and rebuild original environment
cell%symmetry_id = symmetry_id
CALL init_cell(cell)
DO i = 1, natom
particles%els(i)%r = ref_pos_atom(i, :)
END DO
DO i = 1, ncore
core_particles%els(i)%r = ref_pos_core(i, :)
END DO
DO i = 1, nshell
shell_particles%els(i)%r = ref_pos_shell(i, :)
END DO
CALL force_env_calc_energy_force(force_env, &
calc_force=.FALSE., &
consistent_energies=.TRUE., &
calc_stress_tensor=.FALSE.)
! Computing pv_test
virial%pv_virial = 0.0_dp
DO i = 1, 3
DO j = 1, 3
DO k = 1, 3
virial%pv_virial(i, j) = virial%pv_virial(i, j) - &
0.5_dp*(numer_stress(i, k)*cell_local%hmat(j, k) + &
numer_stress(j, k)*cell_local%hmat(i, k))
END DO
END DO
END DO
IF (output_unit > 0) THEN
IF (globenv%run_type_id == debug_run) THEN
CALL section_vals_val_get(force_env%force_env_section, "PRINT%FORCES%FORCE_UNIT", &
c_val=unit_string)
CALL write_stress_tensor(virial%pv_virial, output_unit, cell, unit_string, virial%pv_numer)
END IF
WRITE (output_unit, "(/,A,/)") " **************************** "// &
"NUMERICAL STRESS END *****************************"
END IF
CALL cp_print_key_finished_output(output_unit, logger, force_env%force_env_section, &
"PRINT%STRESS_TENSOR")
! Release storage
IF (ASSOCIATED(ref_pos_atom)) THEN
DEALLOCATE (ref_pos_atom)
END IF
IF (ASSOCIATED(ref_pos_core)) THEN
DEALLOCATE (ref_pos_core)
END IF
IF (ASSOCIATED(ref_pos_shell)) THEN
DEALLOCATE (ref_pos_shell)
END IF
IF (ASSOCIATED(cell_local)) CALL cell_release(cell_local)
END SUBROUTINE force_env_calc_num_pressure
! **************************************************************************************************
!> \brief creates and initializes a force environment
!> \param force_env the force env to create
!> \param root_section ...
!> \param para_env ...
!> \param globenv ...
!> \param fist_env , qs_env: exactly one of these should be
!> associated, the one that is active
!> \param qs_env ...
!> \param meta_env ...
!> \param sub_force_env ...
!> \param qmmm_env ...
!> \param qmmmx_env ...
!> \param eip_env ...
!> \param pwdft_env ...
!> \param force_env_section ...
!> \param mixed_env ...
!> \param embed_env ...
!> \param nnp_env ...
!> \param ipi_env ...
!> \par History
!> 04.2003 created [fawzi]
!> \author fawzi
! **************************************************************************************************
SUBROUTINE force_env_create(force_env, root_section, para_env, globenv, fist_env, &
qs_env, meta_env, sub_force_env, qmmm_env, qmmmx_env, eip_env, pwdft_env, force_env_section, &
mixed_env, embed_env, nnp_env, ipi_env)
TYPE(force_env_type), POINTER :: force_env
TYPE(section_vals_type), POINTER :: root_section
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(global_environment_type), POINTER :: globenv
TYPE(fist_environment_type), OPTIONAL, POINTER :: fist_env
TYPE(qs_environment_type), OPTIONAL, POINTER :: qs_env
TYPE(meta_env_type), OPTIONAL, POINTER :: meta_env
TYPE(force_env_p_type), DIMENSION(:), OPTIONAL, &
POINTER :: sub_force_env
TYPE(qmmm_env_type), OPTIONAL, POINTER :: qmmm_env
TYPE(qmmmx_env_type), OPTIONAL, POINTER :: qmmmx_env
TYPE(eip_environment_type), OPTIONAL, POINTER :: eip_env
TYPE(pwdft_environment_type), OPTIONAL, POINTER :: pwdft_env
TYPE(section_vals_type), POINTER :: force_env_section
TYPE(mixed_environment_type), OPTIONAL, POINTER :: mixed_env
TYPE(embed_env_type), OPTIONAL, POINTER :: embed_env
TYPE(nnp_type), OPTIONAL, POINTER :: nnp_env
TYPE(ipi_environment_type), OPTIONAL, POINTER :: ipi_env
ALLOCATE (force_env)
NULLIFY (force_env%fist_env, force_env%qs_env, &
force_env%para_env, force_env%globenv, &
force_env%meta_env, force_env%sub_force_env, &
force_env%qmmm_env, force_env%qmmmx_env, force_env%fp_env, &
force_env%force_env_section, force_env%eip_env, force_env%mixed_env, &
force_env%embed_env, force_env%pwdft_env, force_env%nnp_env, &
force_env%root_section)
last_force_env_id = last_force_env_id + 1
force_env%ref_count = 1
force_env%in_use = 0
force_env%additional_potential = 0.0_dp
force_env%globenv => globenv
CALL globenv_retain(force_env%globenv)
force_env%root_section => root_section
CALL section_vals_retain(root_section)
force_env%para_env => para_env
CALL force_env%para_env%retain()
CALL section_vals_retain(force_env_section)
force_env%force_env_section => force_env_section
IF (PRESENT(fist_env)) THEN
CPASSERT(ASSOCIATED(fist_env))
CPASSERT(force_env%in_use == 0)
force_env%in_use = use_fist_force
force_env%fist_env => fist_env
END IF
IF (PRESENT(eip_env)) THEN
CPASSERT(ASSOCIATED(eip_env))
CPASSERT(force_env%in_use == 0)
force_env%in_use = use_eip_force
force_env%eip_env => eip_env
END IF
IF (PRESENT(pwdft_env)) THEN
CPASSERT(ASSOCIATED(pwdft_env))
CPASSERT(force_env%in_use == 0)
force_env%in_use = use_pwdft_force
force_env%pwdft_env => pwdft_env
END IF
IF (PRESENT(qs_env)) THEN
CPASSERT(ASSOCIATED(qs_env))
CPASSERT(force_env%in_use == 0)
force_env%in_use = use_qs_force
force_env%qs_env => qs_env
END IF
IF (PRESENT(qmmm_env)) THEN
CPASSERT(ASSOCIATED(qmmm_env))
CPASSERT(force_env%in_use == 0)
force_env%in_use = use_qmmm
force_env%qmmm_env => qmmm_env
END IF
IF (PRESENT(qmmmx_env)) THEN
CPASSERT(ASSOCIATED(qmmmx_env))
CPASSERT(force_env%in_use == 0)
force_env%in_use = use_qmmmx
force_env%qmmmx_env => qmmmx_env
END IF
IF (PRESENT(mixed_env)) THEN
CPASSERT(ASSOCIATED(mixed_env))
CPASSERT(force_env%in_use == 0)
force_env%in_use = use_mixed_force
force_env%mixed_env => mixed_env
END IF
IF (PRESENT(embed_env)) THEN
CPASSERT(ASSOCIATED(embed_env))
CPASSERT(force_env%in_use == 0)
force_env%in_use = use_embed
force_env%embed_env => embed_env
END IF
IF (PRESENT(nnp_env)) THEN
CPASSERT(ASSOCIATED(nnp_env))
CPASSERT(force_env%in_use == 0)
force_env%in_use = use_nnp_force
force_env%nnp_env => nnp_env
END IF
IF (PRESENT(ipi_env)) THEN
CPASSERT(ASSOCIATED(ipi_env))
CPASSERT(force_env%in_use == 0)
force_env%in_use = use_ipi
force_env%ipi_env => ipi_env
END IF
CPASSERT(force_env%in_use /= 0)
IF (PRESENT(sub_force_env)) THEN
force_env%sub_force_env => sub_force_env
END IF
IF (PRESENT(meta_env)) THEN
force_env%meta_env => meta_env
ELSE
NULLIFY (force_env%meta_env)
END IF
END SUBROUTINE force_env_create
! **************************************************************************************************
!> \brief ****f* force_env_methods/mixed_energy_forces [1.0]
!>
!> Computes energy and forces for a mixed force_env type
!> \param force_env the force_env that holds the mixed_env type
!> \param calculate_forces decides if forces should be calculated
!> \par History
!> 11.06 created [fschiff]
!> 04.07 generalization to an illimited number of force_eval [tlaino]
!> 04.07 further generalization to force_eval with different geometrical
!> structures [tlaino]
!> 04.08 reorganizing the genmix structure (collecting common code)
!> 01.16 added CDFT [Nico Holmberg]
!> 08.17 added DFT embedding [Vladimir Rybkin]
!> \author Florian Schiffmann
! **************************************************************************************************
SUBROUTINE mixed_energy_forces(force_env, calculate_forces)
TYPE(force_env_type), POINTER :: force_env
LOGICAL, INTENT(IN) :: calculate_forces
CHARACTER(LEN=default_path_length) :: coupling_function
CHARACTER(LEN=default_string_length) :: def_error, description, this_error
INTEGER :: iforce_eval, iparticle, istate(2), &
jparticle, mixing_type, my_group, &
natom, nforce_eval, source, unit_nr
INTEGER, DIMENSION(:), POINTER :: glob_natoms, itmplist, map_index
LOGICAL :: dip_exists
REAL(KIND=dp) :: coupling_parameter, dedf, der_1, der_2, &
dx, energy, err, lambda, lerr, &
restraint_strength, restraint_target, &
sd
REAL(KIND=dp), DIMENSION(3) :: dip_mix
REAL(KIND=dp), DIMENSION(:), POINTER :: energies
TYPE(cell_type), POINTER :: cell_mix
TYPE(cp_logger_type), POINTER :: logger, my_logger
TYPE(cp_result_p_type), DIMENSION(:), POINTER :: results
TYPE(cp_result_type), POINTER :: loc_results, results_mix
TYPE(cp_subsys_p_type), DIMENSION(:), POINTER :: subsystems
TYPE(cp_subsys_type), POINTER :: subsys_mix
TYPE(mixed_energy_type), POINTER :: mixed_energy
TYPE(mixed_force_type), DIMENSION(:), POINTER :: global_forces
TYPE(particle_list_p_type), DIMENSION(:), POINTER :: particles
TYPE(particle_list_type), POINTER :: particles_mix
TYPE(section_vals_type), POINTER :: force_env_section, gen_section, &
mapping_section, mixed_section, &
root_section
TYPE(virial_p_type), DIMENSION(:), POINTER :: virials
TYPE(virial_type), POINTER :: loc_virial, virial_mix
logger => cp_get_default_logger()
CPASSERT(ASSOCIATED(force_env))
! Get infos about the mixed subsys
CALL force_env_get(force_env=force_env, &
subsys=subsys_mix, &
force_env_section=force_env_section, &
root_section=root_section, &
cell=cell_mix)
CALL cp_subsys_get(subsys=subsys_mix, &
particles=particles_mix, &
virial=virial_mix, &
results=results_mix)
NULLIFY (map_index, glob_natoms, global_forces, itmplist)
nforce_eval = SIZE(force_env%sub_force_env)
mixed_section => section_vals_get_subs_vals(force_env_section, "MIXED")
mapping_section => section_vals_get_subs_vals(mixed_section, "MAPPING")
! Global Info
ALLOCATE (subsystems(nforce_eval))
ALLOCATE (particles(nforce_eval))
! Local Info to sync
ALLOCATE (global_forces(nforce_eval))
ALLOCATE (energies(nforce_eval))
ALLOCATE (glob_natoms(nforce_eval))
ALLOCATE (virials(nforce_eval))
ALLOCATE (results(nforce_eval))
energies = 0.0_dp
glob_natoms = 0
! Check if mixed CDFT calculation is requested and initialize
CALL mixed_cdft_init(force_env, calculate_forces)
!
IF (.NOT. force_env%mixed_env%do_mixed_cdft) THEN
DO iforce_eval = 1, nforce_eval
NULLIFY (subsystems(iforce_eval)%subsys, particles(iforce_eval)%list)
NULLIFY (results(iforce_eval)%results, virials(iforce_eval)%virial)
ALLOCATE (virials(iforce_eval)%virial)
CALL cp_result_create(results(iforce_eval)%results)
IF (.NOT. ASSOCIATED(force_env%sub_force_env(iforce_eval)%force_env)) CYCLE
! From this point on the error is the sub_error
my_group = force_env%mixed_env%group_distribution(force_env%para_env%mepos)
my_logger => force_env%mixed_env%sub_logger(my_group + 1)%p
! Copy iterations info (they are updated only in the main mixed_env)
CALL cp_iteration_info_copy_iter(logger%iter_info, my_logger%iter_info)
CALL cp_add_default_logger(my_logger)
! Get all available subsys
CALL force_env_get(force_env=force_env%sub_force_env(iforce_eval)%force_env, &
subsys=subsystems(iforce_eval)%subsys)
! all force_env share the same cell
CALL cp_subsys_set(subsystems(iforce_eval)%subsys, cell=cell_mix)
! Get available particles
CALL cp_subsys_get(subsys=subsystems(iforce_eval)%subsys, &
particles=particles(iforce_eval)%list)
! Get Mapping index array
natom = SIZE(particles(iforce_eval)%list%els)
CALL get_subsys_map_index(mapping_section, natom, iforce_eval, nforce_eval, &
map_index)
! Mapping particles from iforce_eval environment to the mixed env
DO iparticle = 1, natom
jparticle = map_index(iparticle)
particles(iforce_eval)%list%els(iparticle)%r = particles_mix%els(jparticle)%r
END DO
! Calculate energy and forces for each sub_force_env
CALL force_env_calc_energy_force(force_env%sub_force_env(iforce_eval)%force_env, &
calc_force=calculate_forces, &
skip_external_control=.TRUE.)
! Only the rank 0 process collect info for each computation
IF (force_env%sub_force_env(iforce_eval)%force_env%para_env%is_source()) THEN
CALL force_env_get(force_env%sub_force_env(iforce_eval)%force_env, &
potential_energy=energy)
CALL cp_subsys_get(subsystems(iforce_eval)%subsys, &
virial=loc_virial, results=loc_results)
energies(iforce_eval) = energy
glob_natoms(iforce_eval) = natom
virials(iforce_eval)%virial = loc_virial
CALL cp_result_copy(loc_results, results(iforce_eval)%results)
END IF
! Deallocate map_index array
IF (ASSOCIATED(map_index)) THEN
DEALLOCATE (map_index)
END IF
CALL cp_rm_default_logger()
END DO
ELSE
CALL mixed_cdft_energy_forces(force_env, calculate_forces, particles, energies, &
glob_natoms, virials, results)
END IF
! Handling Parallel execution
CALL force_env%para_env%sync()
! Post CDFT operations
CALL mixed_cdft_post_energy_forces(force_env)
! Let's transfer energy, natom, forces, virials
CALL force_env%para_env%sum(energies)
CALL force_env%para_env%sum(glob_natoms)
! Transfer forces
DO iforce_eval = 1, nforce_eval
ALLOCATE (global_forces(iforce_eval)%forces(3, glob_natoms(iforce_eval)))
global_forces(iforce_eval)%forces = 0.0_dp
IF (ASSOCIATED(force_env%sub_force_env(iforce_eval)%force_env)) THEN
IF (force_env%sub_force_env(iforce_eval)%force_env%para_env%is_source()) THEN
! Forces
DO iparticle = 1, glob_natoms(iforce_eval)
global_forces(iforce_eval)%forces(:, iparticle) = &
particles(iforce_eval)%list%els(iparticle)%f
END DO
END IF
END IF
CALL force_env%para_env%sum(global_forces(iforce_eval)%forces)
!Transfer only the relevant part of the virial..
CALL force_env%para_env%sum(virials(iforce_eval)%virial%pv_total)