V3FIT
bivariate.f
Go to the documentation of this file.
1 !*******************************************************************************
4 !
5 ! Note separating the Doxygen comment block here so detailed decription is
6 ! found in the Module not the file.
7 !
11 !*******************************************************************************
12  MODULE bivariate
13 
14  USE stel_kinds
15  USE profiler
16 
17  IMPLICIT NONE
18 
19 !*******************************************************************************
20 ! DERIVED-TYPE DECLARATIONS
21 ! 1) bivariate_type
22 !
23 !*******************************************************************************
24 !-------------------------------------------------------------------------------
26 !-------------------------------------------------------------------------------
29  INTEGER :: nsu
31  INTEGER :: nrz
33  INTEGER, DIMENSION(:), POINTER :: irz11_bi => null()
35  INTEGER, DIMENSION(:), POINTER :: irz12_bi => null()
37  INTEGER, DIMENSION(:), POINTER :: irz21_bi => null()
39  INTEGER, DIMENSION(:), POINTER :: irz22_bi => null()
41  REAL(rprec), DIMENSION(:), POINTER :: w11_bi => null()
43  REAL(rprec), DIMENSION(:), POINTER :: w12_bi => null()
45  REAL(rprec), DIMENSION(:), POINTER :: w21_bi => null()
47  REAL(rprec), DIMENSION(:), POINTER :: w22_bi => null()
48  END TYPE
49 
50 *******************************************************************************
51 ! INTERFACE BLOCKS
52 !*******************************************************************************
53 !-------------------------------------------------------------------------------
56 !-------------------------------------------------------------------------------
58  MODULE PROCEDURE bivariate_set_grids_1d, &
60  END INTERFACE
61 
62  CONTAINS
63 !*******************************************************************************
64 ! CONSTRUCTION SUBROUTINES
65 !*******************************************************************************
66 !-------------------------------------------------------------------------------
74 !-------------------------------------------------------------------------------
75  FUNCTION bivariate_construct(ns, nu)
76 
77  IMPLICIT NONE
78 
79 ! Declare Arguments
80  TYPE (bivariate_type), POINTER :: bivariate_construct
81  INTEGER, INTENT(in) :: ns
82  INTEGER, INTENT(in) :: nu
83 
84 ! local variables
85  REAL (rprec) :: start_time
86 
87 ! Start of executable code
88  start_time = profiler_get_start_time()
89 
90  ALLOCATE(bivariate_construct)
91 
92  bivariate_construct%nsu = ns*nu
93 
94  ALLOCATE (bivariate_construct%irz11_bi(bivariate_construct%nsu))
95  ALLOCATE (bivariate_construct%irz12_bi(bivariate_construct%nsu))
96  ALLOCATE (bivariate_construct%irz21_bi(bivariate_construct%nsu))
97  ALLOCATE (bivariate_construct%irz22_bi(bivariate_construct%nsu))
98  ALLOCATE (bivariate_construct%w11_bi(bivariate_construct%nsu))
99  ALLOCATE (bivariate_construct%w12_bi(bivariate_construct%nsu))
100  ALLOCATE (bivariate_construct%w21_bi(bivariate_construct%nsu))
101  ALLOCATE (bivariate_construct%w22_bi(bivariate_construct%nsu))
102 
103  CALL profiler_set_stop_time('bivariate_construct', start_time)
104 
105  END FUNCTION
106 
107 !*******************************************************************************
108 ! DESTRUCTION SUBROUTINES
109 !*******************************************************************************
110 !-------------------------------------------------------------------------------
116 !-------------------------------------------------------------------------------
117  SUBROUTINE bivariate_destruct(this)
118 
119  IMPLICIT NONE
120 
121 ! Declare Arguments
122  TYPE (bivariate_type), POINTER :: this
123 
124 ! Start of executable code
125  IF (ASSOCIATED(this%irz11_bi)) THEN
126  DEALLOCATE (this%irz11_bi)
127  this%irz11_bi => null()
128  END IF
129 
130  IF (ASSOCIATED(this%irz12_bi)) THEN
131  DEALLOCATE (this%irz12_bi)
132  this%irz12_bi => null()
133  END IF
134 
135  IF (ASSOCIATED(this%irz21_bi)) THEN
136  DEALLOCATE (this%irz21_bi)
137  this%irz21_bi => null()
138  END IF
139 
140  IF (ASSOCIATED(this%irz22_bi)) THEN
141  DEALLOCATE (this%irz22_bi)
142  this%irz22_bi => null()
143  END IF
144 
145  IF (ASSOCIATED(this%w11_bi)) THEN
146  DEALLOCATE (this%w11_bi)
147  this%w11_bi => null()
148  END IF
149 
150  IF (ASSOCIATED(this%w12_bi)) THEN
151  DEALLOCATE (this%w12_bi)
152  this%w12_bi => null()
153  END IF
154 
155  IF (ASSOCIATED(this%w21_bi)) THEN
156  DEALLOCATE (this%w21_bi)
157  this%w21_bi => null()
158  END IF
159 
160  IF (ASSOCIATED(this%w22_bi)) THEN
161  DEALLOCATE (this%w22_bi)
162  this%w22_bi => null()
163  END IF
164 
165  DEALLOCATE(this)
166 
167  END SUBROUTINE
168 
169 !*******************************************************************************
170 ! SETTER SUBROUTINES
171 !*******************************************************************************
172 !-------------------------------------------------------------------------------
185 !-------------------------------------------------------------------------------
186  SUBROUTINE bivariate_set_grids_2d(this, rsu, zsu, rgrid, zgrid)
187 
188  IMPLICIT NONE
189 
190 ! Declare Arguments
191  TYPE (bivariate_type), INTENT(inout) :: this
192  REAL(rprec), DIMENSION(:,:), INTENT(in) :: rsu
193  REAL(rprec), DIMENSION(:,:), INTENT(in) :: zsu
194  REAL(rprec), DIMENSION(:), INTENT(in) :: rgrid
195  REAL(rprec), DIMENSION(:), INTENT(in) :: zgrid
196 
197 ! local variables
198  INTEGER :: ku, js
199  INTEGER :: ns, nu
200  INTEGER :: nr, nz
201  INTEGER :: index1d
202  INTEGER :: ir, jz
203  INTEGER :: ir1, jz1
204  REAL(rprec) :: rad0, zee0
205  REAL(rprec) :: ri, zj
206  REAL(rprec) :: pr, qz
207  REAL(rprec) :: temp
208  REAL(rprec) :: delr, delz
209  REAL (rprec) :: start_time
210 
211 ! Start of executable code
212  start_time = profiler_get_start_time()
213 
214  nr = SIZE(rgrid, 1)
215  nz = SIZE(zgrid, 1)
216  this%nrz = nr*nz
217  ns = SIZE(rsu, 1)
218  nu = SIZE(rsu, 2)
219 
220  delr = rgrid(2) - rgrid(1)
221  delz = zgrid(2) - zgrid(1)
222 
223  index1d = 0
224 
225  DO ku = 1, nu
226  DO js = 1, ns
227 
228  index1d = index1d + 1
229 !
230 ! CHECK THAT BOUNDARY POINTS ARE INSIDE GRID. IF NOT, STOP!
231 !
232  rad0 = rsu(js, ku)
233  zee0 = zsu(js, ku)
234 
235  IF (rad0.lt.rgrid(1) .or. rad0.gt.rgrid(nr) .or. &
236  & zee0.lt.zgrid(1) .or. zee0.gt.zgrid(nz)) THEN
237  stop 'Plasma point outside response function grid!'
238  END IF
239 !
240 ! DETERMINE INTEGER INDICES (IR,JZ) FOR LOWER LEFT R, Z CORNER GRID POINT
241 !
242  ir = int((rad0 - rgrid(1))/delr) + 1
243  jz = int((zee0 - zgrid(1))/delz) + 1
244  ir1 = min(nr, ir + 1)
245  jz1 = min(nz, jz + 1)
246 
247 !
248 ! STORE INDICES IN 1D ARRAYS
249 !
250  this%irz11_bi(index1d) = ir + nr*(jz - 1)
251  this%irz22_bi(index1d) = ir1 + nr*(jz1 - 1)
252  this%irz12_bi(index1d) = ir + nr*(jz1 - 1)
253  this%irz21_bi(index1d) = ir1 + nr*(jz - 1)
254 !
255 ! COMPUTE RI, ZJ AND PR , QZ AT GRID POINT (IR , JZ)
256 ! ALSO, COMPUTE WEIGHTS WIJ FOR 4 CORNER GRID POINTS
257 !
258  ri = rgrid(ir)
259  zj = zgrid(jz)
260  pr = (rad0 - ri)/delr
261  qz = (zee0 - zj)/delz
262  temp = pr*qz
263  this%w22_bi(index1d) = temp !p*q
264  this%w21_bi(index1d) = pr - temp !p*(1-q)
265  this%w12_bi(index1d) = qz - temp !q*(1-p)
266  this%w11_bi(index1d) = 1 + temp - (pr + qz) !(1-p)*(1-q)
267 
268  END DO
269  END DO
270 
271  CALL profiler_set_stop_time('bivariate_set_grids_2d', start_time)
272 
273  END SUBROUTINE
274 
275 !-------------------------------------------------------------------------------
288 !-------------------------------------------------------------------------------
289  SUBROUTINE bivariate_set_grids_1d(this, rsu, zsu, rgrid, zgrid)
290 
291  IMPLICIT NONE
292 
293 ! Declare Arguments
294  TYPE (bivariate_type), INTENT(inout) :: this
295  REAL(rprec), DIMENSION(:), INTENT(in) :: rsu
296  REAL(rprec), DIMENSION(:), INTENT(in) :: zsu
297  REAL(rprec), DIMENSION(:), INTENT(in) :: rgrid
298  REAL(rprec), DIMENSION(:), INTENT(in) :: zgrid
299 
300 ! local variables
301  INTEGER :: ku
302  INTEGER :: ns, nu
303  INTEGER :: nr, nz
304  INTEGER :: index1d
305  INTEGER :: ir, jz
306  INTEGER :: ir1, jz1
307  REAL(rprec) :: rad0, zee0
308  REAL(rprec) :: ri, zj
309  REAL(rprec) :: pr, qz
310  REAL(rprec) :: temp
311  REAL(rprec) :: delr, delz
312  REAL (rprec) :: start_time
313 
314 ! Start of executable code
315  start_time = profiler_get_start_time()
316 
317  nr = SIZE(rgrid, 1)
318  nz = SIZE(zgrid, 1)
319  this%nrz = nr*nz
320  ns = 1
321  nu = SIZE(rsu)
322 
323  delr = rgrid(2) - rgrid(1)
324  delz = zgrid(2) - zgrid(1)
325 
326  index1d = 0
327 
328  DO ku = 1, nu
329  index1d = index1d + 1
330 !
331 ! CHECK THAT BOUNDARY POINTS ARE INSIDE GRID. IF NOT, STOP!
332 !
333  rad0 = rsu(ku)
334  zee0 = zsu(ku)
335 
336  IF (rad0.lt.rgrid(1) .or. rad0.gt.rgrid(nr) .or. &
337  & zee0.lt.zgrid(1) .or. zee0.gt.zgrid(nz)) THEN
338  stop 'Plasma point outside response function grid!'
339  END IF
340 !
341 ! DETERMINE INTEGER INDICES (IR,JZ) FOR LOWER LEFT R, Z CORNER GRID POINT
342 !
343  ir = int((rad0 - rgrid(1))/delr) + 1
344  jz = int((zee0 - zgrid(1))/delz) + 1
345  ir1 = min(nr, ir + 1)
346  jz1 = min(nz, jz + 1)
347 
348 !
349 ! STORE INDICES IN 1D ARRAYS
350 !
351  this%irz11_bi(index1d) = ir + nr*(jz - 1)
352  this%irz22_bi(index1d) = ir1 + nr*(jz1 - 1)
353  this%irz12_bi(index1d) = ir + nr*(jz1 - 1)
354  this%irz21_bi(index1d) = ir1 + nr*(jz - 1)
355 !
356 ! COMPUTE RI, ZJ AND PR , QZ AT GRID POINT (IR , JZ)
357 ! ALSO, COMPUTE WEIGHTS WIJ FOR 4 CORNER GRID POINTS
358 !
359  ri = rgrid(ir)
360  zj = zgrid(jz)
361  pr = (rad0 - ri)/delr
362  qz = (zee0 - zj)/delz
363  temp = pr*qz
364  this%w22_bi(index1d) = temp !p*q
365  this%w21_bi(index1d) = pr - temp !p*(1-q)
366  this%w12_bi(index1d) = qz - temp !q*(1-p)
367  this%w11_bi(index1d) = 1 + temp - (pr + qz) !(1-p)*(1-q)
368  END DO
369 
370  CALL profiler_set_stop_time('bivariate_set_grids_2d', start_time)
371 
372  END SUBROUTINE
373 
374 !*******************************************************************************
375 ! GETTER SUBROUTINES
376 !*******************************************************************************
377 !-------------------------------------------------------------------------------
389 !-------------------------------------------------------------------------------
390  SUBROUTINE bivariate_get_4pt(this, resp_rz, resp_su)
391  IMPLICIT NONE
392  TYPE (bivariate_type), INTENT(inout) :: this
393  REAL(rprec), DIMENSION(this%nrz), INTENT(in) :: resp_rz
394  REAL(rprec), DIMENSION(this%nsu), INTENT(out) :: resp_su
395 
396 ! local variables
397  INTEGER :: jsu
398  REAL (rprec) :: start_time
399 
400 ! Start of executable code
401  start_time = profiler_get_start_time()
402 
403  DO jsu = 1, this%nsu
404 !
405 ! COMPUTE RESPONSE AT R, PHI (fixed kin), Z BY 4-PT INTERPOLATION
406 !
407  resp_su(jsu) = this%w11_bi(jsu)*resp_rz(this%irz11_bi(jsu)) &
408  & + this%w22_bi(jsu)*resp_rz(this%irz22_bi(jsu)) &
409  & + this%w21_bi(jsu)*resp_rz(this%irz21_bi(jsu)) &
410  & + this%w12_bi(jsu)*resp_rz(this%irz12_bi(jsu))
411 
412  END DO
413 
414  CALL profiler_set_stop_time('bivariate_get_4pt', start_time)
415 
416  END SUBROUTINE
417 
418  END MODULE
profiler
Defines functions for measuring an tabulating performance of function and subroutine calls....
Definition: profiler.f:13
bivariate::bivariate_construct
type(bivariate_type) function, pointer bivariate_construct(ns, nu)
Construct a bivariate_type object.
Definition: bivariate.f:76
bivariate::bivariate_type
An object containing persistent data for the bivariate interpolation.
Definition: bivariate.f:27
bivariate::bivariate_destruct
subroutine bivariate_destruct(this)
Deconstruct a bivariate_type object.
Definition: bivariate.f:118
bivariate::bivariate_set_grids
Interface for the setting of bivariate_type types either using bivariate_set_grids_1d or bivariate_se...
Definition: bivariate.f:57
profiler::profiler_get_start_time
real(rprec) function profiler_get_start_time()
Gets the start time of profiled function.
Definition: profiler.f:194
bivariate::bivariate_set_grids_1d
subroutine bivariate_set_grids_1d(this, rsu, zsu, rgrid, zgrid)
Set up the interpolation grid for a phi plane.
Definition: bivariate.f:290
bivariate::bivariate_set_grids_2d
subroutine bivariate_set_grids_2d(this, rsu, zsu, rgrid, zgrid)
Set up the interpolation grid for a phi plane.
Definition: bivariate.f:187
bivariate::bivariate_get_4pt
subroutine bivariate_get_4pt(this, resp_rz, resp_su)
Interpolate points on to responce function grid.
Definition: bivariate.f:391
profiler::profiler_set_stop_time
subroutine profiler_set_stop_time(symbol_name, start_time)
Gets the end time of profiled function.
Definition: profiler.f:121
bivariate
This modules contains routines for interpolating points inside a grid. This was originally written by...
Definition: bivariate.f:12