module; #include module mean_field; import :mapping.coefficients; namespace mean_field::analysis { double domain_integrate_grid_function(const fem::FEM &fem, const mfem::GridFunction &gf, utils::DOMAINS domain, mapping::COORDINATE_SPACE coord_space) { mfem::LinearForm lf(fem.H1_fes.get()); mfem::GridFunctionCoefficient gf_c(&gf); double local_integral; mfem::Array elem_markers; populate_element_mask(fem.mesh.get(), domain, elem_markers); if (fem.has_mapping() && coord_space == mapping::COORDINATE_SPACE::PHYSICAL) { mapping::MappedScalarCoefficient mapped_gf_c(*fem.mapping, gf_c); // ReSharper disable once CppDFAMemoryLeak // Disabled because MFEM takes ownership so memory is not leaked auto *lf_integrator = new mfem::DomainLFIntegrator(mapped_gf_c); lf_integrator->SetIntRule(fem.int_rule.get()); lf.AddDomainIntegrator(lf_integrator, elem_markers); lf.Assemble(); local_integral = lf.Sum(); } else { if (coord_space == mapping::COORDINATE_SPACE::PHYSICAL) { MFEM_ABORT( "Physical evaluation mode requested but no mapping provided. Check domain bounds and mapping setup."); } lf.AddDomainIntegrator(new mfem::DomainLFIntegrator(gf_c), elem_markers); lf.Assemble(); local_integral = lf.Sum(); } double global_integral = 0.0; MPI_Allreduce(&local_integral, &global_integral, 1, MPI_DOUBLE, MPI_SUM, fem.H1_fes->GetComm()); return global_integral; } mfem::Vector get_com(const fem::FEM &fem, const mfem::GridFunction &rho) { const int dim = fem.mesh->Dimension(); mfem::Vector local_com(dim); local_com = 0.0; double local_mass = 0.0; for (int i = 0; i < fem.H1_fes->GetNE(); ++i) { if (fem.mesh->GetAttribute(i) == 3) continue; mfem::ElementTransformation *trans = fem.H1_fes->GetElementTransformation(i); const mfem::IntegrationRule &ir = *fem.int_rule; for (int j = 0; j < ir.GetNPoints(); ++j) { const mfem::IntegrationPoint &ip = ir.IntPoint(j); trans->SetIntPoint(&ip); double weight = trans->Weight() * ip.weight; if (fem.has_mapping()) { weight *= fem.mapping->ComputeDetJ(*trans, ip); } double rho_val = rho.GetValue(i, ip); mfem::Vector phys_point(dim); if (fem.has_mapping()) { fem.mapping->GetPhysicalPoint(*trans, ip, phys_point); } else { trans->Transform(ip, phys_point); } const double mass_term = rho_val * weight; local_mass += mass_term; for (int d = 0; d < dim; ++d) { local_com(d) += phys_point(d) * mass_term; } } } double global_mass = 0.0; mfem::Vector global_com(dim); MPI_Comm comm = fem.H1_fes->GetComm(); MPI_Allreduce(&local_mass, &global_mass, 1, MPI_DOUBLE, MPI_SUM, comm); MPI_Allreduce(local_com.GetData(), global_com.GetData(), dim, MPI_DOUBLE, MPI_SUM, comm); if (global_mass > 1e-18) { global_com /= global_mass; } else { global_com = 0.0; } return global_com; } void conserve_mass(const fem::FEM &fem, mfem::GridFunction &rho, const double target_mass) { if (const double current_mass = domain_integrate_grid_function(fem, rho, utils::DOMAINS::STELLAR); current_mass > 1e-15) rho *= (target_mass / current_mass); } double get_moment_of_inertia(const fem::FEM &fem, const mfem::GridFunction &rho) { auto s2_func = [](const mfem::Vector &x) { return std::pow(x(0), 2) + std::pow(x(1), 2); }; std::unique_ptr s2_coeff; if (fem.has_mapping()) { s2_coeff = std::make_unique(*fem.mapping, s2_func); } else { s2_coeff = std::make_unique(s2_func); } mfem::GridFunctionCoefficient rho_coeff(&rho); mfem::ProductCoefficient I_integrand(rho_coeff, *s2_coeff); mfem::LinearForm I_lf(fem.H1_fes.get()); double I = 0.0; // TODO: Need to filter here to just the stellar domain and also update the IntRule if (fem.has_mapping()) { mapping::MappedScalarCoefficient mapped_integrand(*fem.mapping, I_integrand); I_lf.AddDomainIntegrator(new mfem::DomainLFIntegrator(mapped_integrand)); I_lf.Assemble(); I = I_lf.Sum(); } else { I_lf.AddDomainIntegrator(new mfem::DomainLFIntegrator(I_integrand)); I_lf.Assemble(); I = I_lf.Sum(); } return I; } double get_mesh_volume( const fem::FEM& fem, const mapping::COORDINATE_SPACE coordinate_space, const utils::DOMAINS domain ) { mfem::ParMesh &mesh = *fem.mesh; const mapping::DomainMapper &map = *fem.mapping; const mfem::IntegrationRule &ir = *fem.int_rule; const bool physical = (coordinate_space == mapping::COORDINATE_SPACE::PHYSICAL); double local_volume = 0.0; for (int e = 0; e < mesh.GetNE(); ++e) { const int attr = mesh.GetAttribute(e); switch (domain) { case utils::DOMAINS::ALL: break; case utils::DOMAINS::STELLAR: if (attr == 3) continue; break; case utils::DOMAINS::VACUUM: if (attr != 3) continue; break; default: MFEM_ABORT("Unsupported domain type for volume computation."); } mfem::ElementTransformation *T = mesh.GetElementTransformation(e); for (int q = 0; q < ir.GetNPoints(); ++q) { const mfem::IntegrationPoint &ip = ir.IntPoint(q); T->SetIntPoint(&ip); double dV = ip.weight * T->Weight(); if (physical) { dV *= std::fabs(map.ComputeDetJ(*T, ip)); } local_volume += dV; } } double global_volume = 0.0; MPI_Allreduce(&local_volume, &global_volume, 1, MPI_DOUBLE, MPI_SUM, mesh.GetComm()); return global_volume; } }