Skip to content

Feature: MMG mesh initialization#57

Open
chaseshyu wants to merge 2 commits intomasterfrom
feature/MMG-mesh-initialization
Open

Feature: MMG mesh initialization#57
chaseshyu wants to merge 2 commits intomasterfrom
feature/MMG-mesh-initialization

Conversation

@chaseshyu
Copy link
Copy Markdown
Member

This pull request introduces several improvements and bug fixes related to mesh initialization, remeshing, and checkpointing, particularly for builds using MMG remeshing (guarded by USEMMG). The changes enhance the handling of the init_elem_size_n variable, ensure correct memory allocation and checkpointing, and add a new configuration parameter for mesh coarsening. Below are the most important changes grouped by theme:

MMG Remeshing and Element Size Handling:

  • Added conditional allocation, initialization, and interpolation of var.init_elem_size_n only when USEMMG is enabled, preventing unnecessary memory usage and operations in non-MMG builds. (brc-interpolation.cxx, dynearthsol.cxx, fields.cxx) [1] [2] [3] [4]
  • Improved initialization logic for init_elem_size_n to avoid redundant work if already initialized, and ensured correct resizing and zero-filling during restarts and remeshing. (remeshing.cxx, dynearthsol.cxx) [1] [2] [3]
  • Added checkpoint read/write support for init_elem_size_n under USEMMG, ensuring this field is correctly persisted and restored. (output.cxx, dynearthsol.cxx) [1] [2]

Mesh Configuration and API Changes:

  • Introduced a new mesh parameter, mmg_init_coarsening_factor, to control the coarsening factor for the initial TetGen+MMG mesh generation, and documented its effect on mesh quality and performance. (input.cxx, parameters.hpp) [1] [2]
  • Updated the renumbering_mesh function signature to accept an optional init_elem_size_hint parameter, improving flexibility for mesh manipulation routines. (mesh.hpp)

Code Maintenance and Bug Fixes:

  • Fixed a minor bug in the call to renumbering_mesh by replacing NULL with nullptr for better type safety in C++. (remeshing.cxx)
  • Cleaned up preprocessor guards and function placement related to MMG and 3D mesh optimization routines, improving code clarity. (remeshing.cxx) [1] [2]

These changes collectively improve the robustness and maintainability of the mesh handling code, especially for advanced remeshing workflows using MMG.

Close #47

@chaseshyu chaseshyu requested review from Copilot and echoi April 15, 2026 15:13
@chaseshyu chaseshyu added enhancement New feature or request bugfixes Fix bugs labels Apr 15, 2026
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 enhances mesh initialization and remeshing workflows for builds using MMG (USEMMG), including persisting the initial element-size hint through remeshing and checkpoints, and adding a new configuration knob to coarsen the initial TetGen mesh before MMG refinement.

Changes:

  • Add two-stage TetGen→MMG initialization in mesh.cxx, including propagation of region/material attributes and an optional per-node initial metric hint.
  • Gate allocation/interpolation/checkpointing of init_elem_size_n behind USEMMG, and improve its initialization behavior.
  • Introduce mesh.mmg_init_coarsening_factor to control coarse TetGen sizing for faster initialization.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
remeshing.cxx Adjust MMG-related guards; refine init_elem_size_n initialization; update nullptr usage.
parameters.hpp Add mmg_init_coarsening_factor to mesh parameters.
input.cxx Declare/configure mesh.mmg_init_coarsening_factor with a default.
output.cxx Write init_elem_size_n to checkpoint only under USEMMG.
mesh.hpp Extend renumbering_mesh signature to accept an optional init-size hint.
mesh.cxx Implement TetGen+MMG two-stage initialization and carry init_elem_size_n through renumbering.
fields.cxx Stop unconditional allocation of init_elem_size_n in variable allocation.
dynearthsol.cxx Allocate/initialize and checkpoint-restore init_elem_size_n (MMG-gated init).
brc-interpolation.cxx Interpolate init_elem_size_n across remeshing only when USEMMG is enabled.

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

Comment thread dynearthsol.cxx

bin_chkpt.read_array(*var.surfinfo.edvacc_surf, "dv surface acc");
#ifdef USEMMG
bin_chkpt.read_array(*var.init_elem_size_n, "init_elem_size_n");
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

Restart will currently fail (BinaryInput/HDF5Input exits) if the checkpoint was produced by an older build that doesn't contain the new "init_elem_size_n" dataset. Use bin_chkpt.has_array("init_elem_size_n") before reading, and provide a fallback when missing (e.g., initialize_elem_size_n(...) under USEMMG after volumes are available, or initialize to a safe default).

Suggested change
bin_chkpt.read_array(*var.init_elem_size_n, "init_elem_size_n");
if (bin_chkpt.has_array("init_elem_size_n"))
bin_chkpt.read_array(*var.init_elem_size_n, "init_elem_size_n");
else {
for (std::size_t i = 0; i < var.init_elem_size_n->size(); ++i)
(*var.init_elem_size_n)[i] = 0;
}

Copilot uses AI. Check for mistakes.
Comment thread mesh.cxx
Comment on lines +890 to +891
double_vec cra(pregattr ? pregattr : nullptr,
pregattr ? pregattr + ce : nullptr);
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

This constructs cra using a pointer-range vector constructor with nullptr when pregattr is null. Passing nullptr iterators is undefined behavior for std::vector's range constructor. Prefer an explicit conditional: leave cra empty when pregattr==nullptr, otherwise copy [pregattr, pregattr+ce).

Suggested change
double_vec cra(pregattr ? pregattr : nullptr,
pregattr ? pregattr + ce : nullptr);
double_vec cra;
if (pregattr)
cra.assign(pregattr, pregattr + ce);

Copilot uses AI. Check for mistakes.
Comment thread mesh.cxx
Comment on lines +1000 to +1004
n_regions, regattr, max_elem_size, mesh.resolution, *fine_init_metric_n);
} else {
// Uniform case: exact constant metric
const double target = std::pow(max_elem_size / sizefactor, 1.0 / NDIMS);
fine_init_metric_n->assign(nnode, target);
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

fine_init_metric_n is documented as optional, but it's dereferenced unconditionally here. If this function is ever called with fine_init_metric_n==nullptr (e.g., a caller only wants the refined mesh), this will crash. Add a nullptr guard (similar to the 2D implementation) or make the parameter non-optional.

Copilot uses AI. Check for mistakes.
Comment thread dynearthsol.cxx
Comment on lines 307 to +308
allocate_variables(param, var);
var.init_elem_size_n->resize(var.nnode);
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

In restart(), init_elem_size_n is resized unconditionally. In non-MMG builds this can allocate a large per-node array that is never used (and contradicts the goal of avoiding init_elem_size_n allocation when USEMMG is off). Guard this resize behind USEMMG (and only resize when you will actually read/populate the array).

Suggested change
allocate_variables(param, var);
var.init_elem_size_n->resize(var.nnode);
allocate_variables(param, var);
#ifdef USEMMG
var.init_elem_size_n->resize(var.nnode);
#endif

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfixes Fix bugs enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slow and crashing when using tetgen for mesh initialization

2 participants