Skip to content

Initialization

This page describes the initialization phase of the Shallow-Water example implementation.

The initialization phase prepares all data structures and parameters before the main computation loop.

! Initialize Tolosa-lib
call tolosa_init()
! Initialize MPI if parallel
call mpi_init_tolosa()

Load the computational mesh:

type(msh) :: mesh
! Load mesh from file
call mesh%load('mesh.msh')
! Generate connectivity
call mesh%build_connectivity()
! Partition for MPI
call mesh%partition(num_procs)

Allocate arrays for physical variables:

! Water depth
real(rp), allocatable :: h(:)
! Velocities
real(rp), allocatable :: u(:), v(:)
! Allocate at cell centers
allocate(h(mesh%nc))
allocate(u(mesh%nc))
allocate(v(mesh%nc))

Set initial values:

! Constant depth
h(:) = 10.0_rp
! At rest
u(:) = 0.0_rp
v(:) = 0.0_rp
! Or call user function
call set_initial_conditions(mesh, h, u, v)

Define boundary types:

! Wall boundaries
mesh%bc_type(1:10) = BC_WALL
! Open boundaries
mesh%bc_type(11:20) = BC_OPEN
! Periodic boundaries
mesh%bc_type(21:30) = BC_PERIODIC

Set numerical and physical parameters:

! Physical parameters
real(rp) :: g = 9.81_rp ! Gravity
real(rp) :: manning = 0.03_rp ! Manning coefficient
! Numerical parameters
real(rp) :: cfl = 0.5_rp ! CFL number
integer :: scheme = SCHEME_HLL
! Time parameters
real(rp) :: dt ! Time step
real(rp) :: t_end = 100.0_rp ! End time

Setup output files:

type(VTK_file) :: vtk_output
type(csv_file) :: csv_output
! Initialize VTK output
call vtk_output%init('results/output', mesh)
! Initialize CSV for time series
call csv_output%init('results/gauges.csv')

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')
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_simulation

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)

Always check for errors:

integer :: ierr
call mesh%load(mesh_file, ierr)
if (ierr /= 0) then
print *, 'Error loading mesh'
call tolosa_abort()
end if