Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions build_sfincs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash
set -euo pipefail

# Build the BMI shared library in the same environment style as the documented
# working SFINCS build.

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SRC_DIR="$ROOT/extern/SFINCS/source/src"
BUILD_DIR="$ROOT/extern/SFINCS/cmake_build"

# Prefer the active conda/pixi env if present
if [[ -n "${CONDA_PREFIX:-}" ]]; then
export NETCDF_PREFIX="$CONDA_PREFIX"
export PKG_CONFIG_PATH="$CONDA_PREFIX/lib/pkgconfig:${PKG_CONFIG_PATH:-}"
fi

# Fall back to nc-config if available
if command -v nc-config >/dev/null 2>&1; then
NC_INC="$(nc-config --includedir)"
NC_LIB="$(nc-config --libdir)"
else
echo "ERROR: nc-config not found on PATH"
exit 1
fi

# Match the documented SFINCS build flags as closely as possible
export FCFLAGS="${FCFLAGS:-} -fopenmp -O3 -fallow-argument-mismatch -w"
export FFLAGS="${FFLAGS:-} -fopenmp -O3 -fallow-argument-mismatch -w"
export CFLAGS="${CFLAGS:-}"
export CXXFLAGS="${CXXFLAGS:-}"

# Clean rebuild while debugging env issues
rm -rf "$BUILD_DIR"

cmake -S "$SRC_DIR" -B "$BUILD_DIR" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_COMPILER="${CC:-gcc}" \
-DCMAKE_Fortran_COMPILER="${FC:-gfortran}" \
-DNETCDF_PREFIX="${NETCDF_PREFIX:-}" \
-DSFINCS_ENABLE_NETCDF=ON \
-DSFINCS_ENABLE_OPENMP=ON \
-DSFINCS_BUILD_TESTS=OFF \
-DCMAKE_Fortran_FLAGS_RELEASE="$FCFLAGS" \
-DCMAKE_EXE_LINKER_FLAGS="-L$NC_LIB -Wl,-rpath,$NC_LIB" \
-DCMAKE_SHARED_LINKER_FLAGS="-L$NC_LIB -Wl,-rpath,$NC_LIB" \
-DCMAKE_Fortran_FLAGS="-g -O0 -fcheck=all -fbacktrace -ffpe-trap=invalid,zero,overflow" \
-DCMAKE_C_FLAGS_RELEASE="${CFLAGS}" \
-DCMAKE_CXX_FLAGS_RELEASE="${CXXFLAGS}"

cmake --build "$BUILD_DIR" -j1

echo
echo "Built:"
echo " $BUILD_DIR/libsfincs_bmi.so"
echo
echo "Runtime linkage:"
ldd "$BUILD_DIR/libsfincs_bmi.so" || true
2 changes: 2 additions & 0 deletions build_test_sfincs.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmake -B cmake_build -S . \
-DNGEN_WITH_BMI_FORTRAN=ON \
-DNGEN_BUILD_COASTAL_TESTS=ON \
-DNGEN_WITH_TESTS=OFF \
-DCMAKE_BUILD_TYPE=Debug \
-DNGEN_ENABLE_SCHISM=OFF \
-DSFINCS_BMI_LIBRARY=/home/mohammed.karim/Calibration/ngen/extern/SFINCS/source/src/build/libsfincs_bmi.so \
-DSFINCS_INIT_CONFIG=/home/mohammed.karim/Calibration/ngen/extern/SFINCS/source/src/build/sfincs_config.txt
Expand Down
20 changes: 13 additions & 7 deletions src/realizations/coastal/SfincsFormulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ SfincsFormulation::~SfincsFormulation()
try { finalize(); } catch (...) {}
}

static std::string normalize_var(const std::string& v)
{
if (v == "BEDLEVEL" || v == "bedlevel" || v == "bed_level")
return "zb";
return v;
}

void SfincsFormulation::create_formulation_()
{
#if NGEN_WITH_BMI_FORTRAN
Expand All @@ -36,7 +43,7 @@ void SfincsFormulation::create_formulation_()
// - (type_name, library, init_config, has_fixed_dt, reg_func)
//
// We want to pass init_config_ so use the 5-arg overload.
const bool has_fixed_time_step = true;
const bool has_fixed_time_step = false;

// Default in adapter header is "register_bmi", but pass explicitly for clarity.
const std::string registration_function = "register_bmi";
Expand Down Expand Up @@ -98,24 +105,23 @@ void SfincsFormulation::update()
#endif
}


void SfincsFormulation::update_until(double const& t)
{
#if NGEN_WITH_BMI_FORTRAN
if (!bmi_) {
throw std::runtime_error("SfincsFormulation::update_until called before initialize()");
}

// Mirror Schism behavior
while (bmi_->GetCurrentTime() < t) {
// set_inputs_();
bmi_->Update();
}
bmi_->UpdateUntil(t);

#else
(void)t;
throw std::runtime_error("SfincsFormulation requires NGEN_WITH_BMI_FORTRAN=ON");
#endif
}


double SfincsFormulation::get_current_time()
{
#if NGEN_WITH_BMI_FORTRAN
Expand Down Expand Up @@ -154,7 +160,7 @@ double SfincsFormulation::get_time_step()

void SfincsFormulation::get_values(const selection_type& selector, boost::span<double> out)
{
const std::string& var = selector.variable_name;
const std::string var = normalize_var(selector.variable_name);

#if NGEN_WITH_BMI_FORTRAN
if (!bmi_) {
Expand Down