Boundary Conditions
The bcs submodule handles boundary condition loading, interpolation, and application.
At a Glance
GeoDynamo.bcs
├── load_boundary_conditions! # Load from files
├── read_netcdf_boundary_data # Read raw data
├── write_netcdf_boundary_data # Write data
└── validate_netcdf_boundary_file # Validate structureusing GeoDynamo
grid = SphericalShellGrid(nr = 64, lmax = 31)
model = GeodynamoModel(grid)
simulation = Simulation(model; Δt = 1e-5, stop_time = 0.02)
state = simulation.model.state
GeoDynamo.bcs.load_boundary_conditions!(state.temperature, GeoDynamo.TEMPERATURE, Dict(
:inner => (:uniform, 1.0),
:outer => (:dirichlet, 0.0),
))Boundary Condition API
GeoDynamo.bcs.load_boundary_conditions! — Function
load_boundary_conditions!(field, field_type::FieldType, boundary_specs::Dict)Unified interface to load boundary conditions for any field type.
Arguments
field: Field structure to apply boundary conditions tofield_type: Type of field (TEMPERATURE, COMPOSITION, VELOCITY, MAGNETIC)boundary_specs: Dictionary specifying boundary condition sources
Current Behavior
- For
TEMPERATUREandCOMPOSITION, this high-level helper accepts programmatic tuple specs like(:uniform, value),(:dirichlet, value), and(:neumann, flux)and immediately embeds them into the field. - File-based scalar loading is validated but not applied here because the matrix-embedded BC workflow needs an explicit field-side setup path.
VELOCITYandMAGNETICBCs remain matrix-embedded and are not configured through this helper.
Examples
# Temperature boundaries
load_boundary_conditions!(temp_field, TEMPERATURE, Dict(
:inner => (:uniform, 1.0),
:outer => (:dirichlet, 0.0),
))
# Composition boundaries
load_boundary_conditions!(comp_field, COMPOSITION, Dict(
:inner => (:neumann, 0.0),
:outer => (:dirichlet, 0.0),
))GeoDynamo.bcs.update_time_dependent_boundaries! — Function
update_time_dependent_boundaries!(field, field_type::FieldType, current_time::Float64)Update time-dependent boundary conditions for any field type.
For scalar fields, this refreshes the active boundary time index and rebuilds the spectral boundary rows from the current physical-space boundary slice. Vector BCs remain matrix-embedded and are therefore rejected if a time-dependent boundary set is attached.
GeoDynamo.bcs.validate_boundary_files — Function
validate_boundary_files(field_type::FieldType, boundary_specs::Dict, config)Validate boundary condition specifications for any field type.
- NetCDF file specs are checked with
validate_netcdf_boundary_file. - Scalar tuple specs are validated via the programmatic boundary parser.
- Velocity and magnetic BCs reject tuple specs here because their supported configuration path is still the matrix-embedded timestep system.
GeoDynamo.bcs.get_current_boundaries — Function
get_current_boundaries(field, field_type::FieldType)Get current boundary values for any field type.
GeoDynamo.bcs.print_boundary_summary — Function
print_boundary_summary(field, field_type::FieldType)Print a summary of loaded boundary conditions for any field type.
GeoDynamo.bcs.get_boundary_module_info — Function
get_boundary_module_info()Get information about the boundary-conditions module, including supported field types, data formats, and major features.
GeoDynamo.bcs.SpectralBoundaryCoefficients — Type
SpectralBoundaryCoefficients{T}Stores spectral boundary condition coefficients for inner and outer boundaries. Row 1 = inner boundary, Row 2 = outer boundary (matching Fortran convention).
Fields
bc_real::Matrix{T}: Real part of spectral BCs [2, nlm]bc_imag::Matrix{T}: Imaginary part of spectral BCs [2, nlm]nlm::Int: Number of spectral modessource_file::String: Path to the source filesource_format::Symbol: Format used to load (:physical or :spectral)
GeoDynamo.bcs.load_spectral_bc_from_file — Function
load_spectral_bc_from_file(filename::String, config; format::Symbol=:physical, T::Type=Float64)Load boundary conditions from a NetCDF file and return spectral coefficients.
Arguments
filename: Path to the NetCDF fileconfig: SHTnsKitConfig (or any object withlmax,mmax,nlmfields)format::physicalfor physical-space data,:spectralfor pre-computed coefficientsT: Numeric type for the coefficients (default Float64)
NetCDF File Formats
Physical format (:physical):
- Variables:
"inner"with shape(nlat, nlon)and/or"outer"with shape(nlat, nlon) - Or a single variable with 3rd dimension of size 2:
"temperature"with shape(nlat, nlon, 2)(slice 1 = inner, slice 2 = outer)
Spectral format (:spectral, Fortran-compatible):
- Variables:
"Cbc_Re"[2, nlm] and"Cbc_Im"[2, nlm] - Row 1 = inner boundary, Row 2 = outer boundary
Returns
SpectralBoundaryCoefficients{T}with spectral BC data
GeoDynamo.bcs.store_bc_in_field! — Function
store_bc_in_field!(field, bc_coeffs::SpectralBoundaryCoefficients)Store spectral boundary coefficients into a field's boundary_interpolation_cache. The field must have a boundary_interpolation_cache attribute.
After calling this, get_bc_vectors_from_field(field) will return the stored vectors.
GeoDynamo.bcs.get_bc_vectors_from_field — Function
get_bc_vectors_from_field(field)Extract boundary condition vectors from a field's cache for use in implicit solves.
Returns
A named tuple (inner_real, outer_real, inner_imag, outer_imag) where each element is either a Vector{T} of length nlm or nothing if no file BCs are loaded.
Boundary Data And File API
GeoDynamo.bcs.BoundaryData — Type
BoundaryData{T}Common data structure for boundary condition data from any source.
GeoDynamo.bcs.BoundaryConditionSet — Type
BoundaryConditionSet{T}Complete set of boundary conditions for inner and outer boundaries.
GeoDynamo.bcs.BoundaryCache — Type
BoundaryCache{T}Cache structure for storing processed boundary data.
GeoDynamo.bcs.create_boundary_data — Function
create_boundary_data(values::Array{T}, field_type::String;
theta=nothing, phi=nothing, time=nothing,
units="", description="", file_path="programmatic") where TCreate a BoundaryData structure from raw data.
GeoDynamo.bcs.validate_boundary_compatibility — Function
validate_boundary_compatibility(inner::BoundaryData, outer::BoundaryData, field_name::String)Validate that inner and outer boundary data are compatible.
GeoDynamo.bcs.get_boundary_statistics — Function
get_boundary_statistics(boundary_data::BoundaryData)Get statistical information about boundary data.
GeoDynamo.bcs.print_boundary_data_info — Function
print_boundary_data_info(boundary_data::BoundaryData, prefix::String="")Print detailed information about boundary data.
GeoDynamo.bcs.print_boundary_info — Function
print_boundary_info(boundary_set::BoundaryConditionSet)Print comprehensive information about a boundary condition set.
GeoDynamo.bcs.cache_boundary_data! — Function
cache_boundary_data!(cache::BoundaryCache{T}, key::String, data::Array{T}) where TCache processed boundary data.
GeoDynamo.bcs.get_cached_data — Function
get_cached_data(cache::BoundaryCache{T}, key::String) where TRetrieve cached boundary data.
GeoDynamo.bcs.clear_boundary_cache! — Function
clear_boundary_cache!(cache::BoundaryCache)Clear all cached boundary data.
GeoDynamo.bcs.find_boundary_time_index — Function
find_boundary_time_index(boundary_set::BoundaryConditionSet, current_time::Float64)Find the appropriate time index for the current simulation time. Used by field-specific boundary condition modules.
GeoDynamo.bcs.read_netcdf_boundary_data — Function
read_netcdf_boundary_data(filename::String; precision::Type{T}=Float64) where TRead boundary condition data from a NetCDF file.
Returns a BoundaryData structure containing all boundary information.
GeoDynamo.bcs.write_netcdf_boundary_data — Function
write_netcdf_boundary_data(filename::String, boundary_data::BoundaryData)Write boundary condition data to a NetCDF file.
GeoDynamo.bcs.validate_netcdf_boundary_file — Function
validate_netcdf_boundary_file(filename::String, required_vars::Vector{String}=[])Validate that a NetCDF boundary condition file has the required structure.
GeoDynamo.bcs.get_netcdf_file_info — Function
get_netcdf_file_info(filename::String)Get information about a NetCDF boundary condition file.
Boundary Application API
GeoDynamo.bcs.initialize_boundary_conditions! — Function
initialize_boundary_conditions!(𝔽, field_type::FieldType)Initialize boundary condition support for a field structure.
IMPORTANT: The field structure must already have the following fields defined:
- boundaryconditionset (can be nothing)
- boundaryinterpolationcache (BoundaryInterpolationCache)
- boundarytimeindex (Ref{Int})
For scalar fields (TEMPERATURE, COMPOSITION):
- bctypeinner, bctypeouter (Vector{Int})
- boundary_values (Matrix)
For vector fields (VELOCITY, MAGNETIC):
- toroidal and poloidal components, each with bctypeinner, bctypeouter, boundary_values
Note: The field struct must be a mutable struct since this function modifies its fields.
GeoDynamo.bcs.apply_boundary_conditions! — Function
apply_boundary_conditions!(𝔽, field_type::FieldType, solver_state)Apply boundary conditions during solver operations.
This function integrates boundary conditions with the timestepping and solving process.
GeoDynamo.bcs.validate_field_boundary_compatibility — Function
validate_field_boundary_compatibility(𝔽, field_type::FieldType, boundary_set::BoundaryConditionSet)Validate that a field structure is compatible with boundary conditions.
GeoDynamo.bcs.copy_boundary_conditions! — Function
copy_boundary_conditions!(dest_𝔽, src_𝔽, field_type::FieldType)Copy boundary conditions from one field to another. Both fields must have boundary condition fields already defined.
GeoDynamo.bcs.reset_boundary_conditions! — Function
reset_boundary_conditions!(𝔽, field_type::FieldType)Reset/clear boundary conditions for a field.
GeoDynamo.bcs.get_boundary_condition_summary — Function
get_boundary_condition_summary(𝔽, field_type::FieldType)Get a summary of the current boundary condition state.
GeoDynamo.bcs.apply_temperature_boundaries! — Function
apply_temperature_boundaries!(temp_field, pbs::ProgrammaticBoundarySet; time=0.0)Apply programmatic temperature boundary conditions to a temperature field.
Transforms physical-space boundary data to spectral coefficients and stores them in the field's boundary_values matrix, along with setting BC types.
Arguments
temp_field: SHTnsTemperatureField to apply boundaries topbs: ProgrammaticBoundarySet containing boundary data and BC typestime: Current simulation time (for time-dependent boundaries)
GeoDynamo.bcs.apply_composition_boundaries! — Function
apply_composition_boundaries!(comp_field, pbs::ProgrammaticBoundarySet; time=0.0)Apply programmatic composition boundary conditions to a composition field. Same interface as apply_temperature_boundaries!.
Boundary Interpolation And Programmatic API
GeoDynamo.bcs.interpolate_boundary_to_grid — Function
interpolate_boundary_to_grid(boundary_data::BoundaryData, target_theta::Vector{T},
target_phi::Vector{T}, time_index::Int=1) where TInterpolate boundary data to a target grid using bilinear interpolation.
GeoDynamo.bcs.create_interpolation_cache — Function
create_interpolation_cache(boundary_data::BoundaryData, target_theta::Vector{T},
target_phi::Vector{T}) where TCreate interpolation cache for efficient repeated interpolations.
GeoDynamo.bcs.interpolate_with_cache — Function
interpolate_with_cache(boundary_data::BoundaryData, cache::Dict, time_index::Int=1)Perform interpolation using pre-computed cache for efficiency.
GeoDynamo.bcs.validate_interpolation_grids — Function
validate_interpolation_grids(src_theta::Vector, src_phi::Vector,
tgt_theta::Vector, tgt_phi::Vector)Validate that source and target grids are compatible for interpolation.
GeoDynamo.bcs.check_interpolation_bounds — Function
check_interpolation_bounds(boundary_data::BoundaryData, target_theta::Vector{T},
target_phi::Vector{T}) where TCheck if target grid is within the bounds of the source grid and warn if extrapolation is needed.
GeoDynamo.bcs.get_interpolation_statistics — Function
get_interpolation_statistics(boundary_data::BoundaryData, interpolated_data::Array{T}) where TCompute statistics comparing original and interpolated data.
GeoDynamo.bcs.estimate_interpolation_error — Function
estimate_interpolation_error(boundary_data::BoundaryData, target_theta::Vector{T},
target_phi::Vector{T}) where TEstimate interpolation error based on grid resolution differences.
GeoDynamo.bcs.Ylm — Type
Ylm(l::Int, m::Int)Spherical harmonic pattern specifier for programmatic boundary conditions.
Examples
# Create Y₂¹ pattern with amplitude 0.5
boundary = create_programmatic_boundary(Ylm(2, 1), config, 0.5)
# Create Y₄⁻² pattern
boundary = create_programmatic_boundary(Ylm(4, -2), config, 1.0)
# Time-dependent oscillating Y₃² pattern
boundary = create_time_dependent_programmatic_boundary(Ylm(3, 2), config, (0.0, 10.0), 100;
amplitude=0.3)GeoDynamo.bcs.ProgrammaticBoundarySet — Type
ProgrammaticBoundarySet{T}Wrapper around BoundaryConditionSet that also stores BC types for each boundary. Returned by create_programmatic_temperature_boundaries and create_programmatic_composition_boundaries.
GeoDynamo.bcs.create_programmatic_boundary — Function
create_programmatic_boundary(ylm::Ylm, config, amplitude::Real=1.0; kwargs...)Create a boundary pattern from a spherical harmonic Yₗᵐ using SHTnsKit synthesis. Values are produced on the Gauss-Legendre grid of the given config.
Examples
boundary = create_programmatic_boundary(Ylm(2, 1), config, 0.5)
boundary = create_programmatic_boundary(Ylm(4, -2), config, 1.0)GeoDynamo.bcs.create_time_dependent_programmatic_boundary — Function
create_time_dependent_programmatic_boundary(ylm::Ylm, config, time_span, ntime; kwargs...)Create a time-dependent boundary pattern from a spherical harmonic Yₗᵐ with cosine time modulation. Values are produced on the Gauss-Legendre grid.
Example
boundary = create_time_dependent_programmatic_boundary(Ylm(3, 2), config, (0.0, 10.0), 100;
amplitude=0.5, time_factor=2π)GeoDynamo.bcs.add_noise_to_boundary — Function
add_noise_to_boundary(boundary_data::BoundaryData, noise_amplitude::Real,
noise_type::Symbol=:gaussian)Add noise to existing boundary data.
GeoDynamo.bcs.smooth_boundary_data — Function
smooth_boundary_data(boundary_data::BoundaryData, smoothing_radius::Real)Apply spatial smoothing to boundary data.
GeoDynamo.bcs.create_programmatic_temperature_boundaries — Function
create_programmatic_temperature_boundaries(inner_spec::Tuple, outer_spec::Tuple, cfg)Create a ProgrammaticBoundarySet for temperature from programmatic specifications.
Arguments
inner_spec: Tuple specifying inner boundary, e.g.(:uniform, 100.0)outer_spec: Tuple specifying outer boundary, e.g.(:uniform, 250.0)cfg: SHTnsKitConfig with grid parameters
Returns
A ProgrammaticBoundarySet{Float64} containing boundary data and BC types.
Examples
bset = create_programmatic_temperature_boundaries((:uniform, 1.0), (:uniform, 0.0), cfg)
bset = create_programmatic_temperature_boundaries((:dirichlet, 1.0), (:neumann, 0.0), cfg)GeoDynamo.bcs.create_programmatic_composition_boundaries — Function
create_programmatic_composition_boundaries(inner_spec::Tuple, outer_spec::Tuple, cfg)Create a ProgrammaticBoundarySet for composition from programmatic specifications. Same interface as create_programmatic_temperature_boundaries.
GeoDynamo.bcs.create_hybrid_temperature_boundaries — Function
create_hybrid_temperature_boundaries(file_spec::String, prog_spec::Tuple, cfg; swap_boundaries=false)Create temperature boundaries with one from file and one programmatic. When swap_boundaries=false, file is inner and programmatic is outer. When swap_boundaries=true, file is outer and programmatic is inner.
GeoDynamo.bcs.create_hybrid_composition_boundaries — Function
create_hybrid_composition_boundaries(file_spec::String, prog_spec::Tuple, cfg; swap_boundaries=false)Create composition boundaries with one from file and one programmatic.
GeoDynamo.bcs.load_temperature_boundaries_from_files — Function
load_temperature_boundaries_from_files(inner_file::String, outer_file::String, cfg)Load temperature boundary conditions from two NetCDF files.
GeoDynamo.bcs.load_composition_boundaries_from_files — Function
load_composition_boundaries_from_files(inner_file::String, outer_file::String, cfg)Load composition boundary conditions from two NetCDF files.
GeoDynamo.bcs.AbstractBoundaryCondition — Type
AbstractBoundaryCondition{T}Abstract base type for all boundary conditions.
GeoDynamo.bcs.BoundaryInterpolationCache — Type
BoundaryInterpolationCache{T}Typed storage for spectral boundary condition coefficients owned by a field. The string-key accessors keep legacy boundary helper code working, while hot solver paths can read the concrete matrix fields directly.
GeoDynamo.bcs.BoundaryLocation — Type
BoundaryLocationEnumeration for boundary locations.
GeoDynamo.bcs.BoundaryType — Type
BoundaryTypeEnumeration for boundary condition types.
GeoDynamo.bcs.FieldType — Type
FieldTypeEnumeration for different physical field types.
GeoDynamo.bcs._get_cached_bc_shtns_config — Method
_get_cached_bc_shtns_config(lmax, mmax, nlat, nlon)Get or create a cached SHTnsKit configuration for boundary transforms. Reuses configurations to avoid repeated setup/teardown overhead. Thread-safe implementation using a lock for the check-then-set pattern.
GeoDynamo.bcs._load_physical_format — Method
Load physical-space boundary data from NetCDF, transform to spectral via SHTnsKit.
GeoDynamo.bcs._load_spectral_format — Method
Load pre-computed spectral boundary coefficients from NetCDF (Fortran-compatible format).
GeoDynamo.bcs._parse_boundary_spec — Method
_parse_boundary_spec(spec::Tuple, cfg) -> (values::Matrix, bc_type::BoundaryType)Parse a boundary specification tuple into physical-space values and BC type.
Supported spec formats:
(:uniform, value)or(:dirichlet, value): Dirichlet BC with uniform value(:neumann, value): Neumann BC with uniform flux value
GeoDynamo.bcs.apply_gaussian_smoothing! — Method
apply_gaussian_smoothing!(data::AbstractMatrix, theta::Vector, phi::Vector, radius::Real)Apply Gaussian smoothing kernel to 2D data array.
GeoDynamo.bcs.bilinear_interpolate — Method
bilinear_interpolate(data::Matrix{T}, theta_idx::Tuple{Int, Int}, phi_idx::Tuple{Int, Int},
theta_weights::Tuple{T, T}, phi_weights::Tuple{T, T}) where TPerform bilinear interpolation on a 2D data array. Handles periodic boundary conditions in phi direction.
GeoDynamo.bcs.clear_bc_shtns_config_cache! — Method
clear_bc_shtns_config_cache!()Clear the cached SHTnsKit configurations for boundary transforms. Call this when grid parameters change or to free memory. Thread-safe implementation.
GeoDynamo.bcs.determine_field_type_from_name — Method
determine_field_type_from_name(field_name::String) -> FieldTypeDetermine field type from field name string.
GeoDynamo.bcs.find_grid_indices — Method
find_grid_indices(coords::Vector{T}, target::T; is_periodic::Bool=false) where TFind the two surrounding indices in a coordinate array for interpolation. Handles periodic coordinates (e.g., longitude) if is_periodic=true.
GeoDynamo.bcs.get_comm — Method
get_comm()Get MPI communicator, initializing MPI if needed. Always returns a valid communicator (never nothing).
GeoDynamo.bcs.get_current_simulation_time — Method
get_current_simulation_time(solver_state)Extract current simulation time from solver state.
GeoDynamo.bcs.get_default_boundary_type — Method
get_default_boundary_type(field_type::FieldType, location::BoundaryLocation) -> BoundaryTypeGet default boundary condition type for a field at a specific location.
GeoDynamo.bcs.get_default_units — Method
get_default_units(field_type::FieldType) -> StringGet default units for a field type.
GeoDynamo.bcs.get_interpolation_weights — Method
get_interpolation_weights(coords::Vector{T}, target::T, indices::Tuple{Int, Int};
is_periodic::Bool=false) where TCalculate interpolation weights for linear interpolation. Handles periodic wrapping when is_periodic=true and indices wrap around (e.g., (n, 1)).
GeoDynamo.bcs.get_nprocs — Method
get_nprocs()Get number of MPI processes (with fallback).
GeoDynamo.bcs.get_rank — Method
get_rank()Get MPI rank (with fallback).
GeoDynamo.bcs.shtns_physical_to_spectral — Method
shtns_physical_to_spectral(physical_data::Matrix{T}, config; return_complex::Bool=false) where TTransform physical boundary data to spectral coefficients using SHTnsKit. This is a common utility function used by both thermal and composition modules.
Arguments
physical_data: 2D array of physical values on (nlat, nlon) gridconfig: SHTnsKit configuration with lmax, mmax, nlm fieldsreturn_complex: If true, returns complex coefficients; otherwise real part only
Returns
Vector of spectral coefficients of length nlm.
Performance (v1.1.15)
Uses cached SHTnsKit configurations to avoid repeated setup overhead.
GeoDynamo.bcs.shtns_spectral_to_physical — Method
shtns_spectral_to_physical(coeffs::Vector, config, nlat::Int, nlon::Int)Transform spectral coefficients to physical boundary data using SHTnsKit. Inverse of shtnsphysicalto_spectral.
Arguments
coeffs: Vector of spectral coefficients of length nlmconfig: SHTnsKit configuration with lmax, mmax fieldsnlat, nlon: Output grid dimensions
Returns
Matrix of physical values on (nlat, nlon) grid.