ITM AMNS: User Interface  \$Id: Doxyfile 502 2015-10-15 12:23:45Z dpc $
interface_to_amns.F90
Go to the documentation of this file.
1 
11 
12  use amns_types
13  type (amns_cpo_list), pointer :: first => null()
14 
15 contains
16 
17 ! zn: nuclear charge
18 ! za: ionization state
19 ! code reaction type
20 ! grid output data (has a structure of the type "grid")
21 ! inpdata input data: amns cpo
22 ! nproc: number of resulting states
23 ! nz: number of initial states (if bundle: number of initial states)
24 ! tdim(2,nproc): dimesionality (1,nproc) and start positions (2,nproc) of tables (e.g. 1D: only T dependence, 2D: T and n dependence, ...)
25 
31  subroutine get_amns_data(reaction_type,reactants,grid,source,shot,run,backend,user,ds_version,ierr,error_description,debug)
32  use itm_types
33  use amns_utility
34  use f90_kind
35 ! do not remove the comment!!!
36  use euitm_schemas ! IGNORE
37  use euitm_routines ! IGNORE
38  use read_structures ! IGNORE
39  implicit none
40 
41  type (grid_t), intent(out) :: grid
42  character*(answer_length), intent(out) :: source
43  type(amns_reaction_type), intent(in) :: reaction_type
44  type(amns_reactants_type), intent(in) :: reactants
45  real (kind=r8) :: zn, za
46  character (len=*) :: backend, user, ds_version
47  integer :: ierr
48  character*128 :: error_description
49  logical :: debug
50  integer :: iza, nz, nproc, i, k, j, isearch, ndim, ncoord
51  type (amns_cpo_list), pointer :: last, work => null()
52 ! for calling AMNS CPO
53  character(len=5):: treename
54  integer :: idx, shot, run
55  logical :: found, done, match
56  character (len=256) :: file
57  integer :: nreac, nprod, nlhs, nrhs, ireac
58  real (kind=r8) :: zn_cpo, za_cpo
59  ierr=0
60  error_description=''
61  iza = -1
62 
63  work => first
64  last => first
65  found = .false.
66 !! write(*,*) associated(first), associated(last), associated(work)
67  do while(associated(work))
68 !! write(*,*) work%shot, shot
69  if(work%shot.eq.shot.and.work%run.eq.run) then
70  found=.true.
71  exit
72  endif
73  last => work
74  work => work%next
75 !! write(*,*) associated(first), associated(last), associated(work)
76  enddo
77 !! write(*,*) associated(first), associated(last), associated(work)
78  if(.not.found) then
79  if(.not.associated(first)) then
80  allocate(first)
81 !! write(*,*) first%shot, first%run, associated(first%prev), associated(first%next)
82  work => first
83 !! write(*,*) associated(first), associated(work), work%shot
84  else if(.not.associated(last%next)) then
85  allocate(last%next)
86  work => last%next
87  work%prev => last
88  endif
89  if(work%shot.eq.0) then
90  treename = 'euitm'
91  if(backend.eq.'ascii') then
92  write(file,'(a,''_'',i6.6,''_'',i6.6,''.CPO'')') 'amns',shot,run
93  if(debug) write(*,*) 'Reading data from ', trim(file), ' for ', shot, run
94  call open_read_file(1, trim(file))
95  call read_cpo(work%amns_cpo, 'amns')
96  call close_read_file
97  else
98 #ifdef UAL
99  if(backend.eq.'mdsplus') then
100  if(debug) write(*,*) 'Using mdsplus backend'
101  call euitm_open_env(treename,shot,run,idx,trim(user),'amns',ds_version)
102  elseif(backend.eq.'hdf5') then
103  if(debug) write(*,*) 'Using hdf5 backend'
104  call euitm_open_hdf5(treename,shot,run,idx)
105  else
106  error_description = 'Backend not yet supported: ' // trim(backend)
107  write(*,*) error_description
108  ierr=1
109  return
110  endif
111  call euitm_get(idx,'amns',work%amns_cpo)
112  call euitm_close(idx)
113 #else
114  error_description = 'The AMNS routines were compiled without UAL support. ' // &
115  'Use the ascii backend'
116  write(*,*) error_description
117  ierr=2
118  return
119 #endif
120  endif
121  work%shot=shot
122  work%run=run
123  endif
124  endif
125 
126  nproc= SIZE(work%amns_cpo(1)%process)
127 
128  k=0
129  do isearch = 1, nproc
130 ! first look for the right process
131  if(reaction_type%string .ne. work%amns_cpo(1)%process(isearch)%proc_label(1)) cycle
132 ! found the right process, now search for the right reaction pattern
133 ! first calculate the number of reactants and products in the database
134  if(associated(work%amns_cpo(1)%process(isearch)%reactant)) then
135  nreac = size(work%amns_cpo(1)%process(isearch)%reactant)
136  else
137  nreac = 0
138  endif
139  if(associated(work%amns_cpo(1)%process(isearch)%product)) then
140  nprod = size(work%amns_cpo(1)%process(isearch)%product)
141  else
142  nprod = 0
143  endif
144 ! then calculate the number of reactants and products for the requested reaction
145  nlhs=0
146  nrhs=0
147  if(allocated(reactants%components)) then
148  do ireac = 1, size(reactants%components)
149  if(reactants%components(ireac)%lr .eq. 0) then
150  if (nrhs .ne. 0) then
151  error_description = 'Reactants should precede products'
152  write (*,*) error_description
153  ierr=3
154  return
155  endif
156  nlhs = nlhs + 1
157  else if(reactants%components(ireac)%lr .eq. 1) then
158  nrhs = nrhs + 1
159  else
160  write(error_description,*) 'Unknown lr option ', reactants%components(ireac)%lr, &
161  ' for component ', ireac
162  write(*,*) error_description
163  ierr=4
164  return
165  endif
166  enddo
167  endif
168 ! the numbers should match
169  if (debug) write(*,*) 'nreac/nprod', isearch, nreac, nprod, nlhs, nrhs
170  if(nreac .ne. nlhs) cycle
171  if(nprod .ne. nrhs) cycle
172  match = .true.
173 ! check the reactants
174  do ireac = 1, nreac
175 ! now check that the zn's match
176  zn_cpo = 0
177  if(associated(work%amns_cpo(1)%process(isearch)%reactant(ireac)%constituents)) then
178  if(size(work%amns_cpo(1)%process(isearch)%reactant(ireac)%constituents) .eq. 1) then
179 ! if(work%amns_cpo(1)%process(isearch)%reactant(ireac)%constituents(1)%multiplicity .eq. 1) then
180  zn_cpo = work%amns_cpo(1)%process(isearch)%reactant(ireac)%constituents(1)%zn
181 ! else
182 ! write(*,*) 'Case with multiplicity != 1 not yet coded'
183 ! write(*,*) ireac, work%amns_cpo(1)%process(isearch)%reactant(ireac)%constituents(1)%multiplicity
184 ! stop 'ERROR' ! for the moment
185 ! endif
186  else
187  error_description = 'Case with number of constituents != 1 not yet coded'
188  write(*,*) error_description
189  ierr=5
190  return
191  endif
192  else
193  error_description = 'Must have at least one constituent'
194  write(*,*) error_description
195  ierr=6
196  return
197  endif
198  if (debug) write(*,*) 'zn', isearch, ireac, zn_cpo, reactants%components(ireac)%zn
199  match = match .and. (nint(reactants%components(ireac)%zn) .eq. nint(zn_cpo))
200  if (.not. match) cycle
201 ! now check that the za's match
202  za_cpo = 0
203  if(work%amns_cpo(1)%process(isearch)%reactant(ireac)%relative .eq. -1) then
204 ! charge is not relevant
205  if (debug) write(*,*) 'za', isearch, ireac, 'za not relavant'
206  else if(work%amns_cpo(1)%process(isearch)%reactant(ireac)%relative .eq. 0) then
207 ! charge is absolute
208  if (debug) write(*,*) 'za', isearch, ireac, 'absolute charge', work%amns_cpo(1)%process(isearch)%reactant(ireac)%za, reactants%components(ireac)%za
209  match = match .and. (work%amns_cpo(1)%process(isearch)%reactant(ireac)%za .eq. reactants%components(ireac)%za)
210  else if(work%amns_cpo(1)%process(isearch)%reactant(ireac)%relative .eq. 1 ) then
211 ! charge is relative
212 ! ???
213  iza = reactants%components(ireac)%za + 1
214  if (debug) write(*,*) 'za', isearch, ireac, 'relative charge', work%amns_cpo(1)%process(isearch)%reactant(ireac)%za, reactants%components(ireac)%za
215  else
216  write(error_description,*) 'Unrecognized value for relative ', work%amns_cpo(1)%process(isearch)%reactant(ireac)%relative
217  write(*,*) error_description
218  ierr=7
219  return
220  endif
221  if (.not. match) cycle
222 ! now check that the am's match
223  if(debug) then
224  write(*,*) 'am', isearch, ireac, work%amns_cpo(1)%process(isearch)%reactant(ireac)%amn, reactants%components(ireac)%mi
225  endif
226  if(nint(work%amns_cpo(1)%process(isearch)%reactant(ireac)%amn) .ne. 0 .and. nint(reactants%components(ireac)%mi) .ne. 0) then
227  match = match .and. (nint(work%amns_cpo(1)%process(isearch)%reactant(ireac)%amn) .eq. nint(reactants%components(ireac)%mi))
228  endif
229  if (.not. match) cycle
230  enddo
231  if (.not. match) cycle
232 ! check the products
233  do ireac = 1, nprod
234 ! now check that the zn's match
235  zn_cpo = 0
236  if(associated(work%amns_cpo(1)%process(isearch)%product(ireac)%constituents)) then
237  if(size(work%amns_cpo(1)%process(isearch)%product(ireac)%constituents) .eq. 1) then
238 ! if(work%amns_cpo(1)%process(isearch)%product(ireac)%constituents(1)%multiplicity .eq. 1) then
239  zn_cpo = work%amns_cpo(1)%process(isearch)%product(ireac)%constituents(1)%zn
240 ! else
241 ! write(*,*) 'Case with multiplicity != 1 not yet coded'
242 ! stop 'ERROR' ! for the moment
243 ! endif
244  else
245  error_description = 'Case with number of constituents != 1 not yet coded'
246  write(*,*) error_description
247  ierr=8
248  return
249  endif
250  else
251  error_description = 'Must have at least one constituent'
252  write(*,*) error_description
253  ierr=9
254  return
255  endif
256  if (debug) write(*,*) 'zn', isearch, ireac, zn_cpo, reactants%components(ireac+nlhs)%zn
257  match = match .and. (nint(reactants%components(ireac+nlhs)%zn) .eq. nint(zn_cpo))
258 ! now check that the za's match
259  za_cpo = 0
260  if(work%amns_cpo(1)%process(isearch)%product(ireac)%relative .eq. -1) then
261 ! charge is not relevant
262  if (debug) write(*,*) 'za', isearch, ireac, 'za not relavant'
263  else if(work%amns_cpo(1)%process(isearch)%product(ireac)%relative .eq. 0) then
264 ! charge is absolute
265  if (debug) write(*,*) 'za', isearch, ireac, 'absolute charge', work%amns_cpo(1)%process(isearch)%product(ireac)%za, reactants%components(ireac+nlhs)%za
266  match = match .and. (work%amns_cpo(1)%process(isearch)%product(ireac)%za .eq. &
267  reactants%components(ireac+nlhs)%za)
268  else if(work%amns_cpo(1)%process(isearch)%product(ireac)%relative .eq. 1 ) then
269 ! charge is relative
270 ! ???
271  if (debug) write(*,*) 'za', isearch, ireac, 'relative charge', work%amns_cpo(1)%process(isearch)%product(ireac)%za, reactants%components(ireac+nlhs)%za
272  else
273  write(error_description,*) 'Unrecognized value for relative ', work%amns_cpo(1)%process(isearch)%reactant(ireac)%relative
274  write(*,*) error_description
275  ierr=10
276  return
277  endif
278  if (.not. match) cycle
279 ! now check that the am's match
280  if(debug) write(*,*) 'am', isearch, ireac, work%amns_cpo(1)%process(isearch)%product(ireac)%amn, reactants%components(ireac+nlhs)%mi
281  if(nint(work%amns_cpo(1)%process(isearch)%product(ireac)%amn) .ne. 0 .and. nint(reactants%components(ireac+nlhs)%mi) .ne. 0) then
282  match = match .and. (nint(work%amns_cpo(1)%process(isearch)%product(ireac)%amn) .eq. nint(reactants%components(ireac+nlhs)%mi))
283  endif
284  if (.not. match) cycle
285  enddo
286  if (.not. match) cycle
287  match = .true.
288  k = isearch
289  exit
290  enddo
291  if(k .eq. 0) then
292  write(error_description,*) 'GET_AMNS_DATA: Case ',trim(reaction_type%string),' not yet implemented'
293  if(debug) then
294  write(*,*) error_description
295  do isearch = 1, nproc
296  write(*,*) 'Process ', isearch, ' = ' , work%amns_cpo(1)%process(isearch)%proc_label(1)
297  enddo
298  endif
299  ierr=11
300  return
301  endif
302 
303  ndim = work%amns_cpo(1)%tables(k)%ndim ! dimensionality of the requested matrix
304  ncoord=work%amns_cpo(1)%tables(k)%coord_index
305  if(iza .eq. -1) iza=1
306  if(size(work%amns_cpo(1)%tables(k)%table) .eq. 1) iza=1
307  if(iza .gt. size(work%amns_cpo(1)%tables(k)%table)) then
308  error_description = 'iza out of bounds'
309  write(*,*) error_description
310  ierr=12
311  return
312  endif
313 
314 ! some of the 1d functions require 2d tables
315  if(ndim.eq.1) then
316  if(.not.associated(work%amns_cpo(1)%tables(k)%table(iza)%table_1d)) then
317  if (debug) write(*,*) 'Special treatment for 1d functions requiring a 2d table'
318  if(associated(work%amns_cpo(1)%tables(k)%table(iza)%table_2d)) then
319  ndim=2
320  endif
321  endif
322  endif
323 
324  done=.true.
325  select case (ndim)
326  case(1)
327  if(ncoord.gt.0) then
328  call new_grid(grid, &
329  work%amns_cpo(1)%tables(k)%table(iza)%table_1d(:), &
330  work%amns_cpo(1)%tables_coord(ncoord)%coords(1)%coord(:))
331  else
332  call new_grid(grid, &
333  work%amns_cpo(1)%tables(k)%table(iza)%table_1d(:))
334  endif
335  case(2)
336  if(ncoord.gt.0) then
337  call new_grid(grid, &
338  work%amns_cpo(1)%tables(k)%table(iza)%table_2d(:,:), &
339  work%amns_cpo(1)%tables_coord(ncoord)%coords(1)%coord(:), &
340  work%amns_cpo(1)%tables_coord(ncoord)%coords(2)%coord(:))
341  else
342  call new_grid(grid, &
343  work%amns_cpo(1)%tables(k)%table(iza)%table_2d(:,:))
344  endif
345  case(3)
346  if(ncoord.gt.0) then
347  call new_grid(grid, &
348  work%amns_cpo(1)%tables(k)%table(iza)%table_3d(:,:,:), &
349  work%amns_cpo(1)%tables_coord(ncoord)%coords(1)%coord(:), &
350  work%amns_cpo(1)%tables_coord(ncoord)%coords(2)%coord(:), &
351  work%amns_cpo(1)%tables_coord(ncoord)%coords(3)%coord(:))
352  else
353  call new_grid(grid, &
354  work%amns_cpo(1)%tables(k)%table(iza)%table_3d(:,:,:))
355  endif
356  case(4)
357  if(ncoord.gt.0) then
358  call new_grid(grid, &
359  work%amns_cpo(1)%tables(k)%table(iza)%table_4d(:,:,:,:), &
360  work%amns_cpo(1)%tables_coord(ncoord)%coords(1)%coord(:), &
361  work%amns_cpo(1)%tables_coord(ncoord)%coords(2)%coord(:), &
362  work%amns_cpo(1)%tables_coord(ncoord)%coords(3)%coord(:), &
363  work%amns_cpo(1)%tables_coord(ncoord)%coords(4)%coord(:))
364  else
365  call new_grid(grid, &
366  work%amns_cpo(1)%tables(k)%table(iza)%table_4d(:,:,:,:))
367  endif
368  case(5)
369  done=.false.
370  write(*) '0D and 5D can be implemented later'
371  end select
372 
373  if(done) then
374  grid%ndim=work%amns_cpo(1)%tables(k)%ndim
375  if(work%amns_cpo(1)%tables(k)%result_trans == 0) then
376  grid%is_lin= .true.
377  elseif(work%amns_cpo(1)%tables(k)%result_trans == 1) then
378  grid%is_log= .true.
379  else
380  grid%interpol_function = work%amns_cpo(1)%tables(k)%result_trans
381  endif
382  if(associated(work%amns_cpo(1)%tables(k)%state_label)) then
383  if(size(work%amns_cpo(1)%tables(k)%state_label).ge.iza) then
384  grid%state_label=work%amns_cpo(1)%tables(k)%state_label(iza)
385  endif
386  else
387  grid%state_label=''
388  endif
389  if(associated(work%amns_cpo(1)%tables(k)%result_label)) then
390  grid%result_label=work%amns_cpo(1)%tables(k)%result_label(1)
391  else
392  grid%result_label=''
393  endif
394  if(associated(work%amns_cpo(1)%tables(k)%result_unit)) then
395  grid%result_unit=work%amns_cpo(1)%tables(k)%result_unit(1)
396  else
397  grid%result_unit=''
398  endif
399  if(ncoord.gt.0) then
400  do j=1,ndim
401  if(work%amns_cpo(1)%tables_coord(ncoord)%coords(j)%transform == 0) then
402  grid%axe(j)%is_lin= .true.
403  elseif(work%amns_cpo(1)%tables_coord(ncoord)%coords(j)%transform == 1) then
404  grid%axe(j)%is_log= .true.
405  else
406  write(*,*) 'The coordinate transformation is unknown, the process', trim(reaction_type%string)
407  endif
408  grid%axe(j)%lbound= lbound(grid%axe(j)%x, 1)
409  enddo
410  endif
411  endif
412 
413  if(associated(work%amns_cpo(1)%source)) then
414  if(size(work%amns_cpo(1)%source).ge.1) then
415  source=work%amns_cpo(1)%source(1)
416  else
417  source="Not specified in the AMNS CPO"
418  endif
419  else
420  source="Not specified in the AMNS CPO"
421  endif
422 
423  end subroutine get_amns_data
424 
428  subroutine end_amns_data
429 
430  use deallocate_structures ! IGNORE
431  implicit none
432 
433  type (amns_cpo_list), pointer :: work, next => null()
434 
435  work => first
436 
437  do while(associated(work))
438  next => work%next
439  call deallocate_cpo(work%amns_cpo)
440  deallocate(work)
441  work => next
442  enddo
443 
444  first => null()
445 
446  end subroutine end_amns_data
447 
448 end module interface_to_amns
Type for linked list of amns cpos NOT interoperable with C.
Definition: amns_types.F90:154
subroutine end_amns_data
deallocate cpos
for i
Definition: amns.pyx:365
subroutine get_amns_data(reaction_type, reactants, grid, source, shot, run, backend, user, ds_version, ierr, error_description, debug)
transfer data from the CPO to the internal data structure
if error_status np ndarray[np.double_t, ndim=2] np ndarray[np.double_t, ndim=2] np ndarray[np.double_t, ndim=2] ndim
Definition: amns.pyx:287
Module implementing various utility functions for the AMNS interface.
Type used for specifying reactions when using the AMNS interface ("interoperable" version) ...
if error_status answer number def version(self) if error_status &error_status if error_status Reactants reactants
Definition: amns.pyx:59
Type for indicating the reactants when using the AMNS interface NOT interoperable with C...
Definition: amns_types.F90:75
f90_kind module from Silvio Gori's grid package
Definition: f90_kind.F90:9
The derived types defined here are meant to be interoperable with C. The ones for this is not the cas...
Definition: amns_types.F90:17