Skip to content

Conservative field transfer (integrator + Monte-Carlo projection)#335

Open
jacobmerson wants to merge 12 commits into
jacobmerson/various-fixesfrom
jacobmerson/conservative-transfer
Open

Conservative field transfer (integrator + Monte-Carlo projection)#335
jacobmerson wants to merge 12 commits into
jacobmerson/various-fixesfrom
jacobmerson/conservative-transfer

Conversation

@jacobmerson

@jacobmerson jacobmerson commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

Adds conservative field-transfer support built on the integrator-based (Galerkin) and Monte-Carlo / control-variate projection operators, replacing the old calculate_mass_matrix / load_vector implementation.

Highlights:

  • Integrator-based conservative (Galerkin) projection transfer
  • Monte-Carlo / control-variate conservative projection
  • DOF holders indexed by a compact GID permutation; coupler separates GID-layout and field messages
  • Python bindings expose conservative transfer

Base is jacobmerson/various-fixes (#340 ).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors conservative field transfer to use reusable integrator abstractions (mass + RHS) and a cached Galerkin projection solver, adds Monte Carlo (control-variate) conservative projection, and updates coupler/GID handling and Python bindings accordingly.

Changes:

  • Replace legacy calculate_mass_matrix / calculate_load_vector and load-vector integrator paths with BilinearFormIntegrator/LinearFormIntegrator + GalerkinProjectionSolver.
  • Add Omega_h-specific conservative RHS integrators (mesh-intersection quadrature) and Monte Carlo RHS integrator, plus a control-variate projection operator.
  • Introduce compact GID permutations and adjust field exchange planning/serialization; expose new transfer operators via Python and add new unit tests + example.

Reviewed changes

Copilot reviewed 53 out of 53 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
test/test_omega_h_mc_rhs_integrator.cpp Adds tests for Monte Carlo RHS integrator and control-variate projection behavior.
test/test_omega_h_mass_integrator.cpp Adds tests validating Omega_h mass-matrix assembly vs MeshField reference.
test/test_omega_h_intersection_rhs_integrator.cpp Adds tests for intersection-based conservative RHS integrator correctness.
test/test_omega_h_form_integrator_utils.cpp Adds tests for polygon vertex dedup/relink helper used in intersection integration.
test/test_mesh_intersection_field_transfer.cpp Reworks/extends conservative projection tests (P0/P1, reordered/sparse GIDs, conservation).
test/test_load_vector.cpp Removes legacy load vector test tied to deleted implementation.
test/test_field_exchange_planner.cpp Adds tests ensuring exchange plans exclude GID headers and headers are inserted correctly.
test/CMakeLists.txt Registers new tests and updates conditional test lists.
src/pcms/transfer/omega_h_mc_rhs_integrator.hpp Defines Monte Carlo RHS integrator interface and sampling enum.
src/pcms/transfer/omega_h_mc_rhs_integrator.cpp Implements Monte Carlo sample generation and COO RHS assembly.
src/pcms/transfer/omega_h_mass_integrator.hpp Defines Omega_h mass-matrix integrator (P0/P1) and builder.
src/pcms/transfer/omega_h_mass_integrator.cpp Implements PETSc COO mass-matrix assembly for Omega_h layouts.
src/pcms/transfer/omega_h_intersection_rhs_integrator.hpp Defines intersection-based conservative RHS integrator and builder.
src/pcms/transfer/omega_h_intersection_rhs_integrator.cpp Implements mesh-intersection quadrature point generation and RHS assembly.
src/pcms/transfer/omega_h_form_integrator_utils.hpp Adds shared validation, intersection traversal, quadrature staging, and target basis helpers.
src/pcms/transfer/omega_h_control_variate_projection.hpp Declares control-variate Monte Carlo conservative projection operator.
src/pcms/transfer/omega_h_control_variate_projection.cpp Implements control-variate projection workflow (interpolate + residual projection).
src/pcms/transfer/omega_h_conservative_projection.hpp Updates conservative projection to cache integrators/evaluator/solver (no per-call refactorization).
src/pcms/transfer/omega_h_conservative_projection.cpp Implements cached projection Apply() using solver + permutation scatter.
src/pcms/transfer/mass_matrix_integrator.hpp Renames helper to buildElementMassMatrix and fixes override signature.
src/pcms/transfer/linear_form_integrator.hpp Introduces LinearFormIntegrator interface for RHS assembly.
src/pcms/transfer/bilinear_form_integrator.hpp Introduces BilinearFormIntegrator interface for matrix assembly.
src/pcms/transfer/integration_point_set.hpp Adds IntegrationPointSet wrapper for ordered integration coordinates.
src/pcms/transfer/conservative_projection_solver.hpp Refactors solver into cached GalerkinProjectionSolver using integrator interfaces.
src/pcms/transfer/conservative_projection_solver.cpp Implements cached KSP setup and Solve overloads (evaluator-driven and pre-sampled).
src/pcms/transfer/load_vector_integrator.hpp Removes legacy supermesh load-vector integrator API.
src/pcms/transfer/load_vector_integrator.cpp Removes legacy load-vector integrator implementation.
src/pcms/transfer/calculate_mass_matrix.hpp Removes legacy PETSc mass matrix assembly API.
src/pcms/transfer/calculate_mass_matrix.cpp Removes legacy mass matrix assembly implementation.
src/pcms/transfer/calculate_load_vector.hpp Removes legacy PETSc load vector assembly API.
src/pcms/transfer/calculate_load_vector.cpp Removes legacy load vector assembly implementation.
src/pcms/transfer/interpolator.h Fixes include path for transfer operator header.
src/pcms/transfer/CMakeLists.txt Wires in new transfer headers/sources and removes deprecated ones.
src/pcms/pythonapi/pythonapi.cpp Initializes PETSc at import time when PETSc+MeshFields are enabled.
src/pcms/pythonapi/CMakeLists.txt Links PETSc/MeshFields into Python module when needed for new transfer bindings.
src/pcms/pythonapi/bind_transfer_field.cpp Exposes conservative and Monte Carlo/control-variate projections + sampling enum to Python.
src/pcms/pythonapi/bind_omega_h.cpp Adds VTU read helper and fixes mesh construction in parallel VTK read.
src/pcms/pythonapi/bind_field_base.cpp Improves numpy contiguity handling; exposes FunctionSpace::mesh and Lagrange backend selection.
src/pcms/localization/adj_search.cpp Adjusts handling of exterior points and caps adaptive radius loop.
src/pcms/field/layout/omega_h_lagrange.h Adds device GID accessor.
src/pcms/field/layout/omega_h_lagrange.cpp Builds global-to-local permutation eagerly in constructors; implements GetGids().
src/pcms/field/gid_permutation.hpp Adds utilities for unmapped/sorted GID permutations and permutation construction.
src/pcms/field/gid_permutation.cpp Implements permutation utilities and uniqueness checks.
src/pcms/field/field_layout.h Adds global-to-local permutation API and eager-build helper/caches.
src/pcms/field/field_layout.cpp Implements permutation cache building and accessors.
src/pcms/field/element_dispatch.h Adds runtime-to-compile-time FE order dispatch helper.
src/pcms/field/CMakeLists.txt Adds new field sources/headers to build.
src/pcms/coupler/serializer/xgc.h Skips unmapped permutation entries during (de)serialization.
src/pcms/coupler/field_serializer.h Skips unmapped permutation entries during (de)serialization and sorted extraction.
src/pcms/coupler/field_layout_communicator.cpp Adjusts outgoing GID message layout/size to account for per-message headers.
src/pcms/coupler/field_exchange_planner.h Clarifies exchange plan offsets/permutation exclude GID headers.
src/pcms/coupler/field_exchange_planner.cpp Refactors GID message handling to insert/parse headers and support unmapped holders.
examples/python-api-example/example_conservative_transfer.py Adds end-to-end Python example for conservative + Monte Carlo transfer and mixed-order cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/pcms/pythonapi/bind_omega_h.cpp Outdated
Comment on lines +630 to +638
m.def(
"read_mesh_vtu",
[](const std::string& filepath, std::shared_ptr<Omega_h::Comm> comm) {
std::ifstream file(filepath, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Cannot open VTU file: " + filepath);
}
Omega_h::Mesh mesh(comm->library());
Omega_h::vtk::read_vtu(file, comm, &mesh);
num_samples * 3);
d.coeffs =
Kokkos::View<Real*, DeviceMemorySpace>("mc_rhs_coeffs", num_samples * 3);
d.nverts = static_cast<PetscInt>(mesh.nverts());
coo_cols(e) = g;
vals(e) = static_cast<PetscScalar>(area);
});

}
}
});

static_cast<PetscScalar>(coeffs(i * ndof + j)) * f;
}
});

coo_vals(i * 3 + j) = static_cast<PetscScalar>(coeffs(i * 3 + j)) * f;
}
});

{
detail::CheckOmegaHScalarP1Layout(target_coordinate_system, target_layout,
"OmegaHMonteCarloRHSIntegrator", "target");
if (samples_per_element <= 0) {
…trix / load_vector)

Squashed original commits: 0a27b53, 0c0e697
…nsfer

Squashed original commits: bad8434, eca15d1, b4af186, 26f35c6, b8baf3b, 6b1d7f2, dba7966, 5baf365, 4b00ac1
(mesh_fields_rhs_integrator churn from 26f35c6/b8baf3b dropped)
Squashed original commits: 844dc1c, b4a4545, 629419f (sobol_sequence churn dropped)
…y buffers

Squashed original commits: e7488c6, 4e68c80
Currently the petsc-based solver is only needed for conservative
projection which requires MeshFields. So petsc is only linked when
PCMS_ENABLE_PETSC and PCMS_ENABLE_MESHFIELDS
Add a shared, format-agnostic GID-to-local permutation and use it so the
transfer operators handle sparse or reordered global IDs correctly.

This allows us to reuse the global-to-local mapping for things outside
of the serialization such as setting up the petsc arrays.
…olders

Split the GID (layout) message from the field (data) message and reuse the
field GID-permutation primitives so exchanges handle overlap masks and
reordered global IDs without corrupting payloads.
@jacobmerson jacobmerson force-pushed the jacobmerson/conservative-transfer branch from 7dfbf1e to 5bda307 Compare July 12, 2026 18:29
@jacobmerson jacobmerson force-pushed the jacobmerson/various-fixes branch from b4d81b3 to 6c13b7c Compare July 12, 2026 18:29
The python pcms module links libpetsc.so dynamically when python_api is
ON, so PETSc lib dir must be on LD_LIBRARY_PATH for import to succeed.
clang-tidy bugprone-implicit-widening-of-multiplication-result (treated
as error) flagged int*int products used to size 64-bit Views in the
intersection/MC RHS integrators and the field-exchange-planner test.
Perform the multiplication in size_t via an explicit cast.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants