(git:15c1bfc)
Loading...
Searching...
No Matches
fp_methods.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2025 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief methods used in the flexible partitioning scheme
10!> \par History
11!> 04.2006 [Joost VandeVondele]
12!> \author Joost VandeVondele
13! **************************************************************************************************
15
16 USE beta_gamma_psi, ONLY: psi
17 USE cell_types, ONLY: cell_type,&
18 pbc
26 USE fp_types, ONLY: fp_type
27 USE kinds, ONLY: default_string_length,&
28 dp
29 USE mathconstants, ONLY: fac,&
30 maxfac,&
34#include "./base/base_uses.f90"
35
36 IMPLICIT NONE
37 PRIVATE
38
39 PUBLIC :: fp_eval
40
41 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'fp_methods'
42
43CONTAINS
44
45! **************************************************************************************************
46!> \brief Computes the forces and the energy due to the flexible potential & bias,
47!> and writes the weights file
48!> \param fp_env ...
49!> \param subsys ...
50!> \param cell ...
51!> \par History
52!> 04.2006 created [Joost VandeVondele]
53! **************************************************************************************************
54 SUBROUTINE fp_eval(fp_env, subsys, cell)
55 TYPE(fp_type), POINTER :: fp_env
56 TYPE(cp_subsys_type), POINTER :: subsys
57 TYPE(cell_type), POINTER :: cell
58
59 CHARACTER(LEN=*), PARAMETER :: routinen = 'fp_eval'
60
61 CHARACTER(LEN=default_string_length) :: tmpstr
62 INTEGER :: handle, i, icenter, iparticle, &
63 output_unit
64 LOGICAL :: zero_weight
65 REAL(kind=dp) :: c, dcdr, kt, r, rab(3), sf, strength
66 TYPE(cp_logger_type), POINTER :: logger
67 TYPE(particle_list_type), POINTER :: particles_list
68 TYPE(particle_type), DIMENSION(:), POINTER :: particles
69
70 CALL timeset(routinen, handle)
71
72 cpassert(ASSOCIATED(fp_env))
73 cpassert(fp_env%use_fp)
74 cpassert(ASSOCIATED(subsys))
75 CALL cp_subsys_get(subsys, particles=particles_list)
76 particles => particles_list%els
77
78 ! compute the force due to the reflecting walls
79 ! and count the distribution in discrete and contiguous ways
80 zero_weight = .false.
81 fp_env%restraint_energy = 0.0_dp
82 icenter = fp_env%central_atom
83 strength = fp_env%strength
84 fp_env%i1 = 0; fp_env%i2 = 0; fp_env%o1 = 0; fp_env%o2 = 0
85 fp_env%ri1 = 0.0_dp; fp_env%ri2 = 0.0_dp; fp_env%ro1 = 0.0_dp; fp_env%ro2 = 0.0_dp
86 fp_env%energy = 0.0_dp
87
88 ! inner particles
89 DO i = 1, SIZE(fp_env%inner_atoms)
90 iparticle = fp_env%inner_atoms(i)
91 rab = particles(iparticle)%r - particles(icenter)%r
92 rab = pbc(rab, cell)
93 r = sqrt(sum(rab**2))
94 ! constraint wall (they feel to outer wall)
95 IF (r > fp_env%outer_radius) THEN
96 zero_weight = .true.
97 fp_env%restraint_energy = fp_env%restraint_energy + 0.5_dp*strength*(r - fp_env%outer_radius)**2
98 sf = strength*(r - fp_env%outer_radius)/r
99 particles(iparticle)%f = particles(iparticle)%f - sf*rab
100 particles(icenter)%f = particles(icenter)%f + sf*rab
101 END IF
102 ! count the distribution
103 IF (r > fp_env%inner_radius) THEN
104 fp_env%i2 = fp_env%i2 + 1
105 ELSE
106 fp_env%i1 = fp_env%i1 + 1
107 END IF
108 ! smooth count the distribution
109 CALL smooth_count(r, fp_env%inner_radius, fp_env%smooth_width, c, dcdr)
110 fp_env%ri1 = fp_env%ri1 + c
111 fp_env%ri2 = fp_env%ri2 + (1.0_dp - c)
112 END DO
113
114 ! outer particles
115 DO i = 1, SIZE(fp_env%outer_atoms)
116 iparticle = fp_env%outer_atoms(i)
117 rab = particles(iparticle)%r - particles(icenter)%r
118 rab = pbc(rab, cell)
119 r = sqrt(sum(rab**2))
120 ! constraint wall (they feel the inner wall)
121 IF (r < fp_env%inner_radius) THEN
122 zero_weight = .true.
123 fp_env%restraint_energy = fp_env%restraint_energy + &
124 0.5_dp*strength*(r - fp_env%inner_radius)**2
125 sf = strength*(r - fp_env%inner_radius)/r
126 particles(iparticle)%f = particles(iparticle)%f - sf*rab
127 particles(icenter)%f = particles(icenter)%f + sf*rab
128 END IF
129 ! count the distribution
130 IF (r > fp_env%outer_radius) THEN
131 fp_env%o2 = fp_env%o2 + 1
132 ELSE
133 fp_env%o1 = fp_env%o1 + 1
134 END IF
135 ! smooth count the distribution
136 CALL smooth_count(r, fp_env%outer_radius, fp_env%smooth_width, c, dcdr)
137 fp_env%ro1 = fp_env%ro1 + c
138 fp_env%ro2 = fp_env%ro2 + (1.0_dp - c)
139 END DO
140 fp_env%energy = fp_env%energy + fp_env%restraint_energy
141
142 ! the combinatorial weight
143 i = fp_env%i2 + fp_env%o1
144 cpassert(i <= maxfac)
145 fp_env%comb_weight = (fac(fp_env%i2)*fac(fp_env%o1))/fac(i)
146
147 ! we can add the bias potential now.
148 ! this bias has the form
149 ! kT * { ln[(o1+i2)!] - ln[o1!] - ln[i2!] }
150 ! where the smooth counts are used for o1 and i2
151 fp_env%bias_energy = 0.0_dp
152 IF (fp_env%bias) THEN
153 kt = fp_env%temperature
154 fp_env%bias_energy = kt*(log_gamma(fp_env%ro1 + fp_env%ri2 + 1) - &
155 log_gamma(fp_env%ro1 + 1) - log_gamma(fp_env%ri2 + 1))
156
157 ! and add the corresponding forces
158 ! inner particles
159 DO i = 1, SIZE(fp_env%inner_atoms)
160 iparticle = fp_env%inner_atoms(i)
161 rab = particles(iparticle)%r - particles(icenter)%r
162 rab = pbc(rab, cell)
163 r = sqrt(sum(rab**2))
164 CALL smooth_count(r, fp_env%inner_radius, fp_env%smooth_width, c, dcdr)
165 sf = kt*(psi(fp_env%ro1 + fp_env%ri2 + 1) - psi(fp_env%ri2 + 1))*(-dcdr)/r
166 particles(iparticle)%f = particles(iparticle)%f - sf*rab
167 particles(icenter)%f = particles(icenter)%f + sf*rab
168 END DO
169 ! outer particles
170 DO i = 1, SIZE(fp_env%outer_atoms)
171 iparticle = fp_env%outer_atoms(i)
172 rab = particles(iparticle)%r - particles(icenter)%r
173 rab = pbc(rab, cell)
174 r = sqrt(sum(rab**2))
175 CALL smooth_count(r, fp_env%outer_radius, fp_env%smooth_width, c, dcdr)
176 sf = kt*(psi(fp_env%ro1 + fp_env%ri2 + 1) - psi(fp_env%ro1 + 1))*(dcdr)/r
177 particles(iparticle)%f = particles(iparticle)%f - sf*rab
178 particles(icenter)%f = particles(icenter)%f + sf*rab
179 END DO
180 END IF
181 fp_env%energy = fp_env%energy + fp_env%bias_energy
182 fp_env%bias_weight = exp(fp_env%bias_energy/kt)
183
184 ! if this configuration is a valid one, compute its weight
185 IF (zero_weight) THEN
186 fp_env%weight = 0.0_dp
187 ELSE
188 fp_env%weight = fp_env%comb_weight*fp_env%bias_weight
189 END IF
190
191 ! put weights and other info on file
192 logger => cp_get_default_logger()
193 output_unit = cp_print_key_unit_nr(logger, fp_env%print_section, "", &
194 extension=".weights")
195 IF (output_unit > 0) THEN
196 tmpstr = cp_iter_string(logger%iter_info, fp_env%print_section)
197 WRITE (output_unit, '(T2,A15,6(1X,F16.10),4(1X,I4),4(1X,F16.10))') &
198 trim(tmpstr), &
199 fp_env%weight, fp_env%comb_weight, fp_env%bias_weight, &
200 fp_env%energy, fp_env%restraint_energy, fp_env%bias_energy, &
201 fp_env%i1, fp_env%i2, fp_env%o1, fp_env%o2, &
202 fp_env%ri1, fp_env%ri2, fp_env%ro1, fp_env%ro2
203 END IF
204
205 CALL cp_print_key_finished_output(output_unit, logger, fp_env%print_section, &
206 "")
207
208 CALL timestop(handle)
209
210 END SUBROUTINE fp_eval
211
212! **************************************************************************************************
213!> \brief counts in a smooth way (error function with width=width)
214!> if r is closer than r1. Returns 1.0 for the count=c if r<<r1
215!> and the derivative wrt r dcdr
216!> \param r ...
217!> \param r1 ...
218!> \param width ...
219!> \param c ...
220!> \param dcdr ...
221!> \par History
222!> 04.2006 created [Joost VandeVondele]
223! **************************************************************************************************
224 SUBROUTINE smooth_count(r, r1, width, c, dcdr)
225 REAL(kind=dp), INTENT(IN) :: r, r1, width
226 REAL(kind=dp), INTENT(OUT) :: c, dcdr
227
228 REAL(kind=dp) :: arg
229
230 arg = (r1 - r)/width
231
232 c = (1.0_dp + erf(arg))/2.0_dp
233 dcdr = (-oorootpi/width)*exp(-arg**2)
234
235 END SUBROUTINE
236
237END MODULE fp_methods
real(dp) function, public psi(xx)
...
Handles all functions related to the CELL.
Definition cell_types.F:15
various routines to log and control the output. The idea is that decisions about where to log should ...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
character(len=default_string_length) function, public cp_iter_string(iter_info, print_key, for_file)
returns the iteration string, a string that is useful to create unique filenames (once you trim it)
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout)
...
subroutine, public cp_print_key_finished_output(unit_nr, logger, basis_section, print_key_path, local, ignore_should_output, on_file, mpi_io)
should be called after you finish working with a unit obtained with cp_print_key_unit_nr,...
types that represent a subsys, i.e. a part of the system
subroutine, public cp_subsys_get(subsys, ref_count, atomic_kinds, atomic_kind_set, particles, particle_set, local_particles, molecules, molecule_set, molecule_kinds, molecule_kind_set, local_molecules, para_env, colvar_p, shell_particles, core_particles, gci, multipoles, natom, nparticle, ncore, nshell, nkind, atprop, virial, results, cell)
returns information about various attributes of the given subsys
methods used in the flexible partitioning scheme
Definition fp_methods.F:14
subroutine, public fp_eval(fp_env, subsys, cell)
Computes the forces and the energy due to the flexible potential & bias, and writes the weights file.
Definition fp_methods.F:55
types used in the flexible partitioning scheme
Definition fp_types.F:14
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
Definition of mathematical constants and functions.
real(kind=dp), parameter, public oorootpi
integer, parameter, public maxfac
real(kind=dp), dimension(0:maxfac), parameter, public fac
represent a simple array based list of the given type
Define the data structure for the particle information.
Type defining parameters related to the simulation cell.
Definition cell_types.F:55
type of a logger, at the moment it contains just a print level starting at which level it should be l...
represents a system: atoms, molecules, their pos,vel,...