Convenience Wrappers
High-level functions for common elliptic PDE inversions on C-grids.
Each wrapper dispatches to spectral, CG, or capacitance solvers
depending on the method argument.
Streamfunction from Vorticity
finitevolx.streamfunction_from_vorticity(zeta, dx, dy, bc='dst', lambda_=0.0, method='spectral', mask=None, capacitance_solver=None, preconditioner=None, known_values=None, known_mask=None)
Invert the vorticity–streamfunction relation ∇²ψ − λψ = ζ.
Solves the Poisson (λ = 0) or Helmholtz (λ ≠ 0) equation to recover the streamfunction from relative vorticity.
Three solver methods are available:
"spectral"— Direct spectral solver (DST/DCT/FFT) for rectangular domains. Selected by bc. Default. ψ lives at X points: the unknowns are sliced out of the full array so that the wall is the C-grid wall ("dst": DST-I on[1:-2, 1:-2], ψ = 0 on the wall nodes0andN-2;"dct": DCT-I on[0:-1, 0:-1];"fft":[1:-1, 1:-1]), the same wallmethod="cg"imposes with aMask2D. The other entries of the result are zero."cg"— Preconditioned Conjugate Gradient for masked / irregular domains. Requires mask. Uses a spectral preconditioner by default, or a custom one via preconditioner."capacitance"— Capacitance matrix method for masked domains. Requires a pre-built :class:CapacitanceSolvervia capacitance_solver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zeta
|
Float[Array, 'Ny Nx']
|
Relative vorticity (right-hand side). |
required |
dx
|
float
|
Grid spacings. |
required |
dy
|
float
|
Grid spacings. |
required |
bc
|
('dst', 'dct', 'fft')
|
Boundary condition on the wall / coast. |
"dst"
|
lambda_
|
float
|
Helmholtz parameter. Use 0.0 for the pure Poisson problem (streamfunction from vorticity). Non-zero values arise in QG PV inversion: (∇² − λ)ψ = q. |
0.0
|
method
|
('spectral', 'cg', 'capacitance')
|
Solver method. Default: |
"spectral"
|
mask
|
Float[Array, 'Ny Nx'] or Mask2D or None
|
Domain mask. Required for |
None
|
capacitance_solver
|
CapacitanceSolver or None
|
Pre-built capacitance solver. Required for
|
None
|
preconditioner
|
callable or None
|
Custom preconditioner for |
None
|
known_values
|
Float[Array, 'Ny Nx'] or None
|
Prescribed (inhomogeneous Dirichlet) values of the solution, used
on the coast ring -- the dry cells adjacent to a wet cell, where
|
None
|
known_mask
|
Bool[Array, 'Ny Nx'] or None
|
Extra interior wet cells with known values (e.g. sparse
observations), pinned alongside the boundary ring.
|
None
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'Ny Nx']
|
Streamfunction ψ. |
Source code in finitevolx/_src/solvers/elliptic.py
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 | |
Pressure from Divergence
finitevolx.pressure_from_divergence(div_u, dx, dy, bc='dct', method='spectral', mask=None, capacitance_solver=None, preconditioner=None, known_values=None, known_mask=None)
Solve ∇²p = ∇·u for the pressure correction.
Used in pressure-projection methods (Chorin splitting) where the divergence of the provisional velocity field must be removed.
p lives at T points, and every method puts the wall on the cell faces:
the spectral path solves on the T interior [1:-1, 1:-1]
("dct": DCT-II, ∂p/∂n = 0; "dst": DST-II, p = 0; "fft":
periodic) and returns a zero ghost ring; method="cg" solves on a
mask (a Mask2D gives its h mask) with the same face conditions
on every wet–dry face. method="capacitance" and known values pin
values at cell centres, not on faces, so they raise here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
div_u
|
Float[Array, 'Ny Nx']
|
Divergence of the velocity field (right-hand side). |
required |
dx
|
float
|
Grid spacings. |
required |
dy
|
float
|
Grid spacings. |
required |
bc
|
('dct', 'dst', 'fft')
|
Boundary condition on the wall / coast, honoured by every method
or rejected. |
"dct"
|
method
|
('spectral', 'cg')
|
Solver method. Default: |
"spectral"
|
mask
|
Float[Array, 'Ny Nx'] or Mask2D or None
|
Domain mask. Required for |
None
|
capacitance_solver
|
None
|
Not supported (kept for signature symmetry with the other wrappers). |
None
|
preconditioner
|
callable or None
|
Custom preconditioner for |
None
|
known_values
|
None
|
Not supported for pressure: a T-point Dirichlet value belongs on the cell face, while known values are pinned at cell centres (the ghost cells, half a cell outside the face), which would be a different, first-order wall. Passing either raises. |
None
|
known_mask
|
None
|
Not supported for pressure: a T-point Dirichlet value belongs on the cell face, while known values are pinned at cell centres (the ghost cells, half a cell outside the face), which would be a different, first-order wall. Passing either raises. |
None
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'Ny Nx']
|
Pressure field p. |
Source code in finitevolx/_src/solvers/elliptic.py
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 | |
PV Inversion
finitevolx.pv_inversion(pv, dx, dy, lambda_, bc='dst', method='spectral', mask=None, capacitance_solver=None, preconditioner=None, known_values=None, known_mask=None)
QG potential-vorticity inversion: solve (∇² − λ)ψ = q.
Supports batched / multi-layer PV fields. When lambda_ is a 1-D
array of shape (nl,), each layer is solved with its own Helmholtz
parameter (e.g. 1/Rd² per vertical mode from
:func:~finitevolx.decompose_vertical_modes).
Solver selection follows the same three-method dispatch as
:func:streamfunction_from_vorticity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pv
|
Float[Array, '... Ny Nx']
|
Potential-vorticity field. Leading dimensions are batched. |
required |
dx
|
float
|
Grid spacings. |
required |
dy
|
float
|
Grid spacings. |
required |
lambda_
|
float or Float[Array, ' nl']
|
Helmholtz parameter(s). Scalar for a single layer; array of
shape |
required |
bc
|
('dst', 'dct', 'fft')
|
Boundary condition on the wall / coast, as in
:func: |
"dst"
|
method
|
('spectral', 'cg', 'capacitance')
|
Solver method. Default: |
"spectral"
|
mask
|
Float[Array, 'Ny Nx'] or Mask2D or None
|
Domain mask. Required for |
None
|
capacitance_solver
|
CapacitanceSolver or None
|
Pre-built capacitance solver. Required for
|
None
|
preconditioner
|
callable or None
|
Custom preconditioner for |
None
|
known_values
|
Float[Array, '... Ny Nx'] or None
|
Prescribed Dirichlet values (see
:func: |
None
|
known_mask
|
Bool[Array, 'Ny Nx'] or None
|
Extra interior wet cells with known values, shared by all layers.
|
None
|
Returns:
| Type | Description |
|---|---|
Float[Array, '... Ny Nx']
|
Streamfunction ψ, same shape as pv. |
Source code in finitevolx/_src/solvers/elliptic.py
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 | |
Known Values (Inhomogeneous Dirichlet)
Building blocks behind the wrappers' known_values / known_mask
arguments, for use with your own solver. The known values sit on the coast
ring (the dry cells adjacent to the wet mask: the C-grid wall nodes for ψ).
finitevolx.coast_ring(mask)
Dry cells adjacent to at least one wet cell (the coast ring).
This is where :func:~finitevolx.masked_laplacian imposes its
homogeneous Dirichlet wall (a dry neighbour reads as zero), so it is
where prescribed (known) values sit. For ψ on a from_interior
rectangle (Mask2D.xy_corner_strict wet on [1:-2, 1:-2]) it is the
C-grid wall: X rows / columns 0 and N-2 (corners excluded).
Uses pad-then-slice (cells outside the array count as dry), so it never wraps around.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
Float[Array, 'Ny Nx'] or Bool[Array, 'Ny Nx']
|
Binary wet/dry mask (1 / True = wet, 0 / False = dry). |
required |
Returns:
| Type | Description |
|---|---|
Bool[Array, 'Ny Nx']
|
True at dry cells that neighbour at least one wet cell. |
Examples:
>>> import jax.numpy as jnp
>>> mask = jnp.zeros((5, 5), dtype=bool).at[1:-2, 1:-2].set(True)
>>> coast_ring(mask).astype(int)[:, :4]
Array([[0, 1, 1, 0],
[1, 0, 0, 1],
[1, 0, 0, 1],
[0, 1, 1, 0],
[0, 0, 0, 0]], dtype=int32)
Source code in finitevolx/_src/solvers/inhomogeneous.py
finitevolx.boundary_ring(mask)
Wet cells adjacent to at least one dry cell (the inner ring).
Uses pad-then-slice to avoid jnp.roll wrap-around artifacts.
JIT-compatible but intended for offline (one-shot) use — the mask
is typically fixed at setup time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
Float[Array, 'Ny Nx'] or Bool[Array, 'Ny Nx']
|
Binary wet/dry mask (1 / True = wet, 0 / False = dry). |
required |
Returns:
| Type | Description |
|---|---|
Bool[Array, 'Ny Nx']
|
True at wet cells that neighbor at least one dry cell. |
Source code in finitevolx/_src/solvers/inhomogeneous.py
finitevolx.SolveDomain
Bases: Module
Derived masks for the lifting trick.
The known values sit on the coast ring -- the dry cells adjacent to the
wet mask, where masked_laplacian / method="cg" put the
homogeneous wall -- plus any wet known_mask cells. The effective
solve domain is every other wet cell::
all_known = coast_ring | (known_mask & wet)
effective_mask = wet & ~known_mask
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
Float[Array, 'Ny Nx'] or Mask2D
|
Binary wet/dry mask (1 = wet, 0 = dry). A :class: |
required |
known_mask
|
Bool[Array, 'Ny Nx'] or None
|
Locations of additional known values (e.g., sparse interior observations). Merged with the coast ring. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
wet_mask |
Bool[Array, 'Ny Nx']
|
The wet cells. |
coast_ring |
Bool[Array, 'Ny Nx']
|
Dry cells adjacent to a wet cell, where the boundary values sit. |
boundary_ring |
Bool[Array, 'Ny Nx']
|
Wet cells adjacent to a dry cell (informational; they are unknowns). |
all_known |
Bool[Array, 'Ny Nx']
|
Cells whose value is prescribed. |
effective_mask |
Bool[Array, 'Ny Nx']
|
Wet cells that are solved for. |
Source code in finitevolx/_src/solvers/inhomogeneous.py
finitevolx.KnownValueLifting
Bases: Module
Pre/post-processing wrapper for the lifting trick.
Does not own or call a solver. The user builds their solver separately and calls it themselves. This operator only sandwiches the solve with pre- and post-processing.
The solver's unknowns must be exactly domain.effective_mask, with
zero on every other cell (masked_laplacian on
domain.effective_mask: a CG or multigrid operator built on it, or,
without a known_mask, a capacitance solver built on
domain.wet_mask).
The known values sit on the coast ring (the dry cells adjacent to the
wet mask) and on the wet known_mask cells.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain
|
SolveDomain
|
Precomputed domain masks. |
required |
dx
|
float
|
Grid spacing in x. |
required |
dy
|
float
|
Grid spacing in y. |
required |
lambda_
|
float
|
Helmholtz parameter. |
required |
Source code in finitevolx/_src/solvers/inhomogeneous.py
postprocess(psi_hom, value_lift)
Reconstruct full solution from homogeneous solve + lift.
Source code in finitevolx/_src/solvers/inhomogeneous.py
preprocess(rhs, known_values)
Correct RHS for known values.
Returns (rhs_corrected, value_lift). Feed rhs_corrected
to your solver, then pass both to :meth:postprocess.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rhs
|
Float[Array, 'Ny Nx']
|
Original right-hand side. |
required |
known_values
|
Float[Array, 'Ny Nx']
|
Prescribed values at known cells. Values at non-known cells are ignored. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
rhs_corrected |
Float[Array, 'Ny Nx']
|
Corrected RHS restricted to the effective solve domain. |
value_lift |
Float[Array, 'Ny Nx']
|
Lifting function — add to homogeneous solution to get the full solution. |