> ## Documentation Index
> Fetch the complete documentation index at: https://bindi-nagda-amr-multiphase.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Physical boundary conditions

> Three examples demonstrating how to apply, solve with, and time-integrate non-periodic boundary conditions.

The `physical_bc/` directory contains three focused examples that demonstrate different aspects of non-periodic (physical) boundary conditions in the multiphase solver. Each example is a standalone executable and is useful for verifying boundary condition implementations independently of the full time-stepping loop.

## Overview

| Subdirectory | CMake target | Purpose |
| - | - | - |
| `physical_bc/apply/` | `bc_apply` | Apply the Stokes operator to a prescribed velocity field and verify the operator output matches an analytical right-hand side |
| `physical_bc/solve/` | `bc_solve` | Solve the steady Stokes system with Dirichlet boundary conditions and check the solution against a known exact solution |
| `physical_bc/channel/` | `channel` | Run a full time-dependent simulation of pressure-driven channel flow with Poiseuille velocity boundary conditions |

## Building

From your build directory:

```bash theme={null}
make bc_apply
make bc_solve
make channel
```

***

## `apply` — operator application test

**Source:** `physical_bc/apply/apply.cpp`\
**Input:** `physical_bc/apply/input2d.const_theta.var_vel`

This example initializes a prescribed velocity field, applies the `MultiphaseStaggeredStokesOperator` directly (without solving), and compares the output to the analytically derived right-hand side. It is used to verify that the discrete operator correctly handles Dirichlet boundary data.

### Run

```bash theme={null}
cd physical_bc/apply
./bc_apply input2d.const_theta.var_vel
```

### What it demonstrates

* Registering `muParserRobinBcCoefs` for each velocity component and for `thn`
* Calling `stokes_op.setPhysicalBcCoefs(un_bc_coefs, us_bc_coefs, nullptr, thn_bc_coef)`
* Calling `stokes_op.apply(u_vec, f_vec)` and comparing to analytical forcing

### Input parameters

The test uses a constant volume fraction `thn = 0.75` with sinusoidal velocities on the unit square domain `[0,1]²` with no-slip Dirichlet conditions:

```text theme={null}
N     = 32*2
XI    = 1.0
ETAN  = 1.0
ETAS  = 1.0
NU    = 1.0
C     = 1.0
D     = -1.0
THN   = "0.75"

UN_0 = "sin(2*PI*X_0)*cos(2*PI*X_1)"
UN_1 = "cos(2*PI*X_0)*sin(2*PI*X_1)"
US_0 = "-sin(2*PI*X_0)*cos(2*PI*X_1)"
US_1 = "-cos(2*PI*X_0)*sin(2*PI*X_1)"
```

### Boundary condition format

Each velocity component uses `muParserRobinBcCoefs` blocks. The Robin coefficients `acoef = 1`, `bcoef = 0` specify pure Dirichlet conditions. `gcoef` holds the boundary value:

```text theme={null}
un_bc_0 {
   acoef_function_0 = "1.0"
   bcoef_function_0 = "0.0"
   gcoef_function_0 = UN_0   // x-component of un on left boundary
   ...
}
```

The suffix `_0` and `_1` on the block name correspond to the x- and y-components of velocity. The four `gcoef_function_N` entries correspond to the four sides of the 2D domain (left, right, bottom, top).

***

## `solve` — steady Stokes solver test

**Source:** `physical_bc/solve/solve.cpp`\
**Inputs:** `physical_bc/solve/input2d.homogeneous`, `physical_bc/solve/input2d.inhomogeneous`

This example solves a single steady Stokes problem with a known exact solution and computes L1, L2, and L∞ error norms. It exercises the full solve path including the `FullFACPreconditioner` multigrid preconditioner on a non-periodic domain.

### Run

> [tab] Homogeneous BCs
>
> ```bash theme={null}
> cd physical_bc/solve
> ./bc_solve input2d.homogeneous
> ```
>
> The homogeneous test uses sinusoidal velocities that vanish on all boundaries:
>
> ```text theme={null}
> UN_0 = "sin(2*PI*X_0)*cos(2*PI*X_1)"
> UN_1 = "cos(2*PI*X_0)*sin(2*PI*X_1)"
> US_0 = "-sin(2*PI*X_0)*cos(2*PI*X_1)"
> US_1 = "-cos(2*PI*X_0)*sin(2*PI*X_1)"
> THN  = "0.25*sin(2*PI*X_0)*sin(2*PI*X_1) + 0.5"
> ```

> [tab] Inhomogeneous BCs
>
> ```bash theme={null}
> cd physical_bc/solve
> ./bc_solve input2d.inhomogeneous
> ```
>
> The inhomogeneous test uses cosine-based velocities that have non-zero values at the domain boundary:
>
> ```text theme={null}
> UN_0 = "cos(2*PI*X_0)*cos(2*PI*X_1)"
> UN_1 = "cos(2*PI*X_0)*sin(2*PI*X_1)"
> US_0 = "-cos(2*PI*X_0)*cos(2*PI*X_1)"
> US_1 = "-cos(2*PI*X_0)*sin(2*PI*X_1)"
> THN  = "0.25*sin(2*PI*X_0)*sin(2*PI*X_1) + 0.5"
> ```

### What it demonstrates

* Building and solving the full `MultiphaseStaggeredStokesOperator` system with physical boundaries
* Using `FullFACPreconditioner` with `MultiphaseStaggeredStokesBoxRelaxationFACOperator` as the FAC strategy
* Setting nullspace vectors for the pressure (and optionally velocity) when the system has a constant-pressure nullspace
* Printing per-variable error norms from `solve.cpp`:

```text theme={null}
Error in u_n :
  L1-norm:  ...
  L2-norm:  ...
  max-norm: ...

Error in u_s :
  L1-norm:  ...
  L2-norm:  ...
  max-norm: ...

Error in p :
  L1-norm:  ...
  L2-norm:  ...
  max-norm: ...
```

### Grid configuration

Both input files use a 64×64 coarse grid (`N = 32*2`) on `[0,1]²` with an L-shaped AMR refinement patch at level 1:

```text theme={null}
N = 32*2

GriddingAlgorithm {
   max_levels = 2
   ratio_to_coarser { level_1 = 4, 4 }
   ...
}

StandardTagAndInitialize {
   tagging_method = "REFINE_BOXES"
   RefineBoxes {
      level_0 = [( N/4,N/4 ),( 3*N/4 - 1,N/2 - 1 )], \
                [( N/4,N/2 ),( N/2 - 1,  3*N/4 - 1 )]
   }
}
```

### Preconditioner settings

```text theme={null}
KrylovPrecond {
   cycle_type         = "V_CYCLE"
   num_pre_sweeps     = 5
   num_post_sweeps    = 5
   enable_logging     = TRUE
   max_multigrid_levels = MAX_MULTIGRID_LEVELS   // = 5
}
```

> [!TIP]
>
> Set `USE_PRECOND = FALSE` in the input file to run without the multigrid preconditioner. This is useful for isolating solver convergence behavior from preconditioner quality.

***

## `channel` — pressure-driven channel flow

**Source:** `physical_bc/channel/channel.cpp`\
**Input:** `physical_bc/channel/input2d`

This is a full unsteady simulation of Poiseuille (pressure-driven channel) flow on `[0,1]²` with Dirichlet velocity boundary conditions. Both phases are assigned the Poiseuille profile `u = X_1 * (1 - X_1)` on the inflow and outflow faces. The simulation runs to `END_TIME = 1.0` and writes a visualization snapshot every `VIZ_DUMP_TIME_INTERVAL = 0.05` time units.

### Run

```bash theme={null}
cd physical_bc/channel
./channel input2d \
    -solver_ksp_rtol 1e-14 \
    -solver_ksp_type fgmres
```

Visualization output is written to `viz2d/`.

### What it demonstrates

* Registering Robin BC coefficient objects for both `un` and `us` via `ins_integrator->registerPhysicalBoundaryConditions`
* Setting a Dirichlet BC for the advected `thn` variable via `adv_diff_integrator->setPhysicalBcCoef`
* A non-periodic domain (`periodic_dimension = 0, 0`) compared to the four-roll mill example
* Side-by-side output of numerical and exact solutions for visual error inspection

### Exact solution

Both network and solvent velocities converge to the parabolic Poiseuille profile:

```text theme={null}
un_exact { function_0 = "X_1 * (1.0 - X_1)"  function_1 = "0.0" }
us_exact { function_0 = "X_1 * (1.0 - X_1)"  function_1 = "0.0" }
p        { function   = "1.0 - 2.0*X_0" }
thn      { function   = "0.5" }
```

The pressure gradient `dp/dx = -2` drives the flow.

### Boundary conditions

Dirichlet conditions are imposed on all four boundaries. The x-velocity component (`_0`) has Poiseuille inflow/outflow values; the y-velocity component (`_1`) and the transverse sides are set to zero:

```text theme={null}
un_bcs_0 {
   acoef_function_0 = "1.0"   bcoef_function_0 = "0.0"
   gcoef_function_0 = "X_1 * (1.0 - X_1)"   // left face
   gcoef_function_1 = "X_1 * (1.0 - X_1)"   // right face
   gcoef_function_2 = "0.0"                  // bottom face
   gcoef_function_3 = "0.0"                  // top face
   ...
}
```

`thn` is held at 0.5 on all boundaries:

```text theme={null}
thn_bc {
   acoef_function_0 = "1.0"
   bcoef_function_0 = "0.0"
   gcoef_function_0 = "0.5"
   ...
}
```

### Grid and solver settings

```text theme={null}
N       = 64
ETA_S   = 1.0
ETA_N   = 1.0
NU      = 1.0
XI      = 250.0*ETA_S
CFL     = 0.1
DT_MAX  = CFL * DX / U_MAX   // U_MAX = 1.0
END_TIME = 1.0
```

The grid uses a single coarse level (`max_levels = 1`) with static L-shaped refinement boxes defined via `REFINE_BOXES`. A multigrid preconditioner with `MAX_MULTIGRID_LEVELS = 3` is used.

> [!NOTE]
>
> The channel example does not use gradient-based AMR tagging. Instead it uses statically defined `REFINE_BOXES` in `StandardTagAndInitialize`. This lets you test physical boundary conditions on an AMR hierarchy without tying the refinement pattern to the solution.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.