forked from precice/elmer-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coupler_Solver.F90
420 lines (333 loc) · 18.6 KB
/
Coupler_Solver.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
MODULE HelperMethods
!------------------------------------------------------------------------------
USE DefUtils
!------------------------------------------------------------------------------
IMPLICIT NONE
CONTAINS
! SUBROUTINE StoreCheckpoint(writeData,t)
! END SUBROUTINE StoreCheckpoint
SUBROUTINE Print(dataName,mesh,BoundaryPerm,CoordVals)
!-------------------------Strings----------------------------------------------
CHARACTER(LEN=MAX_NAME_LEN) :: dataName
CHARACTER(LEN=MAX_NAME_LEN) :: infoMessage
!-------------------------Elmer_Types----------------------------------------------
TYPE(Variable_t), POINTER :: dataVariable
TYPE(Mesh_t), POINTER :: mesh
!------------------------Data Arrays----------------------------------------------
REAL(KIND=dp), POINTER :: CoordVals(:)
INTEGER, POINTER :: BoundaryPerm(:)
!--------------------------Iterators-------------------------------------
INTEGER :: i,j
!--------------------------Mesh-------------------------------------
INTEGER :: meshDim
dataVariable => VariableGet( mesh % Variables, dataName)
meshDim = mesh % MaxDim
CALL Info('CouplerSolver','Printing ' //TRIM(dataName))
DO i = 1, mesh % NumberOfNodes
j = BoundaryPerm(i)
IF(j == 0) CYCLE
IF (meshDim == 2) THEN
write(infoMessage,'(A,I5,A,I5,A,F10.4,A,F10.2,A,F10.2)') 'Node: ',i,' Index: ',j,' Value: ' &
,dataVariable % Values(dataVariable % Perm(i)),&
' X= ', CoordVals(meshDim * j-1), ' Y= ', CoordVals(meshDim * j)
ELSE IF (meshDim == 3) THEN
write(infoMessage,'(A,I5,A,I5,A,F10.4,A,F10.2,A,F10.2,A,F10.2)') 'Node: ',i,' Index: ',j,' Value: ' &
,dataVariable % Values(dataVariable % Perm(i)),&
' X= ', CoordVals(meshDim * j-2), ' Y= ', CoordVals(meshDim *j-1), ' Z= ', CoordVals(meshDim *j)
END IF
CALL Info('CouplerSolver',infoMessage)
END DO
END SUBROUTINE Print
SUBROUTINE PrintDomain(dataName,mesh)
!-------------------------Strings----------------------------------------------
! CHARACTER(LEN=MAX_NAME_LEN) :: dataName
CHARACTER(LEN=MAX_NAME_LEN) :: infoMessage
character(len=*), intent(in) :: dataName
!-------------------------Elmer_Types----------------------------------------------
TYPE(Variable_t), POINTER :: dataVariable
TYPE(Mesh_t), POINTER :: mesh
!------------------------Data Arrays----------------------------------------------
REAL(KIND=dp), POINTER :: CoordVals(:)
INTEGER, POINTER :: BoundaryPerm(:)
!--------------------------Iterators-------------------------------------
INTEGER :: i,j
dataVariable => VariableGet( mesh % Variables, dataName)
CALL Info('CouplerSolver','Printing ' //TRIM(dataName))
DO i = 1, mesh % NumberOfNodes
write(infoMessage,'(A,I5,A,F10.4,A,F10.2,A,F10.2)') 'Node: ',i,' Value: ' &
,dataVariable % Values(dataVariable % Perm(i)),&
' X= ', mesh % Nodes % x(i), ' Y= ', mesh % Nodes % y(i)
CALL Info('CouplerSolver',infoMessage)
END DO
END SUBROUTINE PrintDomain
SUBROUTINE CreateVariable(dataName,dataType,mesh,BoundaryPerm,Solver,solverParams)
!-------------------------Strings-----------------------------------------------
CHARACTER(LEN=MAX_NAME_LEN) :: dataName
CHARACTER(LEN=MAX_NAME_LEN) :: infoMessage
character(len=*), intent(in) :: dataType
!-------------------------Elmer_Types-------------------------------------------
TYPE(Variable_t), POINTER :: dataVariable
TYPE(Mesh_t), POINTER :: mesh
TYPE(Solver_t) :: Solver
TYPE(ValueList_t), POINTER :: solverParams
!------------------------Data Arrays--------------------------------------------
INTEGER, POINTER :: BoundaryPerm(:)
!------------------------Mesh Data----------------------------------------------
INTEGER :: Dofs
!--------------------------Logic-Control-------------------------------------
LOGICAL :: Found
dataName = ListGetString(solverParams,dataType,Found )
dataVariable => VariableGet( mesh % Variables, dataName)
IF(ASSOCIATED( dataVariable ) ) THEN
CALL Info('CouplerSolver','Using existing variable : '//TRIM(dataName) )
ELSE
CALL FATAL('CouplerSolver', 'Variable does not exist : ' // TRIM(dataName) )
END IF
END SUBROUTINE CreateVariable
SUBROUTINE CopyReadData(dataName,mesh,BoundaryPerm,copyData)
!-------------------------Strings-----------------------------------------------
CHARACTER(LEN=MAX_NAME_LEN) :: dataName
!-------------------------Elmer_Types----------------------------------------------
TYPE(Variable_t), POINTER :: dataVariable
TYPE(Mesh_t), POINTER :: mesh
! !------------------------Data Arrays----------------------------------------------
REAL(KIND=dp), POINTER :: copyData(:)
INTEGER, POINTER :: BoundaryPerm(:)
!--------------------------Iterators-------------------------------------
INTEGER :: i,j
dataVariable => VariableGet( mesh % Variables, dataName)
DO i = 1, mesh % NumberOfNodes
j = BoundaryPerm(i)
IF(j == 0) CYCLE
dataVariable % Values(dataVariable % Perm(i)) = copyData(j)
END DO
END SUBROUTINE CopyReadData
SUBROUTINE CopyWriteData(dataName,mesh,BoundaryPerm,copyData)
!-------------------------Strings-----------------------------------------------
CHARACTER(LEN=MAX_NAME_LEN) :: dataName
!-------------------------Elmer_Types----------------------------------------------
TYPE(Variable_t), POINTER :: dataVariable
TYPE(Mesh_t), POINTER :: mesh
!------------------------Data Arrays----------------------------------------------
REAL(KIND=dp), POINTER :: copyData(:)
INTEGER, POINTER :: BoundaryPerm(:)
!--------------------------Iterators-------------------------------------
INTEGER :: i,j
dataVariable => VariableGet( mesh % Variables, dataName)
DO i = 1, mesh % NumberOfNodes
j = BoundaryPerm(i)
IF(j == 0) CYCLE
copyData(j) = dataVariable % Values(dataVariable % Perm(i))
! IF( dataName == "temperature loads") THEN
! copyData(j) = -1 * dataVariable % Values(dataVariable % Perm(i))
! ELSE
! copyData(j) = dataVariable % Values(dataVariable % Perm(i))
! END IF
END DO
END SUBROUTINE CopyWriteData
END MODULE HelperMethods
SUBROUTINE CouplerSolver( Model,Solver,dt,TransientSimulation)
!------------------------------------------------------------------------------
USE DefUtils
USE HelperMethods
!------------------------------------------------------------------------------
IMPLICIT NONE
!--------------------------UDS Prerequistes------------------------------------
TYPE(Solver_t) :: Solver
TYPE(Model_t) :: Model
REAL(KIND=dp) :: dt
LOGICAL :: TransientSimulation
!--------------------------Variables-Start-------------------------------------------
!--------------------------Logic-Control-------------------------------------
LOGICAL :: Found
!--------------------------MPI-Variables-------------------------------------
INTEGER :: rank,commsize
!--------------------------Elmer-Variables-------------------------------------
!-------------------------Loop_Control-------------------------------------
INTEGER :: itask = 1
!-------------------------Strings----------------------------------------------
CHARACTER(LEN=MAX_NAME_LEN) :: BoundaryName
CHARACTER(LEN=MAX_NAME_LEN) :: infoMessage
!-------------------------Elmer_Types----------------------------------------------
TYPE(Variable_t), POINTER :: readDataVariable,writeDataVariable
TYPE(Mesh_t), POINTER :: mesh
TYPE(ValueList_t), POINTER :: simulation, solverParams ! Simulation gets Simulation list, & solverParams hold solver1,solver 2,etc
!------------------------Data Arrays----------------------------------------------
REAL(KIND=dp), POINTER :: CoordVals(:)
INTEGER, POINTER :: BoundaryPerm(:)
!------------------------Time Variable----------------------------------------------
TYPE(Variable_t), POINTER :: TimeVar
Real(KIND=dp) :: Time
!--------------------------preCICE-Variables-------------------------------------
!-------------------------Strings----------------------------------------------
CHARACTER(LEN=MAX_NAME_LEN) :: config
CHARACTER(LEN=MAX_NAME_LEN) :: participantName, meshName
CHARACTER(LEN=MAX_NAME_LEN) :: readDataName, writeDataName
!-------------------------IDs-Integer----------------------------------------------
INTEGER :: meshID,readDataID, writeDataID, meshDim
!------------------------Data Arrays----------------------------------------------
REAL(KIND=dp), POINTER :: vertices(:), writeData(:), readData(:)
INTEGER, POINTER :: vertexIDs(:)
!------------------------Mesh Data----------------------------------------------
INTEGER :: BoundaryNodes
INTEGER :: Dofs
INTEGER :: dimensions ! ?? Do not know
!----------------------Time Loop Control Variables-----------------------------
INTEGER :: bool
INTEGER :: ongoing
INTEGER :: i,j
!--------------------------Variables-End-------------------------------------------
!--------------------------SAVE-Start-------------------------------------------
SAVE meshName,readDataName,writeDataName
SAVE itask
SAVE BoundaryPerm,CoordVals,vertexIDs
SAVE readData,writeData
SAVE BoundaryNodes
!--------------------------SAVE-End-------------------------------------------
!--------------------------Initialize-Start-------------------------------------------
Simulation => GetSimulation()
Mesh => Solver % Mesh
solverParams => GetSolverParams()
meshDim = Mesh % MaxDim
rank = ParEnv % MyPe
commsize = ParEnv % PEs
select case(itask)
case(1)
CALL Info('CouplerSolver ', 'Initializing Coupler Solver')
!--- First Time Visited, Initialization
!-- First Time Visit, Create preCICE, create nodes at interface
!----------------------------- Initialize---------------------
!----------------Acquire Names for solver---------------------
BoundaryName = GetString( Simulation, 'maskName', Found )
participantName = GetString( Simulation, 'participantName', Found )
!-----------------Convert to preCICE Naming Convention
IF (participantName == 'solid') THEN
participantName = 'Solid'
END IF
IF (participantName == 'fluid') THEN
participantName = 'Fluid'
END IF
meshName = GetString( Simulation, 'meshName', Found )
IF (meshName == 'solid-mesh') THEN
meshName = 'Solid-Mesh'
END IF
IF (meshName == 'fluid-mesh') THEN
meshName = 'Fluid-Mesh'
END IF
!---------------------------------------------------------------------
!-----------------Get Config Path-------------------------------------
config = GetString( Simulation, 'configPath', Found )
Print *, TRIM(BoundaryName)," ",TRIM(participantName)," ",TRIM(meshName)," ",TRIM(config)
!-----------Identify Vertex on Coupling Interface & Save Coordinates--------------------
NULLIFY( BoundaryPerm )
ALLOCATE( BoundaryPerm( Mesh % NumberOfNodes ) )
BoundaryPerm = 0
BoundaryNodes = 0
CALL MakePermUsingMask( Model,Model%Solver,Model%Mesh,BoundaryName,.FALSE., &
BoundaryPerm,BoundaryNodes)
CALL Info('CouplerSolver','Number of nodes at interface:'//TRIM(I2S(BoundaryNodes)))
ALLOCATE( CoordVals(meshDim * BoundaryNodes) )
ALLOCATE(vertexIDs(BoundaryNodes))
DO i=1,Mesh % NumberOfNodes
j = BoundaryPerm(i)
IF(j == 0) CYCLE
IF (meshDim == 2) THEN
CoordVals(meshDim *j-1) = mesh % Nodes % x(i)
CoordVals(meshDim*j) = mesh % Nodes % y(i)
ELSE IF (meshDim == 3) THEN
CoordVals(meshDim *j-2) = mesh % Nodes % x(i)
CoordVals(meshDim *j-1) = mesh % Nodes % y(i)
CoordVals(meshDim *j) = mesh % Nodes % z(i)
END IF
END DO
CALL Info('CouplerSolver','Created nodes at interface')
! !-----------Identify read and write Variables and Create it if it does not exist--------------------
CALL CreateVariable(readDataName,'readDataName',mesh,BoundaryPerm,Solver,solverParams)
CALL CreateVariable(writeDataName,'writeDataName',mesh,BoundaryPerm,Solver,solverParams)
!-----------------------------------------------------------------------------------------
! !---------------Initializing preCICE------------------------------------------
CALL Info('CouplerSolver','Initializing preCICE')
CALL precicef_create(participantName, config, rank, commsize)
CALL Info('CouplerSolver','Setting up mesh in preCICE')
CALL precicef_get_mesh_dimensions(meshName, dimensions)
CALL precicef_set_vertices(meshName, BoundaryNodes, CoordVals, vertexIDs)
ALLOCATE(readData(BoundaryNodes*dimensions))
ALLOCATE(writeData(BoundaryNodes*dimensions))
readData = 0
writeData = 0
CALL precicef_requires_initial_data(bool)
IF (bool.EQ.1) THEN
WRITE (*,*) 'TODO: Provide initial data if needed'
CALL CopyWriteData(writeDataName,mesh,BoundaryPerm,writeData)
IF (writeDataName == 'temperature') THEN
CALL precicef_write_data(meshName, 'Temperature', BoundaryNodes, vertexIDs, writeData)
ELSE IF (writeDataName == 'temperature flux_abs') THEN
CALL precicef_write_data(meshName, 'Heat-Flux', BoundaryNodes, vertexIDs, writeData)
ELSE
CALL precicef_write_data(meshName, writeDataName, BoundaryNodes, vertexIDs, writeData)
END IF
ELSE
WRITE (*,*) 'No initial data required'
ENDIF
CALL precicef_initialize()
CALL precicef_is_coupling_ongoing(ongoing)
! !------------------------------------------------------------------------------
itask = 2
case(2)
!-------------------Copy Read values from Variable to buffer---------------------
CALL precicef_requires_writing_checkpoint(bool)
IF (bool.EQ.1) THEN
WRITE (*,*) 'DUMMY: Writing iteration checkpoint'
ELSE
WRITE (*,*) 'Writing iteration checkpoint is not required'
ENDIF
CALL Info('CouplerSolver ', 'Reading the data from preCICE')
CALL precicef_get_max_time_step_size(dt)
!-------------------Sticking preCICE Naming Convention-------------------------------------
IF (readDataName == 'temperature') THEN
CALL precicef_read_data(meshName, 'Temperature', BoundaryNodes, vertexIDs, dt, readData)
ELSE IF (readDataName == 'temperature flux_abs') THEN
CALL precicef_read_data(meshName, 'Heat-Flux', BoundaryNodes, vertexIDs, dt, readData)
ELSE
CALL precicef_read_data(meshName, readDataName, BoundaryNodes, vertexIDs, dt, readData)
END IF
CALL CopyReadData(readDataName,mesh,BoundaryPerm,readData)
!-----------------------------------------------------------------------------------------
itask = 3
case(3)
!-------------------Copy Write values from Variable to buffer----------------------------
CALL CopyWriteData(writeDataName,mesh,BoundaryPerm,writeData)
!-------------------Sticking preCICE Naming Convention-------------------------------------
IF (writeDataName == 'temperature') THEN
CALL precicef_write_data(meshName, 'Temperature', BoundaryNodes, vertexIDs, writeData)
ELSE IF (writeDataName == 'temperature flux_abs') THEN
CALL precicef_write_data(meshName, 'Heat-Flux', BoundaryNodes, vertexIDs, writeData)
ELSE
CALL precicef_write_data(meshName, writeDataName, BoundaryNodes, vertexIDs, writeData)
END IF
!-------------------Advance preCICE-------------------------------------------------------
CALL precicef_advance(dt)
CALL precicef_is_coupling_ongoing(ongoing)
CALL precicef_requires_reading_checkpoint(bool)
IF (bool.EQ.1) THEN
WRITE (*,*) 'DUMMY: Reading iteration checkpoint'
ELSE
WRITE (*,*) 'Reading iteration checkpoint is not required'
ENDIF
IF(ongoing.EQ.0) THEN
itask = 4
ELSE
itask = 2
END IF
!-----------------------------------------------------------------------------------------
case(4)
CALL Info('CouplerSolver','preCICE Finalize')
CALL precicef_finalize()
DEALLOCATE(writeData)
DEALLOCATE(readData)
DEALLOCATE(vertexIDs)
DEALLOCATE(CoordVals)
DEALLOCATE(BoundaryPerm)
end select
CALL Info('CouplerSolver','Ended')
END SUBROUTINE CouplerSolver