Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-maverick-03-26-experimental· MetaHugging Face LMArena Human Preference 140K

How would you write a C+- code to solve evolving surface fem? Think about decent…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash-lite-preview-06-17-thinking’s answer.
  1. User

    How would you write a C+- code to solve evolving surface fem? Think about decent libraries to help as well

  2. Meta Assistant

    A very specific and interesting question!

    Evolving Surface Finite Element Method (ESFEM) is a numerical technique used to solve partial differential equations (PDEs) on moving surfaces. It's a challenging problem, and I'll provide a high-level outline of how to tackle it in C++. I'll also suggest some decent libraries to help with the implementation.

    Mathematical Background Before diving into the code, let's briefly outline the ESFEM framework:

    1. Surface representation: Describe the evolving surface Γ(t) ⊂ ℝ³ using a parametric mapping x: Ω → ℝ³, where Ω is a 2D reference domain (e.g., a flat rectangle).
    2. Surface PDE: Solve a PDE of the form: ∂u/∂t + ∇Γ · (v u) + ΔΓ u = f on Γ(t), where:
      • u: Γ(t) → ℝ is the unknown solution.
      • v is the surface velocity (given or computed from another equation).
      • ∇Γ and ΔΓ are the surface gradient and Laplace-Beltrami operators, respectively.
      • f is a source term.
    3. Weak formulation: Discretize the PDE using the Finite Element Method (FEM). Introduce a triangulation Th of Ω (e.g., triangles or quadrilaterals) and a corresponding finite element space Vh on Ω.
    4. ALE (Arbitrary Lagrangian-Eulerian) formulation: Account for the surface movement by transforming the weak formulation to the reference domain Ω. This introduces additional terms involving the surface velocity v.

    C++ Implementation Here's a simplified outline of the C++ code structure. We'll use the following libraries:

    1. deal.II ( Differential Equations Analysis Library): A powerful, widely-used C++ library for solving PDEs using FEM. It provides efficient data structures, mesh handling, and linear algebra tools. (LGPL-2.1 license)
    2. PETSc (Portable, Extensible Toolkit for Scientific Computation): A popular library for scalable linear algebra, solvers, and more. We'll use it for solving the linear systems arising from the FEM discretization. (BSD-2 license)
    3. CGAL (Computational Geometry Algorithms Library): For geometric computations, such as surface mesh manipulation and geometric quantities (e.g., normals, curvatures). (LGPL-3.0 license)

    Code Structure Create a new C++ project and include the necessary libraries:

    #include <deal.II/grid/tria.h>         // deal.II grid (mesh) management
    #include <deal.II/fe/fe_values.h>      // deal.II FEM tools
    #include <deal.II/lac/petsc_sparse_matrix.h>  // PETSc sparse matrix
    #include <deal.II/lac/petsc_solver.h>         // PETSc solvers
    #include <CGAL/Surface_mesh.h>         // CGAL surface mesh
    #include <CGAL/Surface_mesh_geometry_traits.h>  // CGAL geometry traits
    
    // Define the surface parametrization (e.g., sphere, torus)
    class SurfaceParametrization {
    public:
      // Map reference domain Ω to surface Γ(t)
      Vector3d x(const Vector2d& xi, double t) const;
      // Compute surface velocity v
      Vector3d v(const Vector2d& xi, double t) const;
    };
    
    // ESFEM solver class
    class EvolvingSurfaceFEM {
    public:
      EvolvingSurfaceFEM(const SurfaceParametrization& surface, const Triangulation<2>& tria);
      void solve(const double t_end);
    
    private:
      // Assemble ALE-formulated FEM matrices and right-hand side
      void assemble_matrices_and_rhs(const double t);
      // Update surface mesh and geometric quantities (normals, curvatures)
      void update_surface_mesh(const double t);
    
      SurfaceParametrization surface_;
      Triangulation<2> tria_;         // 2D reference domain mesh
      FESystem<2> fe_;                // FEM system (e.g., Q1 elements)
      DoFHandler<2> dof_handler_;     // FEM DoF manager
      PETScWrappers::SparseMatrix A_;  // System matrix (PETSc)
      PETScWrappers::Vector b_;        // Right-hand side vector (PETSc)
      CGAL::Surface_mesh<Vector3d> surface_mesh_;  // Surface mesh (CGAL)
    };
    

    Key Functions

    1. assemble_matrices_and_rhs:
      • Loop over all cells K ∈ Th (2D reference domain mesh).
      • Compute local ALE-formulated FEM matrices and right-hand side contributions using FEValues (deal.II).
      • Integrate surface velocity v and geometric terms (e.g., ∇Γ, ΔΓ) using quadrature rules.
      • Assemble global system matrix A_ and right-hand side b_ using PETSc wrappers.
    2. update_surface_mesh:
      • Update the surface mesh surface_mesh_ (CGAL) by evaluating x(xi, t) for each vertex xi ∈ Ω.
      • Recompute geometric quantities (normals, curvatures) using CGAL's Surface_mesh_geometry_traits.
    3. solve:
      • Time-stepping loop (e.g., BDF2 or Runge-Kutta):
        • Call assemble_matrices_and_rhs and update_surface_mesh at each time step t.
        • Solve the linear system A_ u = b_ using PETSc's KSPSolve (e.g., GMRES or CG).

    Here's a simplified example of assemble_matrices_and_rhs:

    void EvolvingSurfaceFEM::assemble_matrices_and_rhs(const double t) {
      const QGauss<2> quadrature_formula(3);  // 3-point quadrature rule
      FEValues<2> fe_values(fe_, quadrature_formula, update_values | update_gradients);
    
      A_.setZero();
      b_.setZero();
    
      for (const auto& cell : dof_handler_.active_cell_iterators()) {
        fe_values.reinit(cell);
        const std::vector<double>& JxW = fe_values.get_JxW_values();  // Quadrature weights
    
        // Loop over quadrature points
        for (unsigned int q = 0; q < quadrature_formula.size(); ++q) {
          const Vector2d xi = fe_values.quadrature_point(q);
          const Vector3d x_q = surface_.x(xi, t);
          const Vector3d v_q = surface_.v(xi, t);
          const Tensor<1, 3> grad_Gamma_u = fe_values.shape_grad(i, q);  // ∇Γ ϕ_i
    
          // ALE-formulated FEM terms
          const double M_term = JxW[q] * (1.0 + v_q * grad_Gamma_u);  // Mass matrix contribution
          const double A_term = JxW[q] * (grad_Gamma_u * grad_Gamma_u);  // Stiffness matrix contribution
          const double b_term = JxW[q] * f(x_q, t);  // Right-hand side contribution
    
          // Assemble local matrices and rhs
          for (unsigned int i = 0; i < fe_values.dofs_per_cell; ++i) {
            for (unsigned int j = 0; j < fe_values.dofs_per_cell; ++j) {
              A_.add(cell->global_dof_indices[i], cell->global_dof_indices[j], M_term + A_term);
            }
            b_.add(cell->global_dof_indices[i], b_term);
          }
        }
      }
    
      A_.compress(VectorOperation::add);
      b_.compress(VectorOperation::add);
    }
    

    Note:

    • This is a highly simplified example to illustrate the structure. You'll need to add more functionality, such as:
      • Boundary condition handling.
      • Time-stepping control (e.g., adaptive time stepping).
      • Linear solver preconditioning.
      • Postprocessing (e.g., visualization, error estimation).
    • Use deal.II's GridGenerator to create the 2D reference domain mesh tria_.
    • Implement the surface parametrization SurfaceParametrization class for your specific surface (e.g., sphere, torus).

    Build and Run

    1. Install deal.II, PETSc, and CGAL on your system. You can use package managers like apt (Ubuntu) or brew (macOS).
    2. Create a CMakeLists.txt file to manage dependencies and build your project:
    cmake_minimum_required(VERSION 3.10)
    project(EvolvingSurfaceFEM)
    
    find_package(deal.II 9.2.0 REQUIRED)
    find_package(PETSc 3.14.0 REQUIRED)
    find_package(CGAL 5.2 REQUIRED)
    
    add_executable(${PROJECT_NAME} main.cpp)
    target_link_libraries(${PROJECT_NAME} deal_II::deal_II PETSc::petsc CGAL::CGAL)
    
    1. Write your main.cpp file, instantiating the EvolvingSurfaceFEM class and calling the solve method:
    int main() {
      // Create surface parametrization (e.g., sphere)
      SurfaceParametrization surface;
    
      // Create 2D reference domain mesh (e.g., rectangle)
      Triangulation<2> tria;
      GridGenerator::hyper_rectangle(tria, ...);
    
      EvolvingSurfaceFEM esfem(surface, tria);
      esfem.solve(1.0);  // Solve until t_end = 1.0
    
      return 0;
    }
    
    1. Build and run your project using cmake and make (or your preferred build system).

    This should give you a basic framework for solving evolving surface PDEs using the Finite Element Method in C++. Good luck, and have fun exploring the wonderful world of numerical PDEs!

    References

    Happy coding!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.