flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
flow_bindings_common.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <nanobind/nanobind.h>
10#include <nanobind/ndarray.h>
11#include <nanobind/stl/array.h>
12#include <nanobind/stl/optional.h>
13#include <nanobind/stl/pair.h>
14#include <nanobind/stl/string.h>
15#include <nanobind/stl/tuple.h>
16#include <nanobind/stl/vector.h>
17
18#include <array>
19#include <cstdint>
20#include <Kokkos_Core.hpp>
21#include <limits>
22#include <string>
23#include <vector>
24
25#ifdef PECLET_FLOW_MPI
26#include <mpi.h>
27
28#include "peclet/core/common/types.hpp"
29#include "peclet/core/decomp/block_decomposer.hpp"
30#endif
31
34#include "flow_ibm.hpp"
35#include "peclet/core/python/kokkos_teardown.hpp"
36#include "peclet/core/python/ndarray_interop.hpp"
37
39
40namespace nb = nanobind;
41// Input validation and the checked setters bound in place of a member (flow_bindings_checks.hpp).
42namespace chk = peclet::flow::binding_checks;
43
44#ifdef PECLET_FLOW_MPI
45// Ensure MPI_Init has been called (mirrors the dem init_mpi idiom); safe to call repeatedly.
46inline void ensure_mpi_init() {
47 int inited = 0;
48 MPI_Initialized(&inited);
49 if (!inited) {
50 int argc = 0;
51 char** argv = nullptr;
52 MPI_Init(&argc, &argv);
53 }
54}
55#endif
56
57// A solver field (flat x-fastest, ghost-stripped) -> Fortran-order (nx,ny,nz) float64 NumPy array
58// for [x,y,z] indexing. The vector is moved into the array's backing store (no extra copy vs the
59// old to_xyz).
60template <class S>
61inline nb::ndarray<nb::numpy, double> field_out(S& s, std::vector<double>&& v) {
62 const auto nx = static_cast<std::size_t>(s.nx());
63 const auto ny = static_cast<std::size_t>(s.ny());
64 const auto nz = static_cast<std::size_t>(s.nz());
65 return peclet::core::python::vector_to_ndarray(
66 std::move(v), {nx, ny, nz},
67 {1, static_cast<std::int64_t>(nx), static_cast<std::int64_t>(nx * ny)});
68}
69
70// A Fortran-order (nx,ny,nz) float64 array -> flat x-fastest host vector (F-contiguous data() is
71// already x-fastest). nanobind casts/copies the input to f_contig double if needed.
72inline std::vector<double> grid_in(nb::ndarray<double, nb::f_contig> a) {
73 return peclet::core::python::ndarray_to_vector<double>(nb::ndarray<>(a));
74}
75
76// String enums of the public API (suite/docs/NAMING.md; QUALITY_PLAN F): every integer mode code
77// became a name, and a name outside the accepted set raises listing that set.
78inline int enum_index(const std::string& v, const std::vector<std::string>& names, const char* who,
79 const char* what) {
80 for (std::size_t i = 0; i < names.size(); ++i)
81 if (v == names[i])
82 return static_cast<int>(i);
83 std::string all;
84 for (std::size_t i = 0; i < names.size(); ++i)
85 all += (i ? " | '" : "'") + names[i] + "'";
86 throw std::invalid_argument(std::string(who) + ": " + what + " must be one of " + all +
87 ", got '" + v + "'");
88}
89inline const std::vector<std::string> kFaces{"-x", "+x", "-y", "+y", "-z", "+z"};
90inline int face_index(const std::string& f, const char* who) {
91 return enum_index(f, kFaces, who, "face");
92}
93inline const std::vector<std::string> kAxes{"x", "y", "z"};
94inline const std::vector<std::string> kBcTypes{"periodic", "wall", "inflow", "outflow", "slip"};
95inline const std::vector<std::string> kScalarBcTypes{"periodic", "neumann", "dirichlet"};
96inline const std::vector<std::string> kAdvSchemes{"sou", "koren"};
97inline const std::vector<std::string> kScalarSchemes{"fou", "koren", "sou"};
98inline const std::vector<std::string> kScalarWallTypes{"neumann", "dirichlet", "robin"};
99inline const std::vector<std::string> kFluidOnly{"off", "filter", "star"};
100inline const std::vector<std::string> kCsfModes{"face", "cell"};
101inline const std::vector<std::string> kPivots{"volume", "centroid", "projected-centroid",
102 "contact-line"};
103inline const std::vector<std::string> kAreaModes{"plic",
104 "cascade-metric",
105 "cascade-normal",
106 "cascade-footprint",
107 "sheet-color-centroid",
108 "sheet-color-split",
109 "sheet-plic-centroid",
110 "sheet-plic-split"};
111inline const std::vector<std::string> kBlockAssign{"round-robin", "lpt", "orb"};
112
113// Zero-copy export of a registered field's padded device buffer as a Fortran-order 3-D array of the
114// full block shape (ex,ey,ez) = (nx+2G, ny+2G, nz+2G), x-fastest strides {1,ex,ex*ey}. Includes the
115// ghost band (the flat buffer is contiguous; a ghost-stripped view would not be). The shared bridge
116// makes the capsule a Releasable holding the managed CCField, so the allocation outlives the array
117// (host → NumPy referencing the buffer, device → DLPack for CuPy/torch) and is dropped by the
118// module's shutdown before Kokkos::finalize.
119template <class S>
120inline auto field3d_out(S& s, peclet::flow::CCField f) {
121 const auto bs = s.blockShape();
122 const std::array<std::size_t, 3> shape{static_cast<std::size_t>(bs[0]),
123 static_cast<std::size_t>(bs[1]),
124 static_cast<std::size_t>(bs[2])};
125 const std::array<std::int64_t, 3> strides{1, static_cast<std::int64_t>(bs[0]),
126 static_cast<std::int64_t>(bs[0]) * bs[1]};
127 return peclet::core::python::view_to_ndarray(f, shape, strides);
128}
129
130// The bound solver: Solver<Grid> plus the suite's teardown registration. Solver<Grid> owns its
131// Kokkos Views all over (fields, MG hierarchy, IBM overlays, ...) with no piecemeal reset, so
132// release() destroys the bound instance outright (kokkos_teardown.hpp: nanobind marks the Python
133// object uninitialized; later calls raise, dealloc skips the destructor). Only reached for a Solver
134// still alive when the module shuts down.
135template <class Grid>
136struct BoundSolver final : peclet::flow::Solver<Grid>, peclet::core::python::Releasable {
138 void release() noexcept override { peclet::core::python::destruct_bound_instance(this); }
139};
140
141// The bound Anderson accelerator (doc/steady_acceleration.md §5.2): it owns Kokkos Views (the
142// history), so it is a Releasable like the solver and is destroyed by the module's atexit hook
143// before Kokkos::finalize if it is still alive then.
144template <class Grid>
146 peclet::core::python::Releasable {
148 void release() noexcept override { peclet::core::python::destruct_bound_instance(this); }
149};
150
154template <class Grid>
156 std::vector<double> v = s.hydroForceTorque();
157 const std::size_t n = v.size() / 12;
158 return peclet::core::python::vector_to_ndarray(std::move(v), {4, n, 3},
159 {static_cast<std::int64_t>(3 * n), 3, 1});
160}
161
164template <class Grid>
168
169} // namespace peclet::flow::bindings
flow — the Anderson accelerator of a steady march: core's AndersonCore wrapped around one Solver<Grid...
AndersonAccelerator(Solver< Grid > &solver, int window=5, double mixing=1.0)
Builds the march state (throws std::runtime_error on a refused configuration, §5.3) and allocates the...
Solver(int nx, int ny, int nz)
Input validation of the peclet.flow Python binding (src/flow_bindings.cpp only).
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
const std::vector< std::string > kAreaModes
const std::vector< std::string > kScalarSchemes
int face_index(const std::string &f, const char *who)
int enum_index(const std::string &v, const std::vector< std::string > &names, const char *who, const char *what)
const std::vector< std::string > kBlockAssign
const std::vector< std::string > kScalarWallTypes
const std::vector< std::string > kBcTypes
const std::vector< std::string > kFaces
auto field3d_out(S &s, peclet::flow::CCField f)
const std::vector< std::string > kFluidOnly
const std::vector< std::string > kAdvSchemes
std::vector< double > grid_in(nb::ndarray< double, nb::f_contig > a)
nb::ndarray< nb::numpy, double > field_out(S &s, std::vector< double > &&v)
const std::vector< std::string > kScalarBcTypes
const std::vector< std::string > kCsfModes
const std::vector< std::string > kAxes
const std::vector< std::string > kPivots
auto traction_force_torque_array(peclet::flow::Solver< Grid > &s)
The reconstructed-traction loads as the (4, n_instances, 3) array that both of its Python names retur...
Kokkos::View< double *, CCMem > CCField
The developer tier: a view onto one Solver<Grid> (no state of its own).