Initialization
This page describes the initialization phase of the Shallow-Water example implementation.
Overview
Section titled “Overview”The initialization phase prepares all data structures and parameters before the main computation loop.
Main Steps
Section titled “Main Steps”1. Library Initialization
Section titled “1. Library Initialization”! Initialize Tolosa-libcall tolosa_init()
! Initialize MPI if parallelcall mpi_init_tolosa()2. Mesh Loading
Section titled “2. Mesh Loading”Load the computational mesh:
type(msh) :: mesh
! Load mesh from filecall mesh%load('mesh.msh')
! Generate connectivitycall mesh%build_connectivity()
! Partition for MPIcall mesh%partition(num_procs)3. Field Allocation
Section titled “3. Field Allocation”Allocate arrays for physical variables:
! Water depthreal(rp), allocatable :: h(:)
! Velocitiesreal(rp), allocatable :: u(:), v(:)
! Allocate at cell centersallocate(h(mesh%nc))allocate(u(mesh%nc))allocate(v(mesh%nc))4. Initial Conditions
Section titled “4. Initial Conditions”Set initial values:
! Constant depthh(:) = 10.0_rp
! At restu(:) = 0.0_rpv(:) = 0.0_rp
! Or call user functioncall set_initial_conditions(mesh, h, u, v)5. Boundary Conditions
Section titled “5. Boundary Conditions”Define boundary types:
! Wall boundariesmesh%bc_type(1:10) = BC_WALL
! Open boundariesmesh%bc_type(11:20) = BC_OPEN
! Periodic boundariesmesh%bc_type(21:30) = BC_PERIODIC6. Parameters Setup
Section titled “6. Parameters Setup”Set numerical and physical parameters:
! Physical parametersreal(rp) :: g = 9.81_rp ! Gravityreal(rp) :: manning = 0.03_rp ! Manning coefficient
! Numerical parametersreal(rp) :: cfl = 0.5_rp ! CFL numberinteger :: scheme = SCHEME_HLL
! Time parametersreal(rp) :: dt ! Time stepreal(rp) :: t_end = 100.0_rp ! End time7. Output Initialization
Section titled “7. Output Initialization”Setup output files:
type(VTK_file) :: vtk_outputtype(csv_file) :: csv_output
! Initialize VTK outputcall vtk_output%init('results/output', mesh)
! Initialize CSV for time seriescall csv_output%init('results/gauges.csv')8. Timers Setup
Section titled “8. Timers Setup”Initialize performance timers:
type(timer) :: t_flux, t_update, t_output
call t_flux%init('Flux computation')call t_update%init('Field update')call t_output%init('Output writing')Complete Initialization Routine
Section titled “Complete Initialization Routine”subroutine initialize_simulation() use mod_tolosa implicit none
! Initialize library call tolosa_init()
! Load mesh call mesh%load(mesh_file) call mesh%build_connectivity()
! Allocate fields allocate(h(mesh%nc)) allocate(u(mesh%nc)) allocate(v(mesh%nc))
! Set initial conditions call set_initial_conditions()
! Setup boundaries call setup_boundaries()
! Initialize outputs call init_outputs()
! Initialize timers call init_timers()
if (rank == 0) then print *, 'Initialization complete' print *, 'Number of cells:', mesh%nc print *, 'Number of processors:', num_procs end if
end subroutine initialize_simulationMemory Considerations
Section titled “Memory Considerations”For large meshes, memory usage is approximately:
where:
is the number of cells is the average number of edges per cell - 3 fields (h, u, v) × 8 bytes (real64)
Error Checking
Section titled “Error Checking”Always check for errors:
integer :: ierr
call mesh%load(mesh_file, ierr)if (ierr /= 0) then print *, 'Error loading mesh' call tolosa_abort()end ifNext Steps
Section titled “Next Steps”- Computation Loop - Main time-stepping algorithm
- Finalization - Cleanup and final output