|
| 1 | +module mod_vtkAPI |
| 2 | + implicit none |
| 3 | + private |
| 4 | + public :: vtk_structured_grid |
| 5 | + public :: create_vts_file |
| 6 | + public :: add_scalar |
| 7 | + public :: add_vector |
| 8 | + |
| 9 | + type :: vtk_data_array |
| 10 | + character(len=:), allocatable :: name |
| 11 | + character(len=:), allocatable :: type |
| 12 | + integer :: num_components |
| 13 | + real, allocatable :: data(:) |
| 14 | + end type vtk_data_array |
| 15 | + |
| 16 | + type :: vtk_structured_grid |
| 17 | + integer :: nx, ny, nz |
| 18 | + real, allocatable :: points(:, :) ! size(3, nx*ny*nz) |
| 19 | + type(vtk_data_array), allocatable :: scalars(:) |
| 20 | + type(vtk_data_array), allocatable :: vectors(:) |
| 21 | + end type vtk_structured_grid |
| 22 | + |
| 23 | +contains |
| 24 | + |
| 25 | + subroutine add_scalar(grid, name, data) |
| 26 | + type(vtk_structured_grid), intent(inout) :: grid |
| 27 | + character(len=*), intent(in) :: name |
| 28 | + real, intent(in) :: data(:) |
| 29 | + |
| 30 | + type(vtk_data_array), allocatable :: tmp(:) |
| 31 | + integer :: n |
| 32 | + |
| 33 | + if (.not. allocated(grid%scalars)) then |
| 34 | + allocate (grid%scalars(1)) |
| 35 | + n = 1 |
| 36 | + else |
| 37 | + n = size(grid%scalars) + 1 |
| 38 | + allocate (tmp(n)) |
| 39 | + tmp(1:n - 1) = grid%scalars |
| 40 | + call move_alloc(tmp, grid%scalars) |
| 41 | + end if |
| 42 | + |
| 43 | + grid%scalars(n)%name = name |
| 44 | + grid%scalars(n)%type = 'Float32' |
| 45 | + grid%scalars(n)%num_components = 1 |
| 46 | + grid%scalars(n)%data = data |
| 47 | + end subroutine add_scalar |
| 48 | + |
| 49 | + subroutine add_vector(grid, name, data) |
| 50 | + type(vtk_structured_grid), intent(inout) :: grid |
| 51 | + character(len=*), intent(in) :: name |
| 52 | + real, intent(in) :: data(:) |
| 53 | + |
| 54 | + type(vtk_data_array), allocatable :: tmp(:) |
| 55 | + integer :: n |
| 56 | + |
| 57 | + if (.not. allocated(grid%vectors)) then |
| 58 | + allocate (grid%vectors(1)) |
| 59 | + n = 1 |
| 60 | + else |
| 61 | + n = size(grid%vectors) + 1 |
| 62 | + allocate (tmp(n)) |
| 63 | + tmp(1:n - 1) = grid%vectors |
| 64 | + call move_alloc(tmp, grid%vectors) |
| 65 | + end if |
| 66 | + |
| 67 | + grid%vectors(n)%name = name |
| 68 | + grid%vectors(n)%type = 'Float32' |
| 69 | + grid%vectors(n)%num_components = 3 |
| 70 | + grid%vectors(n)%data = data |
| 71 | + end subroutine add_vector |
| 72 | + |
| 73 | + subroutine create_vts_file(grid, filename) |
| 74 | + type(vtk_structured_grid), intent(in) :: grid |
| 75 | + character(len=*), intent(in) :: filename |
| 76 | + integer :: iunit, i |
| 77 | + |
| 78 | + open (newunit=iunit, file=filename, status='replace', action='write', form='formatted') |
| 79 | + |
| 80 | + ! write header |
| 81 | + write (iunit, *) '<VTKFile type="StructuredGrid" version="1.0" byte_order="LittleEndian" header_type="UInt64">' |
| 82 | + write (iunit, *) ' <StructuredGrid WholeExtent="0 ', grid%nx - 1, ' 0 ', grid%ny - 1, ' 0 ', grid%nz - 1, '">' |
| 83 | + write (iunit, *) ' <Piece Extent="0 ', grid%nx - 1, ' 0 ', grid%ny - 1, ' 0 ', grid%nz - 1, '">' |
| 84 | + write (iunit, *) ' <PointData Scalars="Density" Vectors="Momentum">' |
| 85 | + |
| 86 | + ! write scalars |
| 87 | + do i = 1, size(grid%scalars) |
| 88 | + write (iunit, '(A)') ' <DataArray type="Float32" Name="'//trim(grid%scalars(i)%name)//'" format="ascii">' |
| 89 | + write (iunit, '(1000(F12.6,1X))') grid%scalars(i)%data |
| 90 | + write (iunit, '(A)') ' </DataArray>' |
| 91 | + end do |
| 92 | + |
| 93 | + ! write vectors |
| 94 | + do i = 1, size(grid%vectors) |
| 95 | + write(iunit,'(A)') ' <DataArray type="Float32" Name="'//trim(grid%vectors(i)%name)//'" NumberOfComponents="3" format="ascii">' |
| 96 | + write (iunit, '(1000(F12.6,1X))') grid%vectors(i)%data |
| 97 | + write (iunit, '(A)') ' </DataArray>' |
| 98 | + end do |
| 99 | + |
| 100 | + write (iunit, *) ' </PointData>' |
| 101 | + write (iunit, *) ' <CellData>' |
| 102 | + write (iunit, *) ' </CellData>' |
| 103 | + write (iunit, *) ' <Points>' |
| 104 | + write (iunit, '(A)') ' <DataArray type="Float32" Name="Points" NumberOfComponents="3" format="ascii">' |
| 105 | + write (iunit, '(1000(F12.6,1X))') grid%points |
| 106 | + write (iunit, '(A)') ' </DataArray>' |
| 107 | + write (iunit, *) ' </Points>' |
| 108 | + write (iunit, *) ' </Piece>' |
| 109 | + write (iunit, *) ' </StructuredGrid>' |
| 110 | + write (iunit, *) '</VTKFile>' |
| 111 | + |
| 112 | + close (iunit) |
| 113 | + end subroutine create_vts_file |
| 114 | + |
| 115 | +end module mod_vtkAPI |
0 commit comments