MODFLOW 6  version 6.7.0.dev0
USGS Modular Hydrologic Model
ModelExport.f90
Go to the documentation of this file.
1 !> @brief This module contains the ModelExportModule
2 !!
3 !! This modeule defines the local list of model exports.
4 !! It is not dependent on netcdf or other external
5 !! libraries.
6 !!
7 !<
9 
10  use kindmodule, only: dp, i4b, lgp
12  use simvariablesmodule, only: errmsg
15  use listmodule, only: listtype
18 
19  implicit none
20  private
21  public :: modelexports_create
23  public :: modelexports_post_step
24  public :: modelexports_destroy
25  public :: nc_export_active
26  public :: export_models
27  public :: get_export_model
28  public :: exportmodeltype
29 
31 
32  !> @brief export model type
33  !!
34  !! This is a container variable which holds
35  !! model export objects.
36  !!
37  !<
39  type(modeldynamicpkgstype), pointer :: loaders => null()
40  character(len=LENMODELNAME) :: modelname !< name of model
41  character(len=LENCOMPONENTNAME) :: modeltype !< type of model
42  character(len=LINELENGTH) :: modelfname !< name of model input file
43  character(len=LINELENGTH) :: nc_fname !< name of netcdf export file
44  class(ncbasemodelexporttype), pointer :: nc_export => null() !< netcdf export object pointer
45  integer(I4B) :: nctype !< type of netcdf export
46  integer(I4B) :: disenum !< type of discretization
47  integer(I4B) :: iout !< lst file descriptor
48  contains
49  procedure :: init
50  procedure :: post_prepare
51  procedure :: post_step
52  procedure :: destroy
53  end type exportmodeltype
54 
55 contains
56 
57  !> @brief is netcdf export configured for any model
58  !!
59  function nc_export_active() result(active)
61  logical(LGP) :: active
62  integer(I4B) :: n
63  type(exportmodeltype), pointer :: export_model
64  active = .false.
65  do n = 1, export_models%Count()
66  export_model => get_export_model(n)
67  if (export_model%nctype /= netcdf_undef) then
68  active = .true.
69  exit
70  end if
71  end do
72  end function nc_export_active
73 
74  !> @brief create export container variable for all local models
75  !!
76  subroutine modelexports_create(iout)
84  use sourcecommonmodule, only: file_ext
85  integer(I4B), intent(in) :: iout
86  type(modeldynamicpkgstype), pointer :: model_dynamic_input
87  type(exportmodeltype), pointer :: export_model
88  character(len=LENMEMPATH) :: modelnam_mempath, model_mempath, ext
89  integer(I4B), pointer :: disenum
90  integer(I4B) :: n
91  logical(LGP) :: found
92 
93  do n = 1, model_dynamic_pkgs%Count()
94  ! allocate and initialize
95  allocate (export_model)
96 
97  ! set pointer to dynamic input model instance
98  model_dynamic_input => getdynamicmodelfromlist(model_dynamic_pkgs, n)
99 
100  ! set input mempaths
101  modelnam_mempath = &
102  create_mem_path(component=model_dynamic_input%modelname, &
103  subcomponent='NAM', context=idm_context)
104  model_mempath = create_mem_path(component=model_dynamic_input%modelname, &
105  context=idm_context)
106  ! set pointer to dis enum type
107  call mem_setptr(disenum, 'DISENUM', model_mempath)
108 
109  ! initialize model
110  call export_model%init(model_dynamic_input, disenum, iout)
111 
112  ! update NetCDF fileout name if provided
113  call mem_set_value(export_model%nc_fname, 'NCMESH2DFILE', &
114  modelnam_mempath, found)
115  if (found) then
116  export_model%nctype = netcdf_mesh2d
117  else
118  call mem_set_value(export_model%nc_fname, 'NCSTRUCTFILE', &
119  modelnam_mempath, found)
120  if (found) then
121  export_model%nctype = netcdf_structured
122  end if
123  end if
124 
125  if (found) then
126  ext = file_ext(export_model%nc_fname)
127  if (ext /= 'nc') then
128  errmsg = 'NetCDF output file name must use ".nc" extension. '// &
129  'Filename="'//trim(export_model%nc_fname)//'".'
130  call store_error(errmsg)
131  call store_error_filename(export_model%modelfname)
132  end if
133  end if
134 
135  ! add model to list
136  call add_export_model(export_model)
137  end do
138  end subroutine modelexports_create
139 
140  !> @brief export model list post prepare step
141  !!
143  class(*), pointer :: obj
144  class(exportmodeltype), pointer :: export_model
145  integer(I4B) :: n
146  do n = 1, export_models%Count()
147  obj => export_models%GetItem(n)
148  if (associated(obj)) then
149  select type (obj)
150  class is (exportmodeltype)
151  export_model => obj
152  call export_model%post_prepare()
153  end select
154  end if
155  end do
156  end subroutine modelexports_post_prepare
157 
158  !> @brief export model list post step
159  !!
161  class(*), pointer :: obj
162  class(exportmodeltype), pointer :: export_model
163  integer(I4B) :: n
164  do n = 1, export_models%Count()
165  obj => export_models%GetItem(n)
166  if (associated(obj)) then
167  select type (obj)
168  class is (exportmodeltype)
169  export_model => obj
170  call export_model%post_step()
171  end select
172  end if
173  end do
174  end subroutine modelexports_post_step
175 
176  !> @brief destroy export model list
177  !!
178  subroutine modelexports_destroy()
179  class(*), pointer :: obj
180  class(exportmodeltype), pointer :: export_model
181  integer(I4B) :: n
182  do n = 1, export_models%Count()
183  obj => export_models%GetItem(n)
184  if (associated(obj)) then
185  select type (obj)
186  class is (exportmodeltype)
187  export_model => obj
188  call export_model%destroy()
189  deallocate (export_model)
190  nullify (export_model)
191  end select
192  end if
193  end do
194  !
195  call export_models%clear()
196  end subroutine modelexports_destroy
197 
198  !> @brief initialize model export container variable
199  !!
200  !<
201  subroutine init(this, loaders, disenum, iout)
203  class(exportmodeltype), intent(inout) :: this
204  type(modeldynamicpkgstype), pointer, intent(in) :: loaders
205  integer(I4B), intent(in) :: disenum
206  integer(I4B), intent(in) :: iout
207  this%loaders => loaders
208  this%modelname = loaders%modelname
209  this%modeltype = loaders%modeltype
210  this%modelfname = loaders%modelfname
211  this%nc_fname = ''
212  this%nctype = netcdf_undef
213  this%disenum = disenum
214  this%iout = iout
215  nullify (this%nc_export)
216  end subroutine init
217 
218  !> @brief model export container post prepare step actions
219  !!
220  !<
221  subroutine post_prepare(this)
222  class(exportmodeltype), intent(inout) :: this
223  if (associated(this%nc_export)) then
224  call this%nc_export%export_input()
225  end if
226  end subroutine post_prepare
227 
228  !> @brief model export container post step actions
229  !!
230  !<
231  subroutine post_step(this)
232  class(exportmodeltype), intent(inout) :: this
233  if (associated(this%nc_export)) then
234  call this%nc_export%step()
235  end if
236  end subroutine post_step
237 
238  !> @brief destroy model export container
239  !!
240  !<
241  subroutine destroy(this)
242  class(exportmodeltype), intent(inout) :: this
243  if (associated(this%nc_export)) then
244  call this%nc_export%destroy()
245  deallocate (this%nc_export)
246  nullify (this%nc_export)
247  end if
248  end subroutine destroy
249 
250  !> @brief add model export object to list
251  !!
252  !<
253  subroutine add_export_model(export_model)
254  type(exportmodeltype), pointer, intent(inout) :: export_model
255  class(*), pointer :: obj
256  obj => export_model
257  call export_models%Add(obj)
258  end subroutine add_export_model
259 
260  !> @brief get model export object by index
261  !!
262  !<
263  function get_export_model(idx) result(res)
264  integer(I4B), intent(in) :: idx !< package number
265  class(exportmodeltype), pointer :: res
266  class(*), pointer :: obj
267  ! initialize res
268  nullify (res)
269  ! get the object from the list
270  obj => export_models%GetItem(idx)
271  if (associated(obj)) then
272  select type (obj)
273  class is (exportmodeltype)
274  res => obj
275  end select
276  end if
277  end function get_export_model
278 
279 end module modelexportmodule
subroutine init()
Definition: GridSorting.f90:24
This module contains simulation constants.
Definition: Constants.f90:9
integer(i4b), parameter linelength
maximum length of a standard line
Definition: Constants.f90:45
integer(i4b), parameter lencomponentname
maximum length of a component name
Definition: Constants.f90:18
integer(i4b), parameter lenmodelname
maximum length of the model name
Definition: Constants.f90:22
integer(i4b), parameter lenmempath
maximum length of the memory path
Definition: Constants.f90:27
This module contains the InputLoadTypeModule.
class(modeldynamicpkgstype) function, pointer, public getdynamicmodelfromlist(list, idx)
get model dynamic packages object from list
type(listtype), public model_dynamic_pkgs
This module defines variable data types.
Definition: kind.f90:8
character(len=lenmempath) function create_mem_path(component, subcomponent, context)
returns the path to the memory object
This module contains the ModelExportModule.
Definition: ModelExport.f90:8
subroutine post_step(this)
model export container post step actions
subroutine, public modelexports_post_prepare()
export model list post prepare step
subroutine destroy(this)
destroy model export container
subroutine, public modelexports_destroy()
destroy export model list
subroutine, public modelexports_post_step()
export model list post step
type(listtype), public export_models
Definition: ModelExport.f90:30
subroutine post_prepare(this)
model export container post prepare step actions
class(exportmodeltype) function, pointer, public get_export_model(idx)
get model export object by index
subroutine add_export_model(export_model)
add model export object to list
subroutine, public modelexports_create(iout)
create export container variable for all local models
Definition: ModelExport.f90:77
logical(lgp) function, public nc_export_active()
is netcdf export configured for any model
Definition: ModelExport.f90:60
This module contains the NCModelExportModule.
Definition: NCModel.f90:8
@, public netcdf_structured
netcdf structrured export
Definition: NCModel.f90:33
@, public netcdf_mesh2d
netcdf ugrid layered mesh export
Definition: NCModel.f90:34
@, public netcdf_undef
undefined netcdf export type
Definition: NCModel.f90:32
This module contains simulation methods.
Definition: Sim.f90:10
subroutine, public store_error(msg, terminate)
Store an error message.
Definition: Sim.f90:92
subroutine, public store_error_filename(filename, terminate)
Store the erroring file name.
Definition: Sim.f90:203
This module contains simulation variables.
Definition: SimVariables.f90:9
character(len=maxcharlen) errmsg
error message string
character(len=linelength) idm_context
This module contains the SourceCommonModule.
Definition: SourceCommon.f90:7
character(len=lenpackagetype) function, public file_ext(filename)
input file extension
type for storing a dynamic package load list
A generic heterogeneous doubly-linked list.
Definition: List.f90:14
abstract type for model netcdf export type
Definition: NCModel.f90:101