This repository was archived by the owner on Mar 14, 2023. It is now read-only.
forked from openmc-dev/openmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_interface.F90
531 lines (388 loc) · 16.2 KB
/
xml_interface.F90
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
module xml_interface
use constants, only: MAX_LINE_LEN
use error, only: fatal_error
use openmc_fox
implicit none
private
public :: Node
public :: NodeList
public :: open_xmldoc
public :: close_xmldoc
public :: check_for_node
public :: get_node_ptr
public :: get_node_list
public :: get_list_size
public :: get_list_item
public :: get_node_value
public :: get_node_array
public :: get_arraysize_integer
public :: get_arraysize_double
public :: get_arraysize_string
integer, parameter :: ATTR_NODE = 0
integer, parameter :: ELEM_NODE = 1
interface get_node_value
module procedure get_node_value_integer
module procedure get_node_value_long
module procedure get_node_value_double
module procedure get_node_value_string
end interface get_node_value
interface get_node_array
module procedure get_node_array_integer
module procedure get_node_array_double
module procedure get_node_array_string
end interface get_node_array
contains
!===============================================================================
! OPEN_XMLDOC opens and parses an XML and returns a pointer to the document
!===============================================================================
subroutine open_xmldoc(ptr, filename)
character(len=*) :: filename
type(Node), pointer :: ptr
ptr => parseFile(trim(filename)) ! Pointer to the whole XML document
ptr => getDocumentElement(ptr) ! Grabs root element of XML document
end subroutine open_xmldoc
!===============================================================================
! CLOSE_XMLDOC closes and destroys all memory associated with document
!===============================================================================
subroutine close_xmldoc(ptr)
type(Node), pointer :: ptr
ptr => getParentNode(ptr) ! Go from root element ptr to document ptr
call destroy(ptr) ! Deallocates all nodes recursively
end subroutine close_xmldoc
!===============================================================================
! CHECK_FOR_NODE checks for an attribute or sub-element node with the given
! node name. This should only be used for checking a single occurance of a
! sub-element node. To check for sub-element nodes that repeat, use
! get_node_list and get_list_size. This is to minimize number of calls
! to getChildrenByTagName.
!===============================================================================
function check_for_node(ptr, node_name) result(found)
character(len=*), intent(in) :: node_name
logical :: found
type(Node), pointer, intent(in) :: ptr
type(Node), pointer :: temp_ptr
type(NodeList), pointer :: elem_list
! Default that we found it and attribute
found = .true.
! Get the attribute node
temp_ptr => getAttributeNode(ptr, trim(node_name))
! Check if node exists
if (associated(temp_ptr)) return
! Check for a sub-element
elem_list => getChildrenByTagName(ptr, trim(node_name))
! Get the length of the list
if (getLength(elem_list) == 0) then
found = .false.
return
end if
end function check_for_node
!===============================================================================
! GET_NODE_PTR returns a Node pointer to an attribute or sub-element node.
! Note, this should only be used for sub-element nodes that occur only once.
! For repeated nodes, use get_node_list and then get_item_item.
!===============================================================================
subroutine get_node_ptr(in_ptr, node_name, out_ptr, found)
character(len=*), intent(in) :: node_name
logical, intent(out), optional :: found
type(Node), pointer, intent(in) :: in_ptr
type(Node), pointer, intent(out) :: out_ptr
logical :: found_
type(NodeList), pointer :: elem_list
! Set found to false
found_ = .false.
! Check for a sub-element
elem_list => getChildrenByTagName(in_ptr, trim(node_name))
! Get the length of the list
if (getLength(elem_list) == 0) return
! Point to the new element
out_ptr => item(elem_list, 0)
found_ = .true.
! Check to output found
if (present(found)) found = found_
end subroutine get_node_ptr
!===============================================================================
! GET_NODE_LIST is used to get a pointer to a list of sub-element nodes
!===============================================================================
subroutine get_node_list(in_ptr, node_name, out_ptr)
character(len=*), intent(in) :: node_name
type(Node), pointer, intent(in) :: in_ptr
type(NodeList), pointer, intent(out) :: out_ptr
! Check for a sub-element
out_ptr => getChildrenByTagName(in_ptr, trim(node_name))
end subroutine get_node_list
!===============================================================================
! GET_LIST_SIZE is used to get the number of elements from a node list
!===============================================================================
function get_list_size(in_ptr) result(n_size)
integer :: n_size
type(NodeList), pointer, intent(in) :: in_ptr
! Get the size of the list
n_size = getLength(in_ptr)
end function get_list_size
!===============================================================================
! GET_LIST_ITEM is used to select a specific item from a node list
!===============================================================================
subroutine get_list_item(in_ptr, idx, out_ptr)
integer, intent(in) :: idx
type(NodeList), pointer, intent(in) :: in_ptr
type(Node), pointer, intent(out) :: out_ptr
! Check for a sub-element
out_ptr => item(in_ptr, idx - 1)
end subroutine get_list_item
!===============================================================================
! GET_NODE_VALUE_INTEGER returns a integer value from an attribute or node
!===============================================================================
subroutine get_node_value_integer(ptr, node_name, result_int)
character(len=*), intent(in) :: node_name
integer :: result_int
type(Node), pointer, intent(in) :: ptr
integer :: node_type
logical :: found
type(Node), pointer :: temp_ptr
! Get pointer to the node
call get_node(ptr, node_name, temp_ptr, node_type, found)
! Leave if it was not found
if (.not. found) then
call fatal_error("Node " // node_name // " not part of Node " &
&// getNodeName(ptr) // ".")
end if
! Extract value
if (node_type == ATTR_NODE) then
call extractDataAttribute(ptr, node_name, result_int)
else
call extractDataContent(temp_ptr, result_int)
end if
end subroutine get_node_value_integer
!===============================================================================
! GET_NODE_VALUE_LONG returns an 8-byte integer from attribute or node
!===============================================================================
subroutine get_node_value_long(ptr, node_name, result_long)
character(len=*), intent(in) :: node_name
integer(8) :: result_long
type(Node), pointer, intent(in) :: ptr
integer :: node_type
logical :: found
type(Node), pointer :: temp_ptr
! Get pointer to the node
call get_node(ptr, node_name, temp_ptr, node_type, found)
! Leave if it was not found
if (.not. found) then
call fatal_error("Node " // node_name // " not part of Node " &
&// getNodeName(ptr) // ".")
end if
! Extract value
if (node_type == ATTR_NODE) then
call extractDataAttribute(ptr, node_name, result_long)
else
call extractDataContent(temp_ptr, result_long)
end if
end subroutine get_node_value_long
!===============================================================================
! GET_NODE_VALUE_DOUBLE returns a double precision real from attr. or node
!===============================================================================
subroutine get_node_value_double(ptr, node_name, result_double)
character(len=*), intent(in) :: node_name
real(8) :: result_double
type(Node), pointer, intent(in) :: ptr
integer :: node_type
logical :: found
type(Node), pointer :: temp_ptr
! Get pointer to the node
call get_node(ptr, node_name, temp_ptr, node_type, found)
! Leave if it was not found
if (.not. found) then
call fatal_error("Node " // node_name // " not part of Node " &
&// getNodeName(ptr) // ".")
end if
! Extract value
if (node_type == ATTR_NODE) then
call extractDataAttribute(ptr, node_name, result_double)
else
call extractDataContent(temp_ptr, result_double)
end if
end subroutine get_node_value_double
!===============================================================================
! GET_NODE_ARRAY_INTEGER returns a 1-D array of integers
!===============================================================================
subroutine get_node_array_integer(ptr, node_name, result_int)
character(len=*), intent(in) :: node_name
integer :: result_int(:)
type(Node), pointer, intent(in) :: ptr
integer :: node_type
logical :: found
type(Node), pointer :: temp_ptr
! Get pointer to the node
call get_node(ptr, node_name, temp_ptr, node_type, found)
! Leave if it was not found
if (.not. found) then
call fatal_error("Node " // node_name // " not part of Node " &
&// getNodeName(ptr) // ".")
end if
! Extract value
if (node_type == ATTR_NODE) then
call extractDataAttribute(ptr, node_name, result_int)
else
call extractDataContent(temp_ptr, result_int)
end if
end subroutine get_node_array_integer
!===============================================================================
! GET_NODE_ARRAY_DOUBLE returns a 1-D array of double precision reals
!===============================================================================
subroutine get_node_array_double(ptr, node_name, result_double)
character(len=*), intent(in) :: node_name
real(8) :: result_double(:)
type(Node), pointer, intent(in) :: ptr
integer :: node_type
logical :: found
type(Node), pointer :: temp_ptr
! Get pointer to the node
call get_node(ptr, node_name, temp_ptr, node_type, found)
! Leave if it was not found
if (.not. found) then
call fatal_error("Node " // node_name // " not part of Node " &
&// getNodeName(ptr) // ".")
end if
! Extract value
if (node_type == ATTR_NODE) then
call extractDataAttribute(ptr, node_name, result_double)
else
call extractDataContent(temp_ptr, result_double)
end if
end subroutine get_node_array_double
!===============================================================================
! GET_NODE_ARRAY_STRING returns a 1-D array of strings
!===============================================================================
subroutine get_node_array_string(ptr, node_name, result_string)
character(len=*), intent(in) :: node_name
character(len=*) :: result_string(:)
type(Node), pointer, intent(in) :: ptr
integer :: node_type
logical :: found
type(Node), pointer :: temp_ptr
! Get pointer to the node
call get_node(ptr, node_name, temp_ptr, node_type, found)
! Leave if it was not found
if (.not. found) then
call fatal_error("Node " // node_name // " not part of Node " &
&// getNodeName(ptr) // ".")
end if
! Extract value
if (node_type == ATTR_NODE) then
call extractDataAttribute(ptr, node_name, result_string)
else
call extractDataContent(temp_ptr, result_string)
end if
end subroutine get_node_array_string
!===============================================================================
! GET_NODE_VALUE_STRING returns a single string from attr. or node
!===============================================================================
subroutine get_node_value_string(ptr, node_name, result_str)
character(len=*), intent(in) :: node_name
character(len=*) :: result_str
type(Node), pointer, intent(in) :: ptr
integer :: node_type
logical :: found
type(Node), pointer :: temp_ptr
! Get pointer to the node
call get_node(ptr, node_name, temp_ptr, node_type, found)
! Leave if it was not found
if (.not. found) then
call fatal_error("Node " // node_name // " not part of Node " // &
getNodeName(ptr) // ".")
end if
! Extract value
if (node_type == ATTR_NODE) then
call extractDataAttribute(ptr, node_name, result_str)
else
call extractDataContent(temp_ptr, result_str)
end if
end subroutine get_node_value_string
!===============================================================================
! GET_NODE_ARRAYSIZE_INTEGER returns the size of the integer array
!===============================================================================
function get_arraysize_integer(ptr, node_name) result(n)
character(len=*), intent(in) :: node_name
integer :: n
type(Node), pointer, intent(in) :: ptr
integer :: node_type
logical :: found
type(Node), pointer :: temp_ptr
! Get pointer to the node
call get_node(ptr, node_name, temp_ptr, node_type, found)
! Leave if it was not found
if (.not. found) then
call fatal_error("Node " // node_name // " not part of Node " &
&// getNodeName(ptr) // ".")
end if
! Get the size
n = countrts(getTextContent(temp_ptr), 0)
end function get_arraysize_integer
!===============================================================================
! GET_NODE_ARRAYSIZE_DOUBLE returns the size of double prec. real array
!===============================================================================
function get_arraysize_double(ptr, node_name) result(n)
character(len=*), intent(in) :: node_name
integer :: n
type(Node), pointer, intent(in) :: ptr
integer :: node_type
logical :: found
type(Node), pointer :: temp_ptr
! Get pointer to the node
call get_node(ptr, node_name, temp_ptr, node_type, found)
! Leave if it was not found
if (.not. found) then
call fatal_error("Node " // node_name // " not part of Node " &
&// getNodeName(ptr) // ".")
end if
! Get the size
n = countrts(getTextContent(temp_ptr), 0.0_8)
end function get_arraysize_double
!===============================================================================
! GET_NODE_ARRAYSIZE_STRING returns the size of string array
!===============================================================================
function get_arraysize_string(ptr, node_name) result(n)
character(len=*), intent(in) :: node_name
integer :: n
type(Node), pointer, intent(in) :: ptr
integer :: node_type
logical :: found
type(Node), pointer :: temp_ptr
! Get pointer to the node
call get_node(ptr, node_name, temp_ptr, node_type, found)
! Leave if it was not found
if (.not. found) then
call fatal_error("Node " // node_name // " not part of Node " // &
getNodeName(ptr) // ".")
end if
! Get the size
n = countrts(getTextContent(temp_ptr), 'a')
end function get_arraysize_string
!===============================================================================
! GET_NODE private routine that gets a pointer to a specific node
!===============================================================================
subroutine get_node(in_ptr, node_name, out_ptr, node_type, found)
character(len=*), intent(in) :: node_name
integer, intent(out) :: node_type
logical, intent(out) :: found
type(Node), pointer, intent(in) :: in_ptr
type(Node), pointer, intent(out) :: out_ptr
type(NodeList), pointer :: elem_list
! Default that we found it and attribute
found = .true.
node_type = ATTR_NODE
! Get the attribute node
out_ptr => getAttributeNode(in_ptr, trim(node_name))
! Check if node exists
if (associated(out_ptr)) return
! Check for a sub-element
elem_list => getChildrenByTagName(in_ptr, trim(node_name))
! Get the length of the list
if (getLength(elem_list) == 0) then
found = .false.
return
end if
! Point to the new element
node_type = ELEM_NODE
out_ptr => item(elem_list, 0)
end subroutine get_node
end module xml_interface