KIM API V2
ex_model_Ar_P_MLJ_F03.F03
Go to the documentation of this file.
1 !
2 ! CDDL HEADER START
3 !
4 ! The contents of this file are subject to the terms of the Common Development
5 ! and Distribution License Version 1.0 (the "License").
6 !
7 ! You can obtain a copy of the license at
8 ! http://www.opensource.org/licenses/CDDL-1.0. See the License for the
9 ! specific language governing permissions and limitations under the License.
10 !
11 ! When distributing Covered Code, include this CDDL HEADER in each file and
12 ! include the License file in a prominent location with the name LICENSE.CDDL.
13 ! If applicable, add the following below this CDDL HEADER, with the fields
14 ! enclosed by brackets "[]" replaced with your own identifying information:
15 !
16 ! Portions Copyright (c) [yyyy] [name of copyright owner]. All rights reserved.
17 !
18 ! CDDL HEADER END
19 !
20 
21 !
22 ! Copyright (c) 2013--2018, Regents of the University of Minnesota.
23 ! All rights reserved.
24 !
25 ! Contributors:
26 ! Ryan S. Elliott
27 ! Ellad B. Tadmor
28 ! Valeriu Smirichinski
29 ! Stephen M. Whalen
30 !
31 
32 !****************************************************************************
33 !**
34 !** MODULE ex_model_Ar_P_MLJ_F03
35 !**
36 !** Modified Lennard-Jones pair potential (with smooth cutoff) model for Ar
37 !**
38 !** Reference: Ashcroft and Mermin
39 !**
40 !** Language: Fortran 2003
41 !**
42 !****************************************************************************
43 
44 
46 
47 use, intrinsic :: iso_c_binding
49 implicit none
50 
51 save
52 private
53 public &!Compute_Energy_Forces, &
54  model_cutoff, &
55  speccode, &
56  buffer_type
57 
58 ! Below are the definitions and values of all Model parameters
59 integer(c_int), parameter :: cd = c_double ! used for literal constants
60 integer(c_int), parameter :: DIM = 3 ! dimensionality of space
61 integer(c_int), parameter :: speccode = 1 ! internal species code
62 real(c_double), parameter :: model_cutoff = 8.15_cd ! cutoff radius
63  ! in angstroms
64 real(c_double), parameter :: model_cutsq = model_cutoff**2
65 
66 !-------------------------------------------------------------------------------
67 ! Below are the definitions and values of all additional model parameters
68 !
69 ! Recall that the Fortran 2003 format for declaring parameters is as follows:
70 !
71 ! integer(c_int), parameter :: parname = value ! This defines an integer
72 ! ! parameter called `parname'
73 ! ! with a value equal to
74 ! ! `value' (a number)
75 !
76 ! real(c_double), parameter :: parname = value ! This defines a real(c_double)
77 ! ! parameter called `parname'
78 ! ! with a value equal to
79 ! ! `value' (a number)
80 !-------------------------------------------------------------------------------
81 real(c_double), parameter :: lj_epsilon = 0.0104_cd
82 real(c_double), parameter :: lj_sigma = 3.40_cd
83 real(c_double), parameter :: lj_cutnorm = model_cutoff/lj_sigma
84 real(c_double), parameter :: lj_A = 12.0_cd*lj_epsilon*(-26.0_cd &
85  + 7.0_cd*lj_cutnorm**6)/(lj_cutnorm**14 &
86  *lj_sigma**2)
87 real(c_double), parameter :: lj_B = 96.0_cd*lj_epsilon*(7.0_cd &
88  - 2.0_cd*lj_cutnorm**6)/(lj_cutnorm**13*lj_sigma)
89 real(c_double), parameter :: lj_C = 28.0_cd*lj_epsilon*(-13.0_cd &
90  + 4.0_cd*lj_cutnorm**6)/(lj_cutnorm**12)
91 
92 type, bind(c) :: buffer_type
93  real(c_double) :: influence_distance
94  real(c_double) :: cutoff(1)
95  integer(c_int) :: padding_neighbor_hints(1)
96  integer(c_int) :: half_list_hints(1)
97 end type buffer_type
98 
99 contains
100 
101 !-------------------------------------------------------------------------------
102 !
103 ! Calculate pair potential phi(r)
104 !
105 !-------------------------------------------------------------------------------
106 subroutine calc_phi(r,phi)
107 implicit none
108 
109 !-- Transferred variables
110 real(c_double), intent(in) :: r
111 real(c_double), intent(out) :: phi
112 
113 !-- Local variables
114 real(c_double) rsq,sor,sor6,sor12
115 
116 rsq = r*r ! r^2
117 sor = lj_sigma/r ! (sig/r)
118 sor6 = sor*sor*sor !
119 sor6 = sor6*sor6 ! (sig/r)^6
120 sor12= sor6*sor6 ! (sig/r)^12
121 if (r .gt. model_cutoff) then
122  ! Argument exceeds cutoff radius
123  phi = 0.0_cd
124 else
125  phi = 4.0_cd*lj_epsilon*(sor12-sor6) + lj_a*rsq + lj_b*r + lj_c
126 endif
127 
128 end subroutine calc_phi
129 
130 !-------------------------------------------------------------------------------
131 !
132 ! Calculate pair potential phi(r) and its derivative dphi(r)
133 !
134 !-------------------------------------------------------------------------------
135 subroutine calc_phi_dphi(r,phi,dphi)
136 implicit none
137 
138 !-- Transferred variables
139 real(c_double), intent(in) :: r
140 real(c_double), intent(out) :: phi,dphi
141 
142 !-- Local variables
143 real(c_double) rsq,sor,sor6,sor12
144 
145 rsq = r*r ! r^2
146 sor = lj_sigma/r ! (sig/r)
147 sor6 = sor*sor*sor !
148 sor6 = sor6*sor6 ! (sig/r)^6
149 sor12= sor6*sor6 ! (sig/r)^12
150 if (r .gt. model_cutoff) then
151  ! Argument exceeds cutoff radius
152  phi = 0.0_cd
153  dphi = 0.0_cd
154 else
155  phi = 4.0_cd*lj_epsilon*(sor12-sor6) + lj_a*rsq + lj_b*r + lj_c
156  dphi = 24.0_cd*lj_epsilon*(-2.0_cd*sor12+sor6)/r + 2.0_cd*lj_a*r + lj_b
157 endif
158 
159 end subroutine calc_phi_dphi
160 
161 !!-------------------------------------------------------------------------------
162 !!
163 !! Compute energy and forces on particles from the positions.
164 !!
165 !!-------------------------------------------------------------------------------
166 !integer(c_int) function Compute_Energy_Forces(pkim) bind(c)
167 !implicit none
168 !
169 !!-- Transferred variables
170 !type(c_ptr), intent(in) :: pkim
171 !
172 !!-- Local variables
173 !real(c_double) :: Rij(DIM)
174 !real(c_double) :: r,Rsqij,phi,dphi,dEidr = 0.0_cd
175 !integer(c_int) :: i,j,jj,numnei,part_ret,comp_force,comp_enepot,comp_virial, &
176 ! comp_energy
177 !character (len=80) :: error_message
178 !
179 !!-- KIM variables
180 !integer(c_int), pointer :: N; type(c_ptr) :: pN
181 !real(c_double), pointer :: energy; type(c_ptr) :: penergy
182 !real(c_double), pointer :: coor(:,:); type(c_ptr) :: pcoor
183 !real(c_double), pointer :: force(:,:); type(c_ptr) :: pforce
184 !real(c_double), pointer :: enepot(:); type(c_ptr) :: penepot
185 !real(c_double), pointer :: Rij_list(:,:); type(c_ptr) :: pRij_list
186 !integer(c_int), pointer :: nei1part(:); type(c_ptr) :: pnei1part
187 !integer(c_int), pointer :: particleSpecies(:);type(c_ptr) :: pparticleSpecies
188 !real(c_double), pointer :: virial(:); type(c_ptr) :: pvirial
189 !integer(c_int) idum
190 !
191 !
192 !! Check to see if we have been asked to compute the forces, energyperpart,
193 !! energy and virial
194 !!
195 !call kim_api_getm_compute(pkim, Compute_Energy_Forces, &
196 ! "energy", comp_energy, 1, &
197 ! "forces", comp_force, 1, &
198 ! "particleEnergy", comp_enepot, 1, &
199 ! "virial", comp_virial, 1)
200 !if (Compute_Energy_Forces.lt.KIM_STATUS_OK) then
201 ! idum = kim_api_report_error(__LINE__, THIS_FILE_NAME, &
202 ! "kim_api_getm_compute", Compute_Energy_Forces)
203 ! return
204 !endif
205 !
206 !! Unpack data from KIM object
207 !!
208 !call kim_api_getm_data(pkim, Compute_Energy_Forces, &
209 ! "numberOfParticles", pN, 1, &
210 ! "particleSpecies", pparticleSpecies,1, &
211 ! "coordinates", pcoor, 1, &
212 ! "energy", penergy, TRUEFALSE(comp_energy.eq.1), &
213 ! "forces", pforce, TRUEFALSE(comp_force.eq.1), &
214 ! "particleEnergy", penepot, TRUEFALSE(comp_enepot.eq.1), &
215 ! "virial", pvirial, TRUEFALSE(comp_virial.eq.1))
216 !if (Compute_Energy_Forces.lt.KIM_STATUS_OK) then
217 ! idum = kim_api_report_error(__LINE__, THIS_FILE_NAME, &
218 ! "kim_api_getm_data_f", Compute_Energy_Forces)
219 ! return
220 !endif
221 !
222 !call c_f_pointer(pN, N)
223 !call c_f_pointer(pparticleSpecies, particleSpecies, [N])
224 !call c_f_pointer(pcoor, coor, [DIM,N])
225 !if (comp_energy.eq.1) call c_f_pointer(penergy, energy)
226 !if (comp_force.eq.1) call c_f_pointer(pforce, force, [DIM,N])
227 !if (comp_enepot.eq.1) call c_f_pointer(penepot, enepot, [N])
228 !if (comp_virial.eq.1) call c_f_pointer(pvirial, virial, [6])
229 !
230 !
231 !! Check to be sure that the species are correct
232 !!
233 !Compute_Energy_Forces = KIM_STATUS_FAIL ! assume an error
234 !do i = 1,N
235 ! if (particleSpecies(i).ne.speccode) then
236 ! idum = kim_api_report_error(__LINE__, THIS_FILE_NAME, &
237 ! "Unexpected species detected", &
238 ! Compute_Energy_Forces)
239 ! return
240 ! endif
241 !enddo
242 !Compute_Energy_Forces = KIM_STATUS_OK ! everything is ok
243 !
244 !! Initialize potential energies, forces, virial term
245 !!
246 !if (comp_enepot.eq.1) enepot = 0.0_cd
247 !if (comp_energy.eq.1) energy = 0.0_cd
248 !if (comp_force.eq.1) force = 0.0_cd
249 !if (comp_virial.eq.1) virial = 0.0_cd
250 !
251 !
252 !!
253 !! Compute energy and forces
254 !!
255 !
256 !! Loop over particles and compute energy and forces
257 !!
258 !do i=1,N
259 ! Compute_Energy_Forces = kim_api_get_neigh(pkim,1,i,part_ret,numnei, &
260 ! pnei1part,pRij_list)
261 ! if (Compute_Energy_Forces.ne.KIM_STATUS_OK) then
262 ! ! some sort of problem, exit
263 ! idum = kim_api_report_error(__LINE__, THIS_FILE_NAME, &
264 ! "kim_api_get_neigh", &
265 ! Compute_Energy_Forces)
266 ! Compute_Energy_Forces = KIM_STATUS_FAIL
267 ! return
268 ! endif
269 !
270 ! call c_f_pointer(pnei1part, nei1part, [numnei])
271 !
272 ! ! Loop over the neighbors of particle i
273 ! !
274 ! do jj = 1, numnei
275 !
276 ! j = nei1part(jj) ! get neighbor ID
277 !
278 ! ! compute relative position vector
279 ! !
280 ! Rij(:) = coor(:,j) - coor(:,i) ! distance vector between i j
281 !
282 ! ! compute energy and forces
283 ! !
284 ! Rsqij = dot_product(Rij,Rij) ! compute square distance
285 ! if ( Rsqij .lt. model_cutsq ) then ! particles are interacting?
286 !
287 ! r = sqrt(Rsqij) ! compute distance
288 ! if (comp_force.eq.1.or.comp_virial.eq.1) then
289 ! call calc_phi_dphi(r,phi,dphi) ! compute pair potential
290 ! ! and it derivative
291 ! dEidr = 0.5_cd*dphi
292 ! else
293 ! call calc_phi(r,phi) ! compute just pair potential
294 ! endif
295 !
296 ! ! contribution to energy
297 ! !
298 ! if (comp_enepot.eq.1) then
299 ! enepot(i) = enepot(i) + 0.5_cd*phi ! accumulate energy
300 ! endif
301 ! if (comp_energy.eq.1) then
302 ! energy = energy + 0.5_cd*phi
303 ! endif
304 !
305 ! ! contribution to virial tensor, virial(i,j)=r(i)*r(j)*(dV/dr)/r
306 ! !
307 ! if (comp_virial.eq.1) then
308 ! virial(1) = virial(1) + Rij(1)*Rij(1)*dEidr/r
309 ! virial(2) = virial(2) + Rij(2)*Rij(2)*dEidr/r
310 ! virial(3) = virial(3) + Rij(3)*Rij(3)*dEidr/r
311 ! virial(4) = virial(4) + Rij(2)*Rij(3)*dEidr/r
312 ! virial(5) = virial(5) + Rij(1)*Rij(3)*dEidr/r
313 ! virial(6) = virial(6) + Rij(1)*Rij(2)*dEidr/r
314 ! endif
315 !
316 ! ! contribution to forces
317 ! !
318 ! if (comp_force.eq.1) then
319 ! force(:,i) = force(:,i) + dEidr*Rij/r ! accumulate force on i
320 ! force(:,j) = force(:,j) - dEidr*Rij/r ! accumulate force on j
321 ! endif
322 !
323 ! endif
324 !
325 ! enddo ! loop on jj
326 !
327 !enddo
328 !
329 !! Everything is great
330 !!
331 !Compute_Energy_Forces = KIM_STATUS_OK
332 !return
333 !
334 !end function Compute_Energy_Forces
335 
336 end module ex_model_ar_p_mlj_f03
337 
338 !-------------------------------------------------------------------------------
339 !
340 ! Model create routine (REQUIRED)
341 !
342 !-------------------------------------------------------------------------------
343 #include "kim_model_create_log_macros.fd"
344 subroutine model_create_routine(model_create_handle, requested_length_unit, &
345  requested_energy_unit, requested_charge_unit, requested_temperature_unit, &
346  requested_time_unit, ierr) bind(c)
347 use, intrinsic :: iso_c_binding
350 implicit none
351 
352 !-- Transferred variables
353 type(kim_model_create_handle_type), intent(inout) :: model_create_handle
354 type(kim_length_unit_type), intent(in) :: requested_length_unit
355 type(kim_energy_unit_type), intent(in) :: requested_energy_unit
356 type(kim_charge_unit_type), intent(in) :: requested_charge_unit
357 type(kim_temperature_unit_type), intent(in) :: requested_temperature_unit
358 type(kim_time_unit_type), intent(in) :: requested_time_unit
359 integer(c_int), intent(out) :: ierr
360 
361 !-- KIM variables
362 integer(c_int) :: ierr2
363 type(buffer_type), pointer :: buf
364 character(kind=c_char, len=100000) model_create_string
365 
366 kim_log_file = __file__
367 
368 ierr = 0
369 ierr2 = 0
370 
371 ! set units
372 call kim_model_create_set_units(model_create_handle, &
373  kim_length_unit_a, &
374  kim_energy_unit_ev, &
375  kim_charge_unit_unused, &
376  kim_temperature_unit_unused, &
377  kim_time_unit_unused, &
378  ierr2)
379 ierr = ierr + ierr2
380 
381 ! register species
382 call kim_model_create_set_species_code(model_create_handle, &
383  kim_species_name_ar, speccode, ierr2)
384 ierr = ierr + ierr2
385 
386 ! register numbering
387 call kim_model_create_set_model_numbering(model_create_handle, &
388  kim_numbering_one_based, ierr2);
389 ierr = ierr + ierr2
390 
391 ! register function pointers
392 call kim_model_create_set_compute_pointer(model_create_handle, &
393  kim_language_name_fortran, c_funloc(kim_model_create_string), ierr2)
394 ierr = ierr + ierr2
395 call kim_model_create_set_compute_arguments_create_pointer( &
396  model_create_handle, kim_language_name_fortran, &
397  c_funloc(kim_model_create_string), ierr2)
398 ierr = ierr + ierr2
399 call kim_model_create_set_compute_arguments_destroy_pointer( &
400  model_create_handle, kim_language_name_fortran, &
401  c_funloc(kim_model_create_string), ierr2)
402 ierr = ierr + ierr2
403 call kim_model_create_set_destroy_pointer(model_create_handle, &
404  kim_language_name_fortran, c_funloc(kim_model_create_string), ierr2)
405 ierr = ierr + ierr2
406 call kim_model_create_set_refresh_pointer( &
407  model_create_handle, kim_language_name_fortran, &
408  c_funloc(kim_model_create_string), ierr2)
409 ierr = ierr + ierr2
410 
411 ! allocate buffer
412 allocate( buf )
413 
414 ! store model buffer in KIM object
415 call kim_model_create_set_model_buffer_pointer(model_create_handle, &
416  c_loc(buf))
417 
418 ! set buffer values
419 buf%influence_distance = model_cutoff
420 buf%cutoff = model_cutoff
421 buf%padding_neighbor_hints = 1
422 buf%half_list_hints = 0
423 
424 ! register influence distance
425 call kim_model_create_set_influence_distance_pointer( &
426  model_create_handle, buf%influence_distance)
427 
428 ! register cutoff
429 call kim_model_create_set_neighbor_list_pointers(model_create_handle, &
430  1, buf%cutoff, buf%padding_neighbor_hints, buf%half_list_hints)
431 
432 if (ierr /= 0) then
433  ierr = 1
434  deallocate( buf )
435  kim_log_message = "Unable to successfully initialize model"
436  log_error()
437 endif
438 
439 call kim_model_create_string(model_create_handle, model_create_string)
440 print '(A)', trim(model_create_string)
441 stop
442 
443 return
444 
445 end subroutine model_create_routine