Neko  0.9.0
A portable framework for high-order spectral element flow simulations
generic_file.f90
Go to the documentation of this file.
1 ! Copyright (c) 2019-2023, The Neko Authors
2 ! All rights reserved.
3 !
4 ! Redistribution and use in source and binary forms, with or without
5 ! modification, are permitted provided that the following conditions
6 ! are met:
7 !
8 ! * Redistributions of source code must retain the above copyright
9 ! notice, this list of conditions and the following disclaimer.
10 !
11 ! * Redistributions in binary form must reproduce the above
12 ! copyright notice, this list of conditions and the following
13 ! disclaimer in the documentation and/or other materials provided
14 ! with the distribution.
15 !
16 ! * Neither the name of the authors nor the names of its
17 ! contributors may be used to endorse or promote products derived
18 ! from this software without specific prior written permission.
19 !
20 ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 ! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 ! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 ! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 ! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 ! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 ! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 ! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 ! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 ! POSSIBILITY OF SUCH DAMAGE.
32 !
34  use num_types
35  implicit none
36 
38  type, abstract :: generic_file_t
39  character(len=1024) :: fname
40  integer :: counter
41  integer :: start_counter = 0
43  logical :: serial = .false.
44  contains
46  procedure :: init => generic_file_init
47 
48  procedure(generic_file_write), deferred :: write
49 
50  procedure(generic_file_read), deferred :: read
52  procedure :: set_counter => generic_file_set_counter
54  procedure :: set_start_counter => generic_file_set_start_counter
56  procedure :: check_exists => generic_file_check_exists
57  end type generic_file_t
58 
59  abstract interface
60  subroutine generic_file_write(this, data, t)
61  import :: generic_file_t
62  import :: rp
63  class(generic_file_t), intent(inout) :: this
64  class(*), target, intent(in) :: data
65  real(kind=rp), intent(in), optional :: t
66  end subroutine generic_file_write
67  end interface
68 
69  abstract interface
70  subroutine generic_file_read(this, data)
71  import :: generic_file_t
72  class(generic_file_t) :: this
73  class(*), target, intent(inout) :: data
74  end subroutine generic_file_read
75  end interface
76 
77 contains
78 
81  subroutine generic_file_init(this, fname)
82  class(generic_file_t) :: this
83  character(len=*) :: fname
84 
85  this%fname = fname
86  this%counter = 0
87 
88  end subroutine generic_file_init
89 
91  subroutine generic_file_set_counter(this, n)
92  class(generic_file_t), intent(inout) :: this
93  integer, intent(in) :: n
94  this%counter = n
95  end subroutine generic_file_set_counter
96 
98  subroutine generic_file_set_start_counter(this, n)
99  class(generic_file_t), intent(inout) :: this
100  integer, intent(in) :: n
101  this%start_counter = n
102  end subroutine generic_file_set_start_counter
103 
105  subroutine generic_file_check_exists(this)
106  use utils, only: neko_error
107  use comm, only: pe_rank, neko_comm
108  use mpi_f08
109  implicit none
110 
111  class(generic_file_t), intent(in) :: this
112  logical :: file_exists
113  integer :: neko_mpi_ierr
114 
115  file_exists = .false.
116 
117  if (pe_rank .eq. 0 .or. this%serial) then
118  ! Stop if the file does not exist
119  inquire(file=this%fname, exist=file_exists)
120  end if
121  if (.not. this%serial) then
122  call mpi_bcast(file_exists, 1, mpi_logical, 0, neko_comm, neko_mpi_ierr)
123  end if
124 
125  if (.not. file_exists) then
126  call neko_error('File does not exist: '//trim(this%fname))
127  end if
128 
129  end subroutine generic_file_check_exists
130 
131 
132 end module generic_file
Definition: comm.F90:1
integer pe_rank
MPI rank.
Definition: comm.F90:28
type(mpi_comm) neko_comm
MPI communicator.
Definition: comm.F90:16
Module for file I/O operations.
Definition: file.f90:34
subroutine generic_file_check_exists(this)
check if the file exists
subroutine generic_file_set_start_counter(this, n)
Set the file start counter to n.
subroutine generic_file_init(this, fname)
Generic file constructor.
subroutine generic_file_set_counter(this, n)
Set the file counter to n.
integer, parameter, public rp
Global precision used in computations.
Definition: num_types.f90:12
Utilities.
Definition: utils.f90:35
A generic file handler.