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, &
56  model_cutoff, &
57  speccode, &
58  buffer_type
59 
60 ! Below are the definitions and values of all Model parameters
61 integer(c_int), parameter :: cd = c_double ! used for literal constants
62 integer(c_int), parameter :: DIM = 3 ! dimensionality of space
63 integer(c_int), parameter :: speccode = 1 ! internal species code
64 real(c_double), parameter :: model_cutoff = 8.15_cd ! cutoff radius
65  ! in angstroms
66 real(c_double), parameter :: model_cutsq = model_cutoff**2
67 
68 !-------------------------------------------------------------------------------
69 ! Below are the definitions and values of all additional model parameters
70 !
71 ! Recall that the Fortran 2003 format for declaring parameters is as follows:
72 !
73 ! integer(c_int), parameter :: parname = value ! This defines an integer
74 ! ! parameter called `parname'
75 ! ! with a value equal to
76 ! ! `value' (a number)
77 !
78 ! real(c_double), parameter :: parname = value ! This defines a real(c_double)
79 ! ! parameter called `parname'
80 ! ! with a value equal to
81 ! ! `value' (a number)
82 !-------------------------------------------------------------------------------
83 real(c_double), parameter :: lj_epsilon = 0.0104_cd
84 real(c_double), parameter :: lj_sigma = 3.40_cd
85 real(c_double), parameter :: lj_cutnorm = model_cutoff/lj_sigma
86 real(c_double), parameter :: lj_A = 12.0_cd*lj_epsilon*(-26.0_cd &
87  + 7.0_cd*lj_cutnorm**6)/(lj_cutnorm**14 &
88  *lj_sigma**2)
89 real(c_double), parameter :: lj_B = 96.0_cd*lj_epsilon*(7.0_cd &
90  - 2.0_cd*lj_cutnorm**6)/(lj_cutnorm**13*lj_sigma)
91 real(c_double), parameter :: lj_C = 28.0_cd*lj_epsilon*(-13.0_cd &
92  + 4.0_cd*lj_cutnorm**6)/(lj_cutnorm**12)
93 
94 type, bind(c) :: buffer_type
95  real(c_double) :: influence_distance
96  real(c_double) :: cutoff(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 !-------------------------------------------------------------------------------
337 !
338 ! Model compute arguments create routine (REQUIRED)
339 !
340 !-------------------------------------------------------------------------------
341 #include "kim_model_compute_arguments_create_log_macros.fd"
342 subroutine model_compute_arguments_create(model_compute_handle, &
343  model_compute_arguments_create_handle, ierr) bind(c)
344  use, intrinsic :: iso_c_binding
346  log_entry=>kim_model_compute_arguments_create_log_entry
347  implicit none
348 
349  !-- Transferred variables
350  type(kim_model_compute_handle_type), intent(in) :: model_compute_handle
351  type(kim_model_compute_arguments_create_handle_type), intent(inout) :: &
352  model_compute_arguments_create_handle
353  integer(c_int), intent(out) :: ierr
354 
355  integer(c_int) :: ierr2
356  character(kind=c_char, len=100000) compute_arguments_string
357 
358  ierr = 0
359  ierr2 = 0
360 
361  ! register arguments
362  call kim_model_compute_arguments_create_set_argument_support_status( &
363  model_compute_arguments_create_handle, &
364  kim_compute_argument_name_partial_energy, &
365  kim_support_status_optional, ierr2)
366  ierr = ierr + ierr2
367  call kim_model_compute_arguments_create_set_argument_support_status( &
368  model_compute_arguments_create_handle, &
369  kim_compute_argument_name_partial_forces, &
370  kim_support_status_optional, ierr2)
371  ierr = ierr + ierr2
372  call kim_model_compute_arguments_create_set_argument_support_status( &
373  model_compute_arguments_create_handle, &
374  kim_compute_argument_name_partial_particle_energy, &
375  kim_support_status_optional, ierr2)
376  ierr = ierr + ierr2
377  call kim_model_compute_arguments_create_set_argument_support_status( &
378  model_compute_arguments_create_handle, &
379  kim_compute_argument_name_partial_virial, &
380  kim_support_status_optional, ierr2)
381  ierr = ierr + ierr2
382 
383  ! register call backs
384  ! NONE
385 
386  if (ierr /= 0) then
387  ierr = 1
388  kim_log_message = "Unable to successfully create compute_arguments object"
389  log_error()
390  else
392  model_compute_arguments_create_handle, compute_arguments_string)
393  print '(A)', trim(compute_arguments_string)
394  endif
395 
396  return
397 end subroutine model_compute_arguments_create
398 
399 !-------------------------------------------------------------------------------
400 !
401 ! Model compute arguments destroy routine (REQUIRED)
402 !
403 !-------------------------------------------------------------------------------
404 #include "kim_model_compute_arguments_destroy_log_macros.fd"
405 subroutine model_compute_arguments_destroy(model_compute_handle, &
406  model_compute_arguments_destroy_handle, ierr) bind(c)
407  use, intrinsic :: iso_c_binding
409  log_entry=>kim_model_compute_arguments_destroy_log_entry
410  implicit none
411 
412  !-- Transferred variables
413  type(kim_model_compute_handle_type), intent(in) :: model_compute_handle
414  type(kim_model_compute_arguments_destroy_handle_type), intent(inout) :: &
415  model_compute_arguments_destroy_handle
416  integer(c_int), intent(out) :: ierr
417 
418  integer(c_int) :: ierr2
419 
420  ierr = 0
421  ierr2 = 0
422 
423  ! nothing to do
424 
425  return
426 end subroutine model_compute_arguments_destroy
427 
428 end module ex_model_ar_p_mlj_f03
429 
430 !-------------------------------------------------------------------------------
431 !
432 ! Model create routine (REQUIRED)
433 !
434 !-------------------------------------------------------------------------------
435 #include "kim_model_create_log_macros.fd"
436 subroutine model_create_routine(model_create_handle, requested_length_unit, &
437  requested_energy_unit, requested_charge_unit, requested_temperature_unit, &
438  requested_time_unit, ierr) bind(c)
439 use, intrinsic :: iso_c_binding
442 implicit none
443 
444 !-- Transferred variables
445 type(kim_model_create_handle_type), intent(inout) :: model_create_handle
446 type(kim_length_unit_type), intent(in) :: requested_length_unit
447 type(kim_energy_unit_type), intent(in) :: requested_energy_unit
448 type(kim_charge_unit_type), intent(in) :: requested_charge_unit
449 type(kim_temperature_unit_type), intent(in) :: requested_temperature_unit
450 type(kim_time_unit_type), intent(in) :: requested_time_unit
451 integer(c_int), intent(out) :: ierr
452 
453 !-- KIM variables
454 integer(c_int) :: ierr2
455 type(buffer_type), pointer :: buf
456 character(kind=c_char, len=100000) model_create_string
457 
458 kim_log_file = __file__
459 
460 ierr = 0
461 ierr2 = 0
462 
463 ! set units
464 call kim_model_create_set_units(model_create_handle, &
465  kim_length_unit_a, &
466  kim_energy_unit_ev, &
467  kim_charge_unit_unused, &
468  kim_temperature_unit_unused, &
469  kim_time_unit_unused, &
470  ierr2)
471 ierr = ierr + ierr2
472 
473 ! register species
474 call kim_model_create_set_species_code(model_create_handle, &
475  kim_species_name_ar, speccode, ierr2)
476 ierr = ierr + ierr2
477 
478 ! register numbering
479 call kim_model_create_set_model_numbering(model_create_handle, &
480  kim_numbering_one_based, ierr2);
481 ierr = ierr + ierr2
482 
483 ! register function pointers
484 call kim_model_create_set_compute_pointer(model_create_handle, &
485  kim_language_name_fortran, c_funloc(kim_model_create_string), ierr2)
486 ierr = ierr + ierr2
487 call kim_model_create_set_compute_arguments_create_pointer( &
488  model_create_handle, kim_language_name_fortran, &
489  c_funloc(model_compute_arguments_create), ierr2)
490 ierr = ierr + ierr2
491 call kim_model_create_set_compute_arguments_destroy_pointer( &
492  model_create_handle, kim_language_name_fortran, &
493  c_funloc(model_compute_arguments_destroy), ierr2)
494 ierr = ierr + ierr2
495 call kim_model_create_set_destroy_pointer(model_create_handle, &
496  kim_language_name_fortran, c_funloc(kim_model_create_string), ierr2)
497 ierr = ierr + ierr2
498 call kim_model_create_set_refresh_pointer( &
499  model_create_handle, kim_language_name_fortran, &
500  c_funloc(kim_model_create_string), ierr2)
501 ierr = ierr + ierr2
502 
503 ! allocate buffer
504 allocate( buf )
505 
506 ! store model buffer in KIM object
507 call kim_model_create_set_model_buffer_pointer(model_create_handle, &
508  c_loc(buf))
509 
510 ! set buffer values
511 buf%influence_distance = model_cutoff
512 buf%cutoff = model_cutoff
513 
514 ! register influence distance
515 call kim_model_create_set_influence_distance_pointer( &
516  model_create_handle, buf%influence_distance)
517 
518 ! register cutoff
519 call kim_model_create_set_neighbor_list_cutoffs_pointer(model_create_handle, &
520  1, buf%cutoff)
521 
522 if (ierr /= 0) then
523  ierr = 1
524  deallocate( buf )
525  kim_log_message = "Unable to successfully initialize model"
526  log_error()
527 endif
528 
529 call kim_model_create_string(model_create_handle, model_create_string)
530 print '(A)', trim(model_create_string)
531 
532 return
533 
534 end subroutine model_create_routine
subroutine, public model_compute_arguments_destroy(model_compute_handle, model_compute_arguments_destroy_handle, ierr)
subroutine, public model_compute_arguments_create(model_compute_handle, model_compute_arguments_create_handle, ierr)