Afivo 0.3
Loading...
Searching...
No Matches
m_write_silo.f90
1!> This module contains wrapper functions to simplify writing Silo files
2!> \todo Document the functions in this module
4
5 implicit none
6 private
7
8 include 'silo_f9x.inc'
9
10 integer, parameter :: dp = kind(0.0d0)
11 integer, parameter :: line_len = 200
12 integer, parameter :: db_type = db_pdb
13
14 public :: silo_mkdir
15 public :: silo_create_file
16 public :: silo_open_file
17 public :: silo_close_file
18 public :: silo_set_time_varying
19 public :: silo_add_curve
20 public :: silo_add_grid
21 public :: silo_add_var
22 public :: silo_set_mmesh_grid
23 public :: silo_set_mmesh_var
24
25contains
26
27 subroutine silo_create_file(filename, dbix)
28 character(len=*), intent(in) :: filename
29 integer, intent(out) :: dbix
30 integer :: ierr
31 character(len=line_len) :: fileinfo
32
33 fileinfo = "A silo file"
34 ierr = dbcreate(trim(filename), len_trim(filename), db_clobber, &
35 db_local, fileinfo, len_trim(fileinfo), db_type, dbix)
36 if (ierr /= 0) then
37 print *, "Error creating file ", trim(filename)
38 error stop
39 end if
40 end subroutine silo_create_file
41
42 subroutine silo_open_file(filename, dbix)
43 character(len=*), intent(in) :: filename
44 integer :: dbix, ierr
45
46 ierr = dbopen(trim(filename), len_trim(filename), db_type, &
47 db_append, dbix)
48 if (ierr /= 0) then
49 print *, "Error opening file ", trim(filename)
50 error stop
51 end if
52 end subroutine silo_open_file
53
54 subroutine silo_close_file(dbix)
55 integer, intent(in) :: dbix
56 integer :: ierr
57
58 ierr = dbclose(dbix)
59 if (ierr /= 0) then
60 print *, "Error closing file with index", dbix
61 error stop
62 end if
63 end subroutine silo_close_file
64
65 subroutine silo_mkdir(dbix, dirname)
66 character(len=*), intent(in) :: dirname
67 integer, intent(in) :: dbix
68 integer :: ierr, iostat
69
70 ierr = dbmkdir(dbix, trim(dirname), len_trim(dirname), iostat)
71 if (ierr /= 0) then
72 print *, "Error creating directory ", dirname
73 error stop
74 end if
75 end subroutine silo_mkdir
76
77 !> Write two entries to the Silo file so that Visit treats it as a
78 !> time-varying database
79 subroutine silo_set_time_varying(dbix)
80 integer, intent(in) :: dbix
81 integer :: ierr
82 integer, parameter :: int_bool(1) = 1
83 integer, parameter :: dims(1) = 1
84 character(len=*), parameter :: name1 = "/ConnectivityIsTimeVarying"
85 character(len=*), parameter :: name2 = "/MetadataIsTimeVarying"
86
87 interface
88 integer function dbwrite(dbid, varname, lvarname, var, dims, &
89 ndims, datatype)
90 use, intrinsic :: iso_c_binding
91 integer(c_int) :: dbid, var(*), lvarname, dims(*), ndims, datatype
92 character(kind=c_char) :: varname(*)
93 end function dbwrite
94 end interface
95
96 ierr = dbwrite(dbix, name1, len(name1), int_bool, dims, 1, db_int);
97 if (ierr /= 0) print *, "Error writing ", trim(name1)
98 ierr = dbwrite(dbix, name2, len(name2), int_bool, dims, 1, db_int);
99 if (ierr /= 0) print *, "Error writing ", trim(name2)
100 end subroutine silo_set_time_varying
101
102 !> Add a curve-object (pairs of x-y values) to the Silo file
103 subroutine silo_add_curve(dbix, curvename, xvals, yvals)
104 character(len=*), intent(in) :: curvename
105 integer, intent(in) :: dbix
106 real(dp), intent(in) :: xvals(:), yvals(:)
107 integer :: iostat, ierr, dboptix
108
109 interface
110 integer (c_int) function dbputcurve(dbid, curvename, lcurvename, &
111 xvals, yvals, datatype, npoints, optlist_id, status)
112 use, intrinsic :: iso_c_binding
113 integer(c_int) :: dbid, lcurvename, datatype, npoints, status, optlist_id
114 real(c_double) :: xvals(*), yvals(*)
115 character(kind=c_char) :: curvename(*)
116 end function dbputcurve
117
118 integer (c_int) function dbaddiopt(optlist_id, option, ivalue)
119 use, intrinsic :: iso_c_binding
120 integer(c_int), intent(in) :: optlist_id, option, ivalue(*)
121 end function dbaddiopt
122 end interface
123
124 if (size(shape(xvals)) /= 1 .or. size(shape(yvals)) /= 1) then
125 print *, "Input is not a one-dimensional array. "
126 end if
127
128 if (size(xvals) /= size(yvals)) then
129 print *, "x and y arrays not of the same dimensions"
130 print *, size(xvals), " and ", size(yvals)
131 end if
132
133 ierr = dbmkoptlist(20, dboptix)
134 if (ierr /= 0) print *, &
135 "Error creating options list in SILO_add_curve ", dboptix
136
137 ierr = dbputcurve(dbix, trim(curvename), len_trim(curvename), &
138 xvals, yvals, db_double, size(xvals), dboptix, iostat)
139
140 if (ierr /= 0) print *, &
141 "ERROR: curve object not added to SILO file"
142
143 ierr = dbfreeoptlist(dboptix)
144 if (ierr /= 0) print *, &
145 "Error dbfreeoptlist in SILO_add_curve", ierr
146 end subroutine silo_add_curve
147
148 subroutine silo_add_grid(dbix, gridname, n_dim, N_r, r_min, dr, &
149 lo_offset, hi_offset, n_cycle)
150 character(len=*), intent(in) :: gridname
151 integer, intent(in) :: dbix, n_dim, n_r(:)
152 integer, intent(in) :: lo_offset(n_dim), hi_offset(n_dim)
153 real(dp), intent(in) :: r_min(:), dr(:)
154 real(dp), allocatable :: x_coords(:), y_coords(:), z_coords(:)
155 integer :: i, ierr, iostat, dboptix
156 integer, optional, intent(in) :: n_cycle
157
158 interface
159 function dbputqm(dbid, name, lname, xname, lxname, yname, &
160 lyname, zname, lzname, x, y, z, dims, ndims, &
161 datatype, coordtype, optlist_id, status)
162 use, intrinsic :: iso_c_binding
163 integer(c_int) :: dbid, lname, lxname, lyname, lzname, dims(*), ndims
164 integer(c_int) :: datatype, coordtype, optlist_id, status, dbputqm
165 real(c_double) :: x(*), y(*), z(*)
166 character(kind=c_char) :: name(*), xname(*), yname(*), zname(*)
167 end function dbputqm
168
169 integer (c_int) function dbaddiopt(optlist_id, option, ivalue)
170 use, intrinsic :: iso_c_binding
171 integer(c_int), intent(in) :: optlist_id, option, ivalue(*)
172 end function dbaddiopt
173 end interface
174
175 if (n_dim < 1 .or. n_dim > 3) then
176 print *, "Cannot add grid for which n_dim < 1 or n_dim > 3"
177 return
178 end if
179
180 allocate(x_coords(n_r(1)))
181 do i = 1, n_r(1)
182 x_coords(i) = r_min(1) + (i-1) * dr(1)
183 end do
184
185 if (n_dim > 1) then
186 allocate(y_coords(n_r(2)))
187 do i = 1, n_r(2)
188 y_coords(i) = r_min(2) + (i-1) * dr(2)
189 end do
190 else
191 allocate(y_coords(0))
192 end if
193
194 if (n_dim > 2) then
195 allocate(z_coords(n_r(3)))
196 do i = 1, n_r(3)
197 z_coords(i) = r_min(3) + (i-1) * dr(3)
198 end do
199 else
200 allocate(z_coords(0))
201 end if
202
203 ! Make option list
204 ierr = dbmkoptlist(20, dboptix)
205 if (ierr /= 0) print *, &
206 "Error creating options list in SILO_add_grid ", dboptix
207
208 ! Make iteration number explicit
209 ierr = dbaddiopt(dboptix, dbopt_cycle, [n_cycle])
210 if (ierr /= 0) print *, &
211 "Error passing iteration number SILO_add_grid ", dboptix
212
213 ! Set integer options
214 ierr = dbaddiopt(dboptix, dbopt_nspace, [n_dim])
215 if (ierr /= 0) print *, &
216 "Error dbaddiopt in SILO_add_grid: DBOPT_NSPACE", ierr
217
218 ierr = dbaddiopt(dboptix, dbopt_lo_offset, lo_offset)
219 if (ierr /= 0) print *, &
220 "Error dbaddiopt in SILO_add_grid: DBOPT_LO_OFFSET", ierr
221
222 ierr = dbaddiopt(dboptix, dbopt_hi_offset, hi_offset)
223 if (ierr /= 0) print *, &
224 "Error dbaddiopt in SILO_add_grid: DBOPT_HI_OFFSET", ierr
225
226 ! Write the grid structure
227 ierr = dbputqm(dbix, trim(gridname), len_trim(gridname), &
228 'x', 1, 'y', 1, 'z', 1, x_coords, y_coords, z_coords, &
229 n_r, n_dim, db_double, db_collinear, dboptix, iostat)
230 if (ierr /= 0) print *, &
231 "Error dbputqm is SILO_add_grid", ierr
232
233 ierr = dbfreeoptlist(dboptix)
234 if (ierr /= 0) print *, &
235 "Error dbfreeoptlist is SILO_add_grid", ierr
236 end subroutine silo_add_grid
237
238 subroutine silo_add_var(dbix, dataname, gridname, d_packed, d_shape, n_cycle)
239 character(len=*), intent(in) :: gridname, dataname
240 real(dp), intent(in) :: d_packed(*)
241 integer, intent(in) :: dbix, d_shape(:)
242
243 integer :: dboptix, ierr, iostat
244 real(dp) :: dummy(1)
245 integer, optional, intent(in) :: n_cycle
246
247 interface
248 function dbputqv1(dbid, name, lname, meshname, lmeshname, &
249 var, dims, ndims, mixvar, mixlen, datatype, &
250 centering, optlist_id, status)
251 use, intrinsic :: iso_c_binding
252 integer(c_int) :: dbid, lname, lmeshname, dims(*), ndims, mixlen
253 integer(c_int) :: centering, optlist_id, status, datatype, dbputqv1
254 real(c_double) :: var(*), mixvar(*)
255 character(kind=c_char) :: name(*), meshname(*)
256 end function dbputqv1
257
258 integer (c_int) function dbaddiopt(optlist_id, option, ivalue)
259 use, intrinsic :: iso_c_binding
260 integer(c_int), intent(in) :: optlist_id, option, ivalue(*)
261 end function dbaddiopt
262 end interface
263
264 if (size(d_shape) < 1 .or. size(d_shape) > 3) then
265 error stop "Error: size(d_shape) < 1 or size(d_shape) > 3"
266 end if
267
268 ierr = dbmkoptlist(10, dboptix)
269 if (ierr /= 0) then
270 error stop "Error creating options list in SILO_add_var"
271 end if
272
273 ! Make iteration number explicit
274 if (present(n_cycle)) then
275 ierr = dbaddiopt(dboptix, dbopt_cycle, [n_cycle])
276 if (ierr /= 0) print *, &
277 "Error passing iteration number SILO_add_var ", dboptix
278 end if
279
280 ierr = dbaddiopt(dboptix, dbopt_hide_from_gui, [1])
281
282 ! Write the data to the grid
283 ierr = dbputqv1(dbix, trim(dataname), len_trim(dataname), &
284 trim(gridname), len_trim(gridname), d_packed, d_shape, &
285 size(d_shape), dummy, 0, db_double, db_zonecent, dboptix, iostat)
286 if (ierr /= 0) then
287 print *, "Error dbputqv1 in SILO_add_var", ierr
288 error stop
289 end if
290
291 ierr = dbfreeoptlist(dboptix)
292 end subroutine silo_add_var
293
294 subroutine silo_set_mmesh_grid(dbix, mmname, gridnames, n_cycle, time)
295 character(len=*), intent(in) :: mmname, gridnames(:)
296 integer, intent(in) :: dbix
297 integer, intent(in), optional :: n_cycle
298 real(dp), intent(in), optional :: time
299
300 integer :: i, ierr,length
301 integer :: dboptix, iostat, old_str_len
302 integer :: n_grids, name_len, total_len
303 integer, allocatable :: m_types(:), name_lengths(:)
304 character(len=:), allocatable :: mnames
305
306 interface
307 function dbputmmesh(dbid, name, lname, nmesh, meshnames, lmeshnames, &
308 meshtypes, optlist_id, status)
309 use, intrinsic :: iso_c_binding
310 integer(c_int) :: dbputmmesh, lmeshnames(*)
311 integer(c_int) :: dbid, lname, nmesh, meshtypes(*), optlist_id, status
312 character(kind=c_char) :: name(*), meshnames(*)
313 end function dbputmmesh
314 end interface
315
316 n_grids = size(gridnames)
317 if (n_grids < 1) then
318 error stop "SILO_set_mmesh_grid: error too few grids (<1)"
319 end if
320
321 name_len = len(gridnames(1))
322 total_len = name_len * n_grids
323 allocate(character(total_len) :: mnames)
324 allocate(name_lengths(n_grids))
325 allocate(m_types(n_grids))
326
327 do i = 1, n_grids
328 mnames((i-1)*name_len+1:i*name_len) = trim(gridnames(i)) // char(0)
329 end do
330
331 old_str_len = dbset2dstrlen(name_len)
332 m_types = db_quad_rect
333 name_lengths = name_len
334
335 ierr = dbmkoptlist(10, dboptix)
336
337 if (present(n_cycle)) then
338 ierr = dbaddiopt(dboptix, dbopt_cycle, n_cycle)
339 end if
340
341 if (present(time)) then
342 ierr = dbadddopt(dboptix, dbopt_dtime, time)
343 end if
344
345 ierr = dbputmmesh(dbix, trim(mmname), len_trim(mmname), n_grids, &
346 mnames(1:total_len), name_lengths, m_types, dboptix, iostat)
347 if (ierr /= 0) then
348 error stop "Error calling dbputmmesh"
349 end if
350
351 ierr = dbfreeoptlist(dboptix)
352 length = dbset2dstrlen(old_str_len)
353 end subroutine silo_set_mmesh_grid
354
355 subroutine silo_set_mmesh_var(dbix, mvname, mmname, datanames, n_cycle, time)
356 character(len=*), intent(in) :: mvname, mmname, datanames(:)
357 integer, intent(in) :: dbix
358 integer, intent(in), optional :: n_cycle
359 real(dp), intent(in), optional :: time
360
361
362 integer :: i, ierr, dboptix, iostat,length
363 integer :: old_str_len, n_grids, name_len, total_len
364 integer, allocatable :: m_types(:), name_lengths(:)
365 character(:), allocatable :: dnames
366
367 interface
368 function dbputmvar(dbid, name, lname, nlevels, meshnames, &
369 lmnames, meshtypes, optlist_id, status)
370 use, intrinsic :: iso_c_binding
371 integer(c_int) :: dbputmvar, lmnames(*)
372 integer(c_int) :: dbid, lname, nlevels, meshtypes(*)
373 integer(c_int) :: optlist_id, status
374 character(kind=c_char) :: name(*), meshnames(*)
375 end function dbputmvar
376 end interface
377
378 n_grids = size(datanames)
379 if (n_grids < 1) then
380 error stop "SILO_set_mmesh_var: error too few grids (<1)"
381 end if
382
383 name_len = len(datanames(1))
384 total_len = name_len * n_grids
385 allocate(character(total_len) :: dnames)
386 allocate(name_lengths(n_grids))
387 allocate(m_types(n_grids))
388
389 do i = 1, n_grids
390 dnames((i-1)*name_len+1:i*name_len) = trim(datanames(i)) // char(0)
391 end do
392 old_str_len = dbset2dstrlen(name_len)
393 m_types = db_quadvar
394 name_lengths = name_len
395
396 ierr = dbmkoptlist(10, dboptix)
397 if (ierr /= 0) then
398 error stop "Error creating options list in SILO_set_mmesh_var"
399 end if
400
401 if (present(n_cycle)) then
402 ierr = dbaddiopt(dboptix, dbopt_cycle, n_cycle)
403 end if
404
405 if (present(time)) then
406 ierr = dbadddopt(dboptix, dbopt_dtime, time)
407 end if
408
409 ! Jannis: valgrind complains about a memory leak due to this call, but if
410 ! that leak is there, it seems to be Silo's fault.
411 ierr = dbaddcopt(dboptix, dbopt_mmesh_name, &
412 trim(mmname), len_trim(mmname))
413 if (ierr /= 0) print *, &
414 "Error dbaddiopt in SILO_set_mmesh_var: DBOPT_MMESH_NAME", ierr
415
416 ierr = dbputmvar(dbix, trim(mvname), len_trim(mvname), n_grids, &
417 dnames(1:total_len), name_lengths, m_types, dboptix, iostat)
418 if (ierr /= 0) then
419 error stop "Error calling dbputmvar"
420 end if
421
422 ierr = dbfreeoptlist(dboptix)
423 length = dbset2dstrlen(old_str_len)
424 end subroutine silo_set_mmesh_var
425
426 ! !> Add a curve-object (pairs of x-y values) to the Silo file
427 ! subroutine SILO_add_curve(dbix, curvename, xvals, yvals, xname, yname)
428 ! character(len=*), intent(in) :: curvename
429 ! integer, intent(in) :: dbix
430 ! real(dp), intent(in) :: xvals(:), yvals(:)
431 ! character(len=*), intent(in) :: xname, yname
432 ! integer :: iostat, ierr, dboptix
433 !
434 ! interface
435 ! integer (c_int) function dbputcurve(dbid, curvename, lcurvename, &
436 ! xvals, yvals, datatype, npoints, optlist_id, status)
437 ! use, intrinsic :: iso_c_binding
438 ! integer(c_int) :: dbid, lcurvename, datatype, npoints, status, optlist_id
439 ! real(c_double) :: xvals(*), yvals(*)
440 ! character(kind=c_char) :: curvename(*)
441 ! end function dbputcurve
442 !
443 ! integer (c_int) function dbaddiopt(optlist_id, option, ivalue)
444 ! use, intrinsic :: iso_c_binding
445 ! integer(c_int), intent(in) :: optlist_id, option, ivalue(*)
446 ! end function dbaddiopt
447 ! end interface
448 !
449 ! if (size(xvals) /= size(yvals)) &
450 ! error stop "SILO_add_curve: x and y arrays unequal size"
451 !
452 ! ierr = dbmkoptlist(20, dboptix)
453 ! if (ierr /= 0) error stop "creating options list in SILO_add_curve "
454 !
455 ! ierr = dbaddcopt(dboptix, DBOPT_XLABEL, trim(xname), len_trim(xname))
456 ! if (ierr /= 0) error stop "adding option in SILO_add_curve"
457 ! ierr = dbaddcopt(dboptix, DBOPT_YLABEL, trim(yname), len_trim(yname))
458 ! if (ierr /= 0) error stop "adding option in SILO_add_curve"
459 !
460 ! ierr = dbputcurve(dbix, trim(curvename), len_trim(curvename), &
461 ! xvals, yvals, DB_DOUBLE, size(xvals), dboptix, iostat)
462 ! if (ierr /= 0) error stop "curve object not added to SILO file"
463 !
464 ! ierr = dbfreeoptlist(dboptix)
465 ! if (ierr /= 0) error stop "dbfreeoptlist in SILO_add_curve"
466 ! end subroutine SILO_add_curve
467
468end module m_write_silo
This module contains wrapper functions to simplify writing Silo files.