1 |
!! Fortran interface to C entry plug. |
2 |
|
3 |
module simulation |
4 |
use definitions |
5 |
use neighborLists |
6 |
use force_globals |
7 |
use vector_class |
8 |
use atype_module |
9 |
use lj |
10 |
#ifdef IS_MPI |
11 |
use mpiSimulation |
12 |
#endif |
13 |
|
14 |
implicit none |
15 |
PRIVATE |
16 |
|
17 |
#define __FORTRAN90 |
18 |
#include "fSimulation.h" |
19 |
|
20 |
type (simtype), public :: thisSim |
21 |
|
22 |
logical, save :: simulation_setup_complete = .false. |
23 |
|
24 |
integer, public, save :: natoms |
25 |
integer, public, save :: nExcludes_Global = 0 |
26 |
integer, public, save :: nExcludes_Local = 0 |
27 |
integer, allocatable, dimension(:,:), public :: excludesLocal |
28 |
integer, allocatable, dimension(:), public :: excludesGlobal |
29 |
|
30 |
real(kind=dp), save :: rcut2 = 0.0_DP |
31 |
real(kind=dp), save :: rcut6 = 0.0_DP |
32 |
real(kind=dp), save :: rlist2 = 0.0_DP |
33 |
real(kind=dp), public, dimension(3), save :: box |
34 |
|
35 |
|
36 |
public :: SimulationSetup |
37 |
public :: getNlocal |
38 |
public :: setBox |
39 |
public :: setBox_3d |
40 |
public :: getBox |
41 |
public :: setRcut |
42 |
public :: getRcut |
43 |
public :: getRlist |
44 |
public :: getRrf |
45 |
public :: getRt |
46 |
public :: getDielect |
47 |
public :: SimUsesPBC |
48 |
public :: SimUsesLJ |
49 |
public :: SimUsesDipoles |
50 |
public :: SimUsesSticky |
51 |
public :: SimUsesRF |
52 |
public :: SimUsesGB |
53 |
public :: SimUsesEAM |
54 |
public :: SimRequiresPrepairCalc |
55 |
public :: SimRequiresPostpairCalc |
56 |
public :: SimUsesDirectionalAtoms |
57 |
|
58 |
interface getBox |
59 |
module procedure getBox_3d |
60 |
module procedure getBox_1d |
61 |
end interface |
62 |
|
63 |
interface setBox |
64 |
module procedure setBox_3d |
65 |
module procedure setBox_1d |
66 |
end interface |
67 |
|
68 |
contains |
69 |
|
70 |
subroutine SimulationSetup(setThisSim, nComponents, c_idents, & |
71 |
CnLocalExcludes, CexcludesLocal, CnGlobalExcludes, CexcludesGlobal, & |
72 |
status) |
73 |
|
74 |
type (simtype) :: setThisSim |
75 |
integer, intent(inout) :: nComponents |
76 |
integer, dimension(nComponents),intent(inout) :: c_idents |
77 |
|
78 |
integer :: CnLocalExcludes |
79 |
integer, dimension(2,CnLocalExcludes), intent(in) :: CexcludesLocal |
80 |
integer :: CnGlobalExcludes |
81 |
integer, dimension(CnGlobalExcludes), intent(in) :: CexcludesGlobal |
82 |
!! Result status, success = 0, status = -1 |
83 |
integer, intent(out) :: status |
84 |
integer :: i, me, thisStat, alloc_stat, myNode |
85 |
#ifdef IS_MPI |
86 |
integer, allocatable, dimension(:) :: c_idents_Row |
87 |
integer, allocatable, dimension(:) :: c_idents_Col |
88 |
integer :: nrow |
89 |
integer :: ncol |
90 |
#endif |
91 |
|
92 |
simulation_setup_complete = .false. |
93 |
status = 0 |
94 |
|
95 |
! copy C struct into fortran type |
96 |
thisSim = setThisSim |
97 |
natoms = nComponents |
98 |
rcut2 = thisSim%rcut * thisSim%rcut |
99 |
rcut6 = rcut2 * rcut2 * rcut2 |
100 |
rlist2 = thisSim%rlist * thisSim%rlist |
101 |
box = thisSim%box |
102 |
nExcludes_Global = CnGlobalExcludes |
103 |
nExcludes_Local = CnLocalExcludes |
104 |
|
105 |
call InitializeForceGlobals(natoms, thisStat) |
106 |
if (thisStat /= 0) then |
107 |
status = -1 |
108 |
return |
109 |
endif |
110 |
|
111 |
call InitializeSimGlobals(thisStat) |
112 |
if (thisStat /= 0) then |
113 |
status = -1 |
114 |
return |
115 |
endif |
116 |
|
117 |
#ifdef IS_MPI |
118 |
! We can only set up forces if mpiSimulation has been setup. |
119 |
if (.not. isMPISimSet()) then |
120 |
write(default_error,*) "MPI is not set" |
121 |
status = -1 |
122 |
return |
123 |
endif |
124 |
nrow = getNrow(plan_row) |
125 |
ncol = getNcol(plan_col) |
126 |
mynode = getMyNode() |
127 |
|
128 |
allocate(c_idents_Row(nrow),stat=alloc_stat) |
129 |
if (alloc_stat /= 0 ) then |
130 |
status = -1 |
131 |
return |
132 |
endif |
133 |
|
134 |
allocate(c_idents_Col(ncol),stat=alloc_stat) |
135 |
if (alloc_stat /= 0 ) then |
136 |
status = -1 |
137 |
return |
138 |
endif |
139 |
|
140 |
call gather(c_idents, c_idents_Row, plan_row) |
141 |
call gather(c_idents, c_idents_Col, plan_col) |
142 |
|
143 |
do i = 1, nrow |
144 |
me = getFirstMatchingElement(atypes, "c_ident", c_idents_Row(i)) |
145 |
atid_Row(i) = me |
146 |
enddo |
147 |
|
148 |
do i = 1, ncol |
149 |
me = getFirstMatchingElement(atypes, "c_ident", c_idents_Col(i)) |
150 |
atid_Col(i) = me |
151 |
enddo |
152 |
|
153 |
!! free temporary ident arrays |
154 |
if (allocated(c_idents_Col)) then |
155 |
deallocate(c_idents_Col) |
156 |
end if |
157 |
if (allocated(c_idents_Row)) then |
158 |
deallocate(c_idents_Row) |
159 |
endif |
160 |
|
161 |
#else |
162 |
do i = 1, nComponents |
163 |
|
164 |
me = getFirstMatchingElement(atypes, "c_ident", c_idents(i)) |
165 |
atid(i) = me |
166 |
|
167 |
enddo |
168 |
#endif |
169 |
|
170 |
!! Create neighbor lists |
171 |
call expandNeighborList(nComponents, thisStat) |
172 |
if (thisStat /= 0) then |
173 |
status = -1 |
174 |
return |
175 |
endif |
176 |
|
177 |
do i = 1, nExcludes_Local |
178 |
excludesLocal(1,i) = CexcludesLocal(1,i) |
179 |
excludesLocal(2,i) = CexcludesLocal(2,i) |
180 |
enddo |
181 |
|
182 |
do i = 1, nExcludes_Global |
183 |
excludesGlobal(i) = CexcludesGlobal(i) |
184 |
enddo |
185 |
|
186 |
if (status == 0) simulation_setup_complete = .true. |
187 |
|
188 |
end subroutine SimulationSetup |
189 |
|
190 |
subroutine setBox_3d(new_box_size) |
191 |
real(kind=dp), dimension(3) :: new_box_size |
192 |
integer :: smallest, status, i |
193 |
|
194 |
thisSim%box = new_box_size |
195 |
box = thisSim%box |
196 |
|
197 |
smallest = 1 |
198 |
do i = 2, 3 |
199 |
if (new_box_size(i) .lt. new_box_size(smallest)) smallest = i |
200 |
end do |
201 |
if (thisSim%rcut .gt. 0.5_dp * new_box_size(smallest)) & |
202 |
call setRcut(0.5_dp * new_box_size(smallest), status) |
203 |
return |
204 |
end subroutine setBox_3d |
205 |
|
206 |
subroutine setBox_1d(dim, new_box_size) |
207 |
integer :: dim, status |
208 |
real(kind=dp) :: new_box_size |
209 |
thisSim%box(dim) = new_box_size |
210 |
box(dim) = thisSim%box(dim) |
211 |
if (thisSim%rcut .gt. 0.5_dp * new_box_size) & |
212 |
call setRcut(0.5_dp * new_box_size, status) |
213 |
end subroutine setBox_1d |
214 |
|
215 |
subroutine setRcut(new_rcut, status) |
216 |
real(kind = dp) :: new_rcut |
217 |
integer :: myStatus, status |
218 |
thisSim%rcut = new_rcut |
219 |
rcut2 = thisSim%rcut * thisSim%rcut |
220 |
rcut6 = rcut2 * rcut2 * rcut2 |
221 |
myStatus = 0 |
222 |
call LJ_new_rcut(new_rcut, myStatus) |
223 |
if (myStatus .ne. 0) then |
224 |
write(default_error, *) 'LJ module refused our rcut!' |
225 |
status = -1 |
226 |
return |
227 |
endif |
228 |
status = 0 |
229 |
return |
230 |
end subroutine setRcut |
231 |
|
232 |
function getBox_3d() result(thisBox) |
233 |
real( kind = dp ), dimension(3) :: thisBox |
234 |
thisBox = thisSim%box |
235 |
end function getBox_3d |
236 |
|
237 |
function getBox_1d(dim) result(thisBox) |
238 |
integer, intent(in) :: dim |
239 |
real( kind = dp ) :: thisBox |
240 |
|
241 |
thisBox = thisSim%box(dim) |
242 |
end function getBox_1d |
243 |
|
244 |
subroutine getRcut(thisrcut,rc2,rc6,status) |
245 |
real( kind = dp ), intent(out) :: thisrcut |
246 |
real( kind = dp ), intent(out), optional :: rc2 |
247 |
real( kind = dp ), intent(out), optional :: rc6 |
248 |
integer, optional :: status |
249 |
|
250 |
if (present(status)) status = 0 |
251 |
|
252 |
if (.not.simulation_setup_complete ) then |
253 |
if (present(status)) status = -1 |
254 |
return |
255 |
end if |
256 |
|
257 |
thisrcut = thisSim%rcut |
258 |
if(present(rc2)) rc2 = rcut2 |
259 |
if(present(rc6)) rc6 = rcut6 |
260 |
end subroutine getRcut |
261 |
|
262 |
subroutine getRlist(thisrlist,rl2,status) |
263 |
real( kind = dp ), intent(out) :: thisrlist |
264 |
real( kind = dp ), intent(out), optional :: rl2 |
265 |
|
266 |
integer, optional :: status |
267 |
|
268 |
if (present(status)) status = 0 |
269 |
|
270 |
if (.not.simulation_setup_complete ) then |
271 |
if (present(status)) status = -1 |
272 |
return |
273 |
end if |
274 |
|
275 |
thisrlist = thisSim%rlist |
276 |
if(present(rl2)) rl2 = rlist2 |
277 |
end subroutine getRlist |
278 |
|
279 |
function getRrf() result(rrf) |
280 |
real( kind = dp ) :: rrf |
281 |
rrf = thisSim%rrf |
282 |
end function getRrf |
283 |
|
284 |
function getRt() result(rt) |
285 |
real( kind = dp ) :: rt |
286 |
rt = thisSim%rt |
287 |
end function getRt |
288 |
|
289 |
function getDielect() result(dielect) |
290 |
real( kind = dp ) :: dielect |
291 |
dielect = thisSim%dielect |
292 |
end function getDielect |
293 |
|
294 |
function SimUsesPBC() result(doesit) |
295 |
logical :: doesit |
296 |
doesit = thisSim%SIM_uses_PBC |
297 |
end function SimUsesPBC |
298 |
|
299 |
function SimUsesLJ() result(doesit) |
300 |
logical :: doesit |
301 |
doesit = thisSim%SIM_uses_LJ |
302 |
end function SimUsesLJ |
303 |
|
304 |
function SimUsesSticky() result(doesit) |
305 |
logical :: doesit |
306 |
doesit = thisSim%SIM_uses_sticky |
307 |
end function SimUsesSticky |
308 |
|
309 |
function SimUsesDipoles() result(doesit) |
310 |
logical :: doesit |
311 |
doesit = thisSim%SIM_uses_dipoles |
312 |
end function SimUsesDipoles |
313 |
|
314 |
function SimUsesRF() result(doesit) |
315 |
logical :: doesit |
316 |
doesit = thisSim%SIM_uses_RF |
317 |
end function SimUsesRF |
318 |
|
319 |
function SimUsesGB() result(doesit) |
320 |
logical :: doesit |
321 |
doesit = thisSim%SIM_uses_GB |
322 |
end function SimUsesGB |
323 |
|
324 |
function SimUsesEAM() result(doesit) |
325 |
logical :: doesit |
326 |
doesit = thisSim%SIM_uses_EAM |
327 |
end function SimUsesEAM |
328 |
|
329 |
function SimUsesDirectionalAtoms() result(doesit) |
330 |
logical :: doesit |
331 |
doesit = thisSim%SIM_uses_dipoles .or. thisSim%SIM_uses_sticky .or. & |
332 |
thisSim%SIM_uses_GB .or. thisSim%SIM_uses_RF |
333 |
end function SimUsesDirectionalAtoms |
334 |
|
335 |
function SimRequiresPrepairCalc() result(doesit) |
336 |
logical :: doesit |
337 |
doesit = thisSim%SIM_uses_EAM |
338 |
end function SimRequiresPrepairCalc |
339 |
|
340 |
function SimRequiresPostpairCalc() result(doesit) |
341 |
logical :: doesit |
342 |
doesit = thisSim%SIM_uses_RF |
343 |
end function SimRequiresPostpairCalc |
344 |
|
345 |
subroutine InitializeSimGlobals(thisStat) |
346 |
integer, intent(out) :: thisStat |
347 |
integer :: alloc_stat |
348 |
|
349 |
thisStat = 0 |
350 |
|
351 |
call FreeSimGlobals() |
352 |
|
353 |
allocate(excludesLocal(2,nExcludes_Local), stat=alloc_stat) |
354 |
if (alloc_stat /= 0 ) then |
355 |
thisStat = -1 |
356 |
return |
357 |
endif |
358 |
|
359 |
allocate(excludesGlobal(nExcludes_Global), stat=alloc_stat) |
360 |
if (alloc_stat /= 0 ) then |
361 |
thisStat = -1 |
362 |
return |
363 |
endif |
364 |
|
365 |
end subroutine InitializeSimGlobals |
366 |
|
367 |
subroutine FreeSimGlobals() |
368 |
|
369 |
!We free in the opposite order in which we allocate in. |
370 |
|
371 |
if (allocated(excludesGlobal)) deallocate(excludesGlobal) |
372 |
if (allocated(excludesLocal)) deallocate(excludesLocal) |
373 |
|
374 |
end subroutine FreeSimGlobals |
375 |
|
376 |
pure function getNlocal() result(nlocal) |
377 |
integer :: nlocal |
378 |
nlocal = natoms |
379 |
end function getNlocal |
380 |
|
381 |
|
382 |
end module simulation |