Skip to content

Commit df4bd30

Browse files
Get coupling grid information from Elmer internals instead of file (#772)
* Introduce bool is_root_rank * Implement functionality from elmer_grid.{h,c} into Fortran if possible * Do coordinate conversion in project_to_lonlat.{c,h} * Cleanup logging
1 parent b9a132e commit df4bd30

File tree

8 files changed

+221
-499
lines changed

8 files changed

+221
-499
lines changed

elmerice/Solvers/yac2elmer.F90

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ SUBROUTINE YAC2Elmer( Model,Solver,dt,TransientSimulation )
22
USE DefUtils, ONLY: GetSolverParams, GetMesh, GetNOFActive, &
33
DefaultVariableAdd, GetLogical, GetLogical, GetString, MAX_NAME_LEN, &
44
VariableGet, ParEnv, variable_on_elements
5+
USE GeneralUtils, ONLY: I2S
56
USE Types, ONLY: Model_t, Solver_t, Mesh_t, Variable_t, ValueList_t, dp
67
USE Messages, ONLY: Message, FATAL, INFO, USE_YAC
7-
USE elmer_coupling, ONLY: coupling_setup
8+
USE elmer_coupling, ONLY: coupling_setup, is_root_rank
89
USE elmer_ebfm_coupling, ONLY: elmer_ebfm_interface, t_ice_field, smb_field, &
910
runoff_field, surface_height_field
1011
! USE elmer_icon_coupling, ONLY: elmer_icon_interface, clt_field, pr_field
1112

1213
IMPLICIT NONE
1314

14-
TYPE(Model_t) :: Model
15-
TYPE(Solver_t) :: Solver
16-
REAL(KIND=dp) :: dt
17-
LOGICAL :: TransientSimulation
15+
TYPE(Model_t), INTENT(IN) :: Model
16+
TYPE(Solver_t), INTENT(IN) :: Solver
17+
REAL(KIND=dp), INTENT(IN) :: dt
18+
LOGICAL, INTENT(IN) :: TransientSimulation
1819

1920

2021
TYPE(ValueList_t), POINTER :: SolverParams
@@ -23,9 +24,8 @@ SUBROUTINE YAC2Elmer( Model,Solver,dt,TransientSimulation )
2324
! parameters to be read in from this solvers section in the sif
2425
LOGICAL :: couple_to_ebfm, couple_to_icon ! define which component is coupled to Elmer
2526

26-
CHARACTER(LEN=1024) :: config_file, grid_dir, model_tstep
27-
INTEGER :: num_parts, elmer_mesh_partitions, comm_rank, comm_size, ierror
28-
INTEGER :: I, t, ierr
27+
CHARACTER(LEN=1024) :: config_file, model_tstep
28+
INTEGER :: I, t, ierr, dt_hours
2929
INTEGER, POINTER :: t_icePerm(:), smbPerm(:), runoffPerm(:)
3030
! INTEGER, POINTER :: cltPerm(:), prPerm(:) ! ICON is not supported at the moment
3131
LOGICAL :: Parallel, FirstTime=.TRUE., UnFoundFatal=.TRUE.
@@ -60,6 +60,10 @@ SUBROUTINE YAC2Elmer( Model,Solver,dt,TransientSimulation )
6060
CALL FATAL(SolverName,'No keyword >Couple To ICON< found in yac2elmer solver')
6161
END IF
6262

63+
IF (.NOT. (couple_to_ebfm .OR. couple_to_icon)) THEN
64+
CALL FATAL(SolverName,'At least one of >Couple To EBFM< or >Couple To ICON< must be TRUE')
65+
END IF
66+
6367
! TODO: remove this check when ICON coupling is implemented
6468
IF (couple_to_icon) THEN
6569
CALL FATAL(SolverName,'>Couple To ICON< is currently not supported. Please set to FALSE')
@@ -68,9 +72,11 @@ SUBROUTINE YAC2Elmer( Model,Solver,dt,TransientSimulation )
6872
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6973
! retrieve the timestep in hours
7074
Mesh => Solver % Mesh
71-
write(model_tstep, *) int(dt * 8760)
72-
WRITE(Message,*) 'ELMER timestep size in hours:', TRIM(model_tstep)
73-
CALL INFO(SolverName,Message,Level=3)
75+
76+
dt_hours = int(dt * 8760)
77+
write(model_tstep, *) dt_hours
78+
CALL INFO(SolverName, &
79+
'ELMER timestep size in hours:' // I2S(dt_hours), Level=3)
7480
IF (FirstTime) THEN
7581

7682

@@ -81,13 +87,11 @@ SUBROUTINE YAC2Elmer( Model,Solver,dt,TransientSimulation )
8187
IF ((ParEnv % PEs <= 1) .AND. ( .NOT. ThisMesh % SingleMesh )) THEN
8288
CALL FATAL(SolverName,'Only parallel runs can use this solver')
8389
ELSE
84-
grid_dir= TRIM(ThisMesh % Name)
85-
elmer_mesh_partitions = ParEnv % PEs
86-
WRITE(Message,*) 'Running on ', TRIM(grid_dir),' with ',ParEnv % PEs ,' partitions'
87-
CALL INFO(SolverName,Message,Level=3)
90+
CALL INFO(SolverName, &
91+
'Running with ' // I2S(ParEnv % PEs) // ' partitions', Level=3)
8892
END IF
8993

90-
CALL coupling_setup(TRIM(grid_dir), elmer_mesh_partitions, TRIM(model_tstep), couple_to_ebfm, couple_to_icon)
94+
CALL coupling_setup(ThisMesh, TRIM(model_tstep), couple_to_ebfm, couple_to_icon)
9195

9296
IF (couple_to_ebfm) THEN
9397
! setting up Elmer-side variables for receiving YAC variables
@@ -140,39 +144,33 @@ SUBROUTINE YAC2Elmer( Model,Solver,dt,TransientSimulation )
140144

141145
FirstTime = .FALSE.
142146

143-
WRITE(Message,*) "Coupling setup with ",TRIM(grid_dir)," on ",&
144-
elmer_mesh_partitions, " partitions for YAC coupling done"
145-
CALL INFO(SolverName,Message,Level=1)
147+
CALL INFO(SolverName, "YAC coupling setup done", Level=1)
146148
END IF
147149
!!!!!!!!!! DO WE HAVE TO INITIALIZE WITH EVERY CALL ? !!!!!!!!!!!!!!
148150

149151
IF (couple_to_ebfm) THEN
150-
WRITE(Message,*) 'BEFORE ELMER EBFM INTERFACE'
151-
CALL INFO(SolverName,Message,Level=3)
152+
CALL INFO(SolverName, 'BEFORE ELMER EBFM INTERFACE', Level=3)
152153
! couple with EBFM
153-
CALL elmer_ebfm_interface(ParEnv % MyPE)
154-
WRITE(Message,*) 'AFTER ELMER EBFM INTERFACE'
155-
CALL INFO(SolverName,Message,Level=3)
154+
CALL elmer_ebfm_interface(is_root_rank)
155+
CALL INFO(SolverName, 'AFTER ELMER EBFM INTERFACE', Level=3)
156156

157157
t_iceVar => VariableGet( Mesh % Variables, 'T_ice' )
158158
smbVar => VariableGet( Mesh % Variables, 'smb' )
159159
runoffVar => VariableGet( Mesh % Variables, 'runoff' )
160160
ZsSol => VariableGet( Model % Mesh % Variables, "Zs" ,UnFoundFatal=UnFoundFatal)
161-
WRITE(Message,*) 'AFTER GETTING VARIABLES'
162-
CALL INFO(SolverName,Message,Level=3)
161+
CALL INFO(SolverName, 'AFTER GETTING VARIABLES', Level=3)
163162
IF ((.NOT.ASSOCIATED(t_iceVar)) .OR. (.NOT.ASSOCIATED(smbVar)) .OR. (.NOT.ASSOCIATED(runoffVar))) THEN
164163
CALL FATAL(SolverName,'Elmer variables not associated')
165164
END IF
166165

167-
WRITE(Message,*) 'BEFORE WRITING NODAL VALUES'
168-
CALL INFO(SolverName,Message,Level=3)
166+
CALL INFO(SolverName, 'BEFORE WRITING NODAL VALUES', Level=3)
169167
!write over values for nodes
170168
DO i=1, Mesh % NumberOfNodes
171169
t_iceVar % Values(t_iceVar % Perm(i)) = t_ice_field(i,1)
172170
runoffVar % Values(runoffVar % Perm(i)) = runoff_field(i,1)
173171
surface_height_field(i,1) = ZsSol % Values(ZsSol % Perm(i))
174172
END DO
175-
CALL INFO(SolverName,Message,Level=3)
173+
CALL INFO(SolverName, 'BEFORE WRITING ELEMENT VALUES', Level=3)
176174
! write over values for elements
177175
DO t=1, GetNOFActive(Solver)
178176
smbVar % Values(smbVar % Perm(t)) = smb_field(t,1)
@@ -186,7 +184,7 @@ SUBROUTINE YAC2Elmer( Model,Solver,dt,TransientSimulation )
186184
IF (couple_to_icon) THEN
187185
CALL FATAL(SolverName,'ICON coupling not yet implemented')
188186
! TODO: stub implementation for ICON coupling
189-
! CALL elmer_icon_interface(ParEnv % MyPE)
187+
! CALL elmer_icon_interface(is_root_rank)
190188
! cltVar => VariableGet( Mesh % Variables, 'tas' )
191189
! prVar => VariableGet( Mesh % Variables, 'pr_snow' )
192190
END IF

fem/src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ SET(solverlib_SOURCES AddrFunc.F90 NavierStokes.F90 NavierStokesGeneral.F90
5050
# COMPILE_DEFINITIONS FULL_INDUCTION)
5151

5252
IF(WITH_YAC)
53-
LIST(APPEND solverlib_SOURCES elmer_grid.c elmer_coupling.F90)
53+
LIST(APPEND solverlib_SOURCES project_to_lonlat.c elmer_coupling.F90)
5454
ENDIF()
5555

5656

fem/src/Messages.F90

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ MODULE Messages
5454
#ifdef HAVE_XIOS
5555
USE XIOS, ONLY: xios_context_finalize, xios_finalize
5656
#endif
57-
57+
5858
#ifdef HAVE_YAC
5959
USE elmer_coupling, ONLY: coupling_finalize
6060
#endif
@@ -338,7 +338,6 @@ SUBROUTINE Fatal( Caller, String, noAdvance )
338338
ENDIF
339339
#endif
340340

341-
342341
#ifdef HAVE_YAC
343342
IF (USE_YAC) THEN
344343
CALL coupling_finalize()

fem/src/SParIterComm.F90

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ MODULE SParIterComm
4949
USE Messages, ONLY: Info, Fatal, Message, OutputPE, InfoActive
5050
#ifdef HAVE_XIOS
5151
USE Messages, ONLY: USE_XIOS
52+
#endif
53+
#ifdef HAVE_YAC
54+
USE Messages, ONLY: USE_YAC
5255
#endif
5356
USE LoadMod, ONLY : RealTime
5457
USE SParIterGlobals
@@ -103,7 +106,7 @@ MODULE SParIterComm
103106
#endif
104107

105108
#ifdef HAVE_YAC
106-
USE elmer_coupling, ONLY: coupling_init, coupling_finalize, coupling_setup
109+
USE elmer_coupling, ONLY: coupling_init, coupling_finalize
107110
#endif
108111

109112
#ifdef HAVE_XIOS
@@ -293,6 +296,7 @@ FUNCTION ParCommInit( ) RESULT ( ParallelEnv )
293296
#endif
294297
IF ( ierr /= 0 ) RETURN
295298

299+
! Only needed for MPI_COMM_SPLIT if not using mpi_handshake
296300
CALL MPI_COMM_SIZE( MPI_COMM_WORLD, ParEnv % PEs, ierr )
297301
CALL MPI_COMM_RANK( MPI_COMM_WORLD, ParEnv % MyPE, ierr )
298302

@@ -395,28 +399,10 @@ FUNCTION ParCommInit( ) RESULT ( ParallelEnv )
395399
# endif
396400
END IF
397401
#endif
398-
399-
! Use YAC library for coupling
400-
!
401-
#ifdef HAVE_YAC
402-
IF (USE_YAC) THEN
403-
WRITE(Message,'(A,A)') &
404-
"Using YAC coupler with config-file:", &
405-
TRIM(yac_config_file)
406-
407-
CALL INFO("SparIterComm",Message,Level=25)
408-
! TODO: Refactor to also provide GROUP_NAMES(XIOS_GROUP_IDX) here
409-
! CALL coupling_init(yac_config_file, ELMER_COMM_WORLD,&
410-
! GROUP_COMMS(COUPLER_GROUP_IDX), GROUP_NAMES(ELMER_GROUP_IDX))
411-
CALL coupling_init(yac_config_file, ELMER_COMM_WORLD,&
412-
GROUP_COMMS(COUPLER_GROUP_IDX))
413-
END IF
414-
#endif
415402

403+
! Set ParEnv values according to ELMER_COMM_WORLD
416404
ParEnv % ActiveComm = ELMER_COMM_WORLD
417405

418-
!ELMER_COMM_WORLD=MPI_COMM_WORLD
419-
420406
CALL MPI_COMM_SIZE( ELMER_COMM_WORLD, ParEnv % PEs, ierr )
421407
IF ( ierr /= 0 ) THEN
422408
CALL MPI_Finalize( ierr )
@@ -437,6 +423,28 @@ FUNCTION ParCommInit( ) RESULT ( ParallelEnv )
437423
Parenv % NumOfNeighbours = 0
438424
ParEnv % Initialized = .TRUE.
439425
END IF
426+
427+
! Use YAC library for coupling
428+
! Needs initialized ParEnv % MyPE.
429+
#ifdef HAVE_YAC
430+
IF (USE_YAC) THEN
431+
IF ( .NOT. ParEnv % Initialized ) THEN
432+
WRITE( Message,'(A)') 'ParEnv not initialized before coupling_init'
433+
CALL Fatal( 'ParCommInit', Message )
434+
END IF
435+
436+
WRITE(Message,'(A,A)') &
437+
"Using YAC coupler with config-file:", &
438+
TRIM(yac_config_file)
439+
440+
CALL INFO("SparIterComm",Message,Level=25)
441+
! TODO: Refactor to also provide GROUP_NAMES(XIOS_GROUP_IDX) here
442+
! CALL coupling_init(yac_config_file, ParEnv % MyPE ,&
443+
! GROUP_COMMS(COUPLER_GROUP_IDX), GROUP_NAMES(ELMER_GROUP_IDX))
444+
CALL coupling_init(yac_config_file, ParEnv % MyPE ,&
445+
GROUP_COMMS(COUPLER_GROUP_IDX))
446+
END IF
447+
#endif
440448
!-----------------------------------------------------------------------
441449
END FUNCTION ParCommInit
442450
!-----------------------------------------------------------------------
@@ -4175,7 +4183,7 @@ END SUBROUTINE ExchangeRHSIf
41754183
!> Send parts of the result vector to neighbours
41764184
!----------------------------------------------------------------------
41774185
SUBROUTINE ExchangeResult( SourceMatrix, SplittedMatrix, ParallelInfo, XVec )
4178-
USE types
4186+
USE Types
41794187
IMPLICIT NONE
41804188

41814189
TYPE(SplittedMatrixT) :: SplittedMatrix
@@ -4780,7 +4788,7 @@ END SUBROUTINE Recv_LocIf_size
47804788
!> Receive interface block contributions to vector from neighbours
47814789
!
47824790
SUBROUTINE Recv_LocIf( SplittedMatrix, n, neigh, sizes, requests, buffer )
4783-
uSE Types
4791+
USE Types
47844792
IMPLICIT NONE
47854793

47864794
TYPE (SplittedMatrixT) :: SplittedMatrix

0 commit comments

Comments
 (0)