Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/elmerice/IceSheet/Tools/ConservativeInterpolation/READ_CELL_NETCDF.F90
3206 views
1
!/*****************************************************************************/
2
! *
3
! * Elmer/Ice, a glaciological add-on to Elmer
4
! * http://elmerice.elmerfem.org
5
! *
6
! *
7
! * This program is free software; you can redistribute it and/or
8
! * modify it under the terms of the GNU General Public License
9
! * as published by the Free Software Foundation; either version 2
10
! * of the License, or (at your option) any later version.
11
! *
12
! * This program is distributed in the hope that it will be useful,
13
! * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
! * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
! * GNU General Public License for more details.
16
! *
17
! * You should have received a copy of the GNU General Public License
18
! * along with this program (in file fem/GPL-2); if not, write to the
19
! * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20
! * Boston, MA 02110-1301, USA.
21
! *
22
! *****************************************************************************/
23
! ******************************************************************************
24
! *
25
! * Authors: F. Gillet-Chaulet (IGE-France)
26
! * Web: http://elmerice.elmerfem.org
27
! * Original Date: 04/2019
28
! *
29
! *****************************************************************************
30
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
31
! Read a cell variable stored in a netcdf file
32
! The netcdf should contains all the cell of the serial mesh
33
! IF used in parallel, the parallel mesh should have the same global
34
! cell ordering as the serial mesh
35
!
36
! IF netcdf contains a time dimension, the current simulation time is
37
! used as time index : if t = ]0._dp,1._dp] => index=1 etc...
38
!
39
! Required input parameters:
40
! File Name = File <netcdf file>
41
! VarName = File <name of the netcdf variable>
42
! Target Variable Name = String OPTIONAL <name of the Elmer variable>
43
!
44
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
45
SUBROUTINE READ_CELL_NETCDF( Model,Solver,dt,TransientSimulation )
46
!------------------------------------------------------------------------------
47
USE DefUtils
48
USE NETCDF
49
IMPLICIT NONE
50
!------------------------------------------------------------------------------
51
TYPE(Solver_t), TARGET :: Solver
52
TYPE(Model_t) :: Model
53
REAL(KIND=dp) :: dt
54
LOGICAL :: TransientSimulation
55
!------------------------------------------------------------------------------
56
! Local variables
57
TYPE(ValueList_t), POINTER :: SolverParams
58
TYPE(Variable_t),POINTER :: Var
59
TYPE(Element_t), POINTER :: Element
60
INTEGER :: i
61
INTEGER :: NElements
62
CHARACTER (len=100) :: FName
63
CHARACTER (len=100) :: VarName,TVarName
64
INTEGER :: varid,ncells,ncid,ndims,ntime,TVarID
65
INTEGER :: NetCDFstatus
66
REAL(KIND=dp), ALLOCATABLE :: Values(:)
67
REAL(KIND=dp) :: time
68
INTEGER :: TimeIndex
69
INTEGER,SAVE :: SavedTime=-1
70
INTEGER :: EIndex
71
CHARACTER(LEN=MAX_NAME_LEN) :: SolverName="READ_NETCDF_CELL"
72
LOGICAL :: Parallel,Found
73
74
! get parameters
75
SolverParams => GetSolverParams()
76
FName = ListGetString(SolverParams,'File Name',UnFoundFatal=.TRUE.)
77
78
write(Message,'(a,a)') 'File name: ',Trim(FName)
79
CALL INFO(SolverName,Message,Level=4)
80
81
VarName = ListGetString(SolverParams,'Variable Name',UnFoundFatal=.TRUE.)
82
TVarName = ListGetString(SolverParams,'Target Variable Name',Found)
83
IF (.NOT.Found) TVarName=VarName
84
85
! check if this is a parallel run
86
Parallel=(ParEnv % PEs > 1)
87
88
! get variable
89
Var => VariableGet( Model % Mesh % Variables,TRIM(TVarName),UnFoundFatal=.TRUE.)
90
IF(Var % TYPE /= Variable_on_elements) &
91
CALL FATAL(SolverName,'Wrong variable type; use -elem ')
92
93
NetCDFstatus = NF90_OPEN(trim(FName),NF90_NOWRITE,ncid)
94
IF ( NetCDFstatus /= NF90_NOERR ) &
95
CALL FATAL(SolverName,"file open failed")
96
97
NetCDFstatus = nf90_inq_dimid(ncid, 'ncells' , varid)
98
IF ( NetCDFstatus /= NF90_NOERR ) &
99
CALL FATAL(SolverName,"unable to get ncells dim")
100
101
NetCDFstatus = nf90_inquire_dimension(ncid,varid,len=ncells)
102
IF ( NetCDFstatus /= NF90_NOERR ) &
103
CALL FATAL(SolverName,"unable to get ncells")
104
105
allocate(Values(ncells))
106
107
! get variable ID
108
NetCDFstatus = nf90_inq_varid(ncid,trim(VarName),TVarId)
109
IF ( NetCDFstatus /= NF90_NOERR ) &
110
CALL FATAL(SolverName,"unable to get varid")
111
112
! variable dimensions
113
NetCDFstatus = nf90_inquire_variable(ncid, TVarId, ndims=ndims)
114
IF ( NetCDFstatus /= NF90_NOERR ) &
115
CALL FATAL(SolverName,"unable to get variable dimensions")
116
117
! if ndim > 1 we should have a time dimension
118
IF (ndims.GT.1) THEN
119
NetCDFstatus = nf90_inq_dimid(ncid, 'time' , varid)
120
IF ( NetCDFstatus /= NF90_NOERR ) &
121
CALL FATAL(SolverName,"unable to get time dimension")
122
123
NetCDFstatus = nf90_inquire_dimension(ncid,varid,len=ntime)
124
IF ( NetCDFstatus /= NF90_NOERR ) &
125
CALL FATAL(SolverName,"unable to get ntime")
126
127
! get time index
128
time = GetTime()
129
dt = GetTimeStepSize()
130
TimeIndex = floor(time-dt/2) + 1
131
TimeIndex = max(1,min(TimeIndex,ntime))
132
133
! we have already done this; return
134
IF (SavedTime.EQ.TimeIndex) THEN
135
NetCDFstatus=nf90_close(ncid)
136
CALL INFO(SolverName,"Nothing to do; return",level=4)
137
RETURN
138
ENDIF
139
SavedTime=TimeIndex
140
141
WRITE(Message,'(a,i0)') 'reading time step: ',TimeIndex
142
CALL INFO(SolverName,Message,level=4)
143
144
NetCDFstatus=nf90_get_var(ncid,TVarId,Values,start=(/1,TimeIndex/),count=(/ncells,1/))
145
IF ( NetCDFstatus /= NF90_NOERR ) &
146
CALL FATAL(SolverName,"unable to get variable")
147
148
ELSE
149
! we have already done this; return
150
IF (SavedTime.GT.0) THEN
151
NetCDFstatus=nf90_close(ncid)
152
CALL INFO(SolverName,"Nothing to do; return",level=4)
153
RETURN
154
ENDIF
155
SavedTime=+1
156
157
NetCDFstatus=nf90_get_var(ncid,TVarId,Values,start=(/1/),count=(/ncells/))
158
IF ( NetCDFstatus /= NF90_NOERR ) &
159
CALL FATAL(SolverName,"unable to get variable")
160
161
END IF
162
163
! close file
164
NetCDFstatus=nf90_close(ncid)
165
166
NElements = GetNOFActive()
167
DO i=1,NElements
168
Element => GetActiveElement(i)
169
IF (Parallel) THEN
170
EIndex=Element % GElementIndex
171
ELSE
172
EIndex=Element % ElementIndex
173
ENDIF
174
Var % Values(Var % Perm(Element % ElementIndex))=Values(EIndex)
175
END DO
176
177
DEALLOCATE(Values)
178
179
180
END SUBROUTINE READ_CELL_NETCDF
181
182