File: TracerBundle.F90

package info (click to toggle)
gftl 1.3.0%2Bis-really-1.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,328 kB
  • sloc: pascal: 4,163; f90: 3,551; sh: 27; fortran: 16; makefile: 2
file content (436 lines) | stat: -rw-r--r-- 13,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
module TracerMap_mod
   use Tracer_mod
#define _key_string_deferred
#define _key_equal_defined
#define _Key_less_than_defined

#define _value class(Tracer)
#define _value_allocatable

#define _alt
#include "templates/map.inc"
end module TracerMap_mod

module TracerBundle_mod
   use AbstractValue_mod
   use Values_mod
   use FTLAttrMap_mod, only: AttributeMapIterator=>MapIterator
   use AttributeMap_mod
   use Tracer_mod
   use TracerMap_mod, only :TracerMap=>Map,TracerMapIterator=>MapIterator

   implicit none
   
   type :: TracerBundle
      character(len=MAX_LEN_KEY),allocatable :: mandatoryAttributes(:)
      type(AttributeMap) :: defaultValues
      type(TracerMap) :: tracers
   contains
      procedure :: initEmptyBundle
      procedure :: initBwithMan
      procedure :: initBwithManDV
      procedure :: initBwithManDVTracers

      generic :: init=>initEmptyBundle,initBwithMan,initBwithManDV,initBwithManDVTracers
      
      procedure :: addDefaultAttribute0
      procedure :: addDefaultAttribute1
      generic :: addDefaultAttribute=>addDefaultAttribute0,addDefaultAttribute1

      procedure :: addMandatoryAttribute
      procedure :: addTracer
      procedure :: getTracer
      procedure :: setTracer
      procedure :: hasTracer
      procedure :: isQualifiedTracer

      procedure :: addTracerAttribute0
      procedure :: addTracerAttribute1
      generic :: addTracerAttribute=>addTracerAttribute0,addTracerAttribute1

      procedure :: getTracerAttribute0
      procedure :: getTracerAttribute1
      generic :: getTracerAttribute=>getTracerAttribute0,getTracerAttribute1

      procedure :: setTracerAttribute0
      procedure :: setTracerAttribute1
      generic :: setTracerAttribute=>setTracerAttribute0,setTracerAttribute1

      procedure :: clearTracerBundle
      generic :: clear =>clearTracerBundle

      procedure :: printIt
      generic :: print=>printIt
   end type
contains

   subroutine initEmptyBundle(this)
      class(TracerBundle), intent(inout) :: this
      call this%defaultValues%init()
      allocate(this%mandatoryAttributes(0))
   end subroutine initEmptyBundle

   subroutine initBwithMan(this,mAttr)
      class(TracerBundle), intent(inout) :: this
      character(len=MAX_LEN_KEY),intent(in) :: mAttr(:)
      !allocate(this%mandatoryAttributes(1:size(mAttr,1)), source = mAttr)
      this%mandatoryAttributes = mAttr
      call this%defaultValues%init()
   end subroutine initBwithMan

   subroutine initBwithManDV(this,mAttr,dValues)
      class(TracerBundle), intent(inout) :: this
      character(len=MAX_LEN_KEY),intent(in) :: mAttr(:)
      type(AttributeMap),intent(in) :: dValues

      this%mandatoryAttributes = mAttr
      this%defaultValues = dValues
   end subroutine initBwithManDV

   subroutine initBwithManDVTracers(this,mAttr,dValues,tracers)
      class(TracerBundle), intent(inout) :: this
      character(len=MAX_LEN_KEY),intent(in) :: mAttr(:)
      type(AttributeMap),intent(in) :: dValues
      class(TracerMap),intent(in) :: tracers
      type(TracerMapIterator) :: iter
      integer :: i

      this%mandatoryAttributes = mAttr
      this%defaultValues = dValues
      
      iter = tracers%begin()
      do while (iter /= tracers%end())
         call this%addTracer(iter%value())
         call iter%next()
      enddo 

   end subroutine initBwithManDVTracers

   subroutine addMandatoryAttribute(this,attrName)
      class(TracerBundle),intent(inout) :: this
      character(len=*):: attrName
      integer :: n
      type(TracerMapIterator) :: iter
      type(Tracer),pointer :: tp
      character(len=MAX_LEN_KEY), allocatable :: oldValues(:)
  
      iter = this%tracers%begin()
      do while (iter /= this%tracers%end())
         tp=>iter%value()
         if(.not. tp%hasAttribute(attrName)) return        
         call iter%next()
      enddo

      n = size(this%mandatoryAttributes)
      call move_alloc(this%mandatoryAttributes,oldValues)
      allocate(this%mandatoryAttributes(n+1))
      this%mandatoryAttributes(1:n)=oldValues
      this%mandatoryAttributes(n+1) = attrName

   end subroutine addMandatoryAttribute

   subroutine addDefaultAttribute0(this,attrName,value)
      class(TracerBundle),intent(inout) :: this
      character(len=*),intent(in) :: attrName
      class(*),intent(in) :: value
      type(TracerMapIterator) :: iter
      type(Tracer),pointer :: tp

      call this%defaultValues%insertValue(attrName,value)
      
      iter = this%tracers%begin()
      do while (iter /= this%tracers%end())
         tp=>iter%value()
         if(.not. tp%hasAttribute(attrName)) then
            call tp%attributes%insertValue(attrName,value)
         endif        
         call iter%next()
         nullify(tp)
      enddo
   end subroutine addDefaultAttribute0

   subroutine addDefaultAttribute1(this,attrName,value)
      class(TracerBundle),intent(inout) :: this
      character(len=*),intent(in) :: attrName
      class(*),intent(in) :: value(:)
      type(TracerMapIterator) :: iter
      type(Tracer),pointer :: tp
      character(len=MAX_LEN_ATTRIBUTE_STRING),allocatable:: s1(:)
      integer :: n

      select type (value)
      type is (character(len=*))
         allocate(s1(size(value)))
         s1=value
         call this%defaultValues%insertValue(trim(attrName),s1)
   
         iter = this%tracers%begin()
         do while (iter /= this%tracers%end())
            tp=>iter%value()
            if(.not. tp%hasAttribute(trim(attrName))) then
               call tp%attributes%insertValue(trim(attrName),s1)
            endif        
            call iter%next()
            nullify(tp)
         enddo
         deallocate(s1)
         return
      class default
      end select
   
      call this%defaultValues%insertValue(trim(attrName),value)
   
      iter = this%tracers%begin()
      do while (iter /= this%tracers%end())
         tp=>iter%value()
         if(.not. tp%hasAttribute(trim(attrName))) then
            call tp%attributes%insertValue(trim(attrName),value)
         endif        
         call iter%next()
         nullify(tp)
      enddo

   end subroutine addDefaultAttribute1

   function isQualifiedTracer(this,trcr) result(res)
      class(TracerBundle),intent(inout) :: this
      type(Tracer),intent(in) :: trcr
      logical :: res

      type(AttributeMapIterator) :: iter
      integer :: i 
  
      ! if there is no mandatory attributes, it is qualified. 
      res = .true.
      do i = 1,size(this%mandatoryAttributes)
         res = .false.
         if(trcr%hasAttribute(trim(this%mandatoryAttributes(i)))) then
           res = .true.
           return
         endif
      enddo

   end function isQualifiedTracer

   subroutine addTracer(this,trcr)
      class(TracerBundle),intent(inout) :: this
      type(Tracer),intent(in) :: trcr
      type(AttributeMapIterator) :: iter
      type(TracerMapIterator) :: iterT
      type(Tracer),pointer :: tp
      class(Tracer),allocatable :: tmpTrcr

      if( .not. this%isQualifiedTracer(trcr)) return

      allocate(tmpTrcr,source = trcr)
      iter = this%defaultValues%begin()
      do while (iter /= this%defaultValues%end())
         if( .not. tmpTrcr%hasAttribute(iter%key())) then
            call tmpTrcr%addAttribute(iter%key(),iter%value())
         endif
         call iter%next()
      enddo
      call this%tracers%insert(tmpTrcr%name,tmpTrcr)

   end subroutine addTracer
 
   subroutine setTracer(this,trcr)
      class(TracerBundle),intent(inout) :: this
      type(Tracer),intent(in) :: trcr
      type(Tracer) :: tmpTracer
      type(AttributeMapIterator) :: iter
      integer :: i 

      if( .not. this%isQualifiedTracer(trcr)) return

      tmpTracer = trcr
      iter = this%defaultValues%begin()
      do while (iter /= this%defaultValues%end())
         if( .not. tmpTracer%hasAttribute(iter%key())) then
           call tmpTracer%addAttribute(iter%key(),iter%value())
         endif
         call iter%next()
      enddo
      call this%tracers%set(tmpTracer%name,tmpTracer)

   end subroutine setTracer

   function getTracer(this,name,trPtr) result(res)
      class(TracerBundle),intent(inout) :: this
      character(len=*),intent(in) ::name
      class(Tracer),pointer,intent(inout) :: trPtr
      logical :: res
      res= this%tracers%get(name,trPtr)
   end function getTracer

   function hasTracer(this,tName) result (has)
      class(TracerBundle),intent(in) :: this
      character(len=*),intent(in) :: tName
      logical :: has
      has = ( this%tracers%find(trim(tName)) /= this%tracers%end())
   end function

   subroutine addTracerAttribute0(this,tName,attrName,value)
      class(TracerBundle),intent(inout) :: this
      character(len=*),intent(in) :: tName
      character(len=*),intent(in) :: attrName
      class (*), intent(in) :: value
      type(TracerMapIterator) :: iter
      type(Tracer),pointer :: tp

      iter = this%tracers%find(trim(tName))

      if ( iter /= this%tracers%end()) then
         tp=>iter%value()
         call tp%attributes%insertValue(attrName,value)
      endif
   end subroutine

   subroutine addTracerAttribute1(this,tName,attrName,value)
      class(TracerBundle),intent(inout) :: this
      character(len=*),intent(in) :: tName
      character(len=*),intent(in) :: attrName
      class (*), intent(in) :: value(:)
      type(TracerMapIterator) :: iter
      type(Tracer),pointer :: tp
      character(len=MAX_LEN_ATTRIBUTE_STRING),allocatable:: s1(:)

      iter = this%tracers%find(trim(tName))
      if ( iter /= this%tracers%end()) then
         tp=>iter%value()

         ! work around for gfortran
         select type (value)
         type is (character(len=*))
            allocate(s1(size(value)))
            s1=value
            call tp%attributes%insertValue(trim(attrName),s1)
            deallocate(s1)
            return
         class default
         end select
         
         call tp%attributes%insertValue(trim(attrName),value)
      endif

   end subroutine addTracerAttribute1

   subroutine setTracerAttribute0(this,tName,attrName,value)
      class(TracerBundle),intent(inout) :: this
      character(len=*),intent(in) :: tName
      character(len=*),intent(in) :: attrName
      class (*), intent(in) :: value
      type(TracerMapIterator) :: iter
      type(Tracer),pointer :: tp

      iter = this%tracers%find(trim(tName))
      if ( iter /= this%tracers%end()) then
         tp=>iter%value()
         call tp%attributes%setValue(trim(attrName),value)
      endif

   end subroutine setTracerAttribute0

   subroutine setTracerAttribute1(this,tName,attrName,value)
      class(TracerBundle),intent(inout) :: this
      character(len=*),intent(in) :: tName
      character(len=*),intent(in) :: attrName
      class (*), intent(in) :: value(:)
      type(TracerMapIterator) :: iter
      type(Tracer),pointer :: tp
      character(len=MAX_LEN_ATTRIBUTE_STRING),allocatable:: s1(:)

      iter = this%tracers%find(trim(tName))
      if ( iter /= this%tracers%end()) then
         tp=>iter%value()
         ! work around for gfortran
         select type (value)
         type is (character(len=*))
            allocate(s1(size(value)))
            s1=value
            call tp%attributes%setValue(trim(attrName),s1)
            deallocate(s1)
            return
         class default
         end select

         call tp%attributes%setValue(trim(attrName),value)
      endif
   end subroutine setTracerAttribute1

   function getTracerAttribute0(this,tName,attrName,value) result(res)
      class(TracerBundle),intent(inout) :: this
      character(len=*),intent(in) :: tName
      character(len=*),intent(in) :: attrName
      class (*), intent(inout) :: value
      type(TracerMapIterator) :: iter
      type(Tracer),pointer :: tp
      logical:: res

      res = .false.

      iter = this%tracers%find(trim(tName))

      if ( iter /= this%tracers%end()) then
         tp=>iter%value()
         res = tp%attributes%getValue(trim(attrName),value)
      endif
   end function getTracerAttribute0

   function getTracerAttribute1(this,tName,attrName,value) result(res)
      class(TracerBundle),intent(inout) :: this
      character(len=*),intent(in) :: tName
      character(len=*),intent(in) :: attrName
      class (*), intent(inout) :: value(:)
      type(TracerMapIterator) :: iter
      type(Tracer),pointer :: tp
      logical:: res
      character(len=MAX_LEN_ATTRIBUTE_STRING),allocatable:: s1(:)

      res = .false.

      iter = this%tracers%find(trim(tName))
      if ( iter /= this%tracers%end()) then
         tp=>iter%value()
         ! work around for gfortran
         select type (value)
         type is (character(len=*))
            allocate(s1(size(value)))
            res= tp%attributes%getValue(trim(attrName),s1)
            value=s1
            deallocate(s1)
            return
         class default
         end select
         res = tp%attributes%getValue(trim(attrName),value)
      endif
   end function getTracerAttribute1

   subroutine clearTracerBundle(this)

      class(TracerBundle),intent(inout) :: this
      
      if(this%tracers%size() /=0) call this%tracers%clear()
      if(this%defaultValues%size() /=0) call this%defaultValues%clear()
      deallocate(this%mandatoryAttributes)

   end subroutine

   subroutine printIt(this)

      class(TracerBundle),intent(inout) :: this
      type(TracerMapIterator) :: iter
      type(Tracer),pointer :: p
      print*, "default values: "
      call this%defaultValues%print()
      print*, "loop tracers: "
      iter = this%tracers%begin()
      do while (iter /= this%tracers%end())
         p=>iter%value()
         call p%print()
         call iter%next()
      end do

   end subroutine printIt

end module TracerBundle_mod