LCOV - code coverage report
Current view: top level - src/mfem/vectorpostprocessors - MFEMValueSamplerBase.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: fa5e60 Lines: 58 60 96.7 %
Date: 2026-06-24 08:03:36 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #ifdef MOOSE_MFEM_ENABLED
      11             : 
      12             : #include "MFEMValueSamplerBase.h"
      13             : #include "MFEMProblem.h"
      14             : #include "MFEMVectorUtils.h"
      15             : 
      16             : #include "mfem/fem/fespace.hpp"
      17             : 
      18             : namespace
      19             : {
      20             : void
      21         620 : MFEMVectorToPostprocessorPoints(
      22             :     const mfem::Vector & mfem_points,
      23             :     std::vector<std::reference_wrapper<VectorPostprocessorValue>> & points,
      24             :     const unsigned int num_dims,
      25             :     const mfem::Ordering::Type ordering)
      26             : {
      27         620 :   const unsigned int num_points = mfem_points.Size() / num_dims;
      28        4412 :   for (unsigned int i_point = 0; i_point < num_points; i_point++)
      29             :   {
      30       11822 :     for (unsigned int i_dim = 0; i_dim < num_dims; i_dim++)
      31             :     {
      32        8030 :       const size_t idx = Moose::MFEM::MFEMIndex(i_dim, i_point, num_dims, num_points, ordering);
      33             : 
      34        8030 :       points[i_dim].get()[i_point] = mfem_points(idx);
      35             :     }
      36             :   }
      37         620 : }
      38             : }
      39             : 
      40             : InputParameters
      41        4800 : MFEMValueSamplerBase::validParams()
      42             : {
      43        4800 :   InputParameters params = MFEMVectorPostprocessor::validParams();
      44             : 
      45       19200 :   MFEMExecutedObject::addRequiredDependencyParam<VariableName>(
      46             :       params, "variable", "The names of the variables that this VectorPostprocessor operates on");
      47       19200 :   MooseEnum ordering("NODES VDIM", "VDIM", false);
      48       14400 :   params.addParam<MooseEnum>(
      49             :       "point_ordering", ordering, "Ordering style to use for point vector DoFs.");
      50             : 
      51        9600 :   return params;
      52        4800 : }
      53             : 
      54         302 : MFEMValueSamplerBase::MFEMValueSamplerBase(const InputParameters & parameters,
      55         302 :                                            const std::vector<Point> & points)
      56             :   : MFEMVectorPostprocessor(parameters),
      57         302 :     _var_name(getParam<VariableName>("variable")),
      58         302 :     _var(*getMFEMProblem().getGridFunction(_var_name)),
      59         302 :     _mesh(const_cast<mfem::ParMesh &>(getMFEMProblem().getMFEMVariableMesh(_var_name))),
      60         302 :     _finder(this->comm().get()),
      61         604 :     _points_ordering(getParam<MooseEnum>("point_ordering") == "NODES" ? mfem::Ordering::byNODES
      62             :                                                                       : mfem::Ordering::byVDIM),
      63         302 :     _points(
      64         302 :         Moose::MFEM::libMeshPointsToMFEMVector(points, _mesh.SpaceDimension(), _points_ordering)),
      65         604 :     _interp_vals(points.size())
      66             : {
      67         302 :   if (getMFEMProblem().mesh().shouldDisplace())
      68           0 :     mooseError("MFEMValueSamplerBase does not yet support problems with displacement.");
      69             : 
      70             :   // set up points vector
      71         302 :   _mesh.EnsureNodes();
      72         302 :   _finder.Setup(_mesh);
      73         302 :   _finder.FindPoints(_points, _points_ordering);
      74             : 
      75             :   // check all points were found
      76         302 :   mfem::Array<unsigned int> point_codes = _finder.GetCode();
      77        3728 :   for (size_t i = 0; i < points.size(); i++)
      78             :   {
      79        3426 :     if (point_codes[i] > 1)
      80             :     {
      81           0 :       mooseError("MFEMValueSamplerBase could not find point at ", points[i], ".");
      82             :     }
      83             :   }
      84             : 
      85             :   // declare points vectors for outputting
      86         302 :   const auto mesh_dim = _mesh.SpaceDimension();
      87         970 :   for (int i = 0; i < mesh_dim; i++)
      88             :   {
      89             :     std::reference_wrapper<VectorPostprocessorValue> declared_dim =
      90         668 :         this->declareVector("x_" + std::to_string(i));
      91         668 :     declared_dim.get().resize(points.size());
      92         668 :     _declared_points.push_back(declared_dim);
      93             :   }
      94             : 
      95             :   // declare value vectors for outputting
      96         302 :   const auto val_dim = _var.VectorDim();
      97         668 :   for (int i = 0; i < val_dim; i++)
      98             :   {
      99             :     std::reference_wrapper<VectorPostprocessorValue> declared_dim =
     100         366 :         this->declareVector(_var_name + "_" + std::to_string(i));
     101         366 :     declared_dim.get().resize(points.size());
     102         366 :     _declared_vals.push_back(declared_dim);
     103             :   }
     104         302 : }
     105             : 
     106             : void
     107         620 : MFEMValueSamplerBase::execute()
     108             : {
     109         620 :   _finder.Interpolate(_var, _interp_vals);
     110         620 : }
     111             : 
     112             : void
     113         620 : MFEMValueSamplerBase::finalize()
     114             : {
     115         620 :   _interp_vals.HostReadWrite();
     116         620 :   _points.HostReadWrite();
     117             : 
     118         620 :   const auto mesh_dim = _mesh.SpaceDimension();
     119         620 :   MFEMVectorToPostprocessorPoints(_points, _declared_points, mesh_dim, _points_ordering);
     120         620 :   const auto val_dims = _var.VectorDim();
     121         620 :   const auto num_points = _declared_points[0].get().size();
     122         620 :   const auto val_fespace_ordering = _var.FESpace()->GetOrdering();
     123        1304 :   for (int i_dim = 0; i_dim < val_dims; i_dim++)
     124             :   {
     125        4934 :     for (size_t i_point = 0; i_point < num_points; i_point++)
     126             :     {
     127             :       const auto mfem_idx =
     128        4250 :           Moose::MFEM::MFEMIndex(i_dim, i_point, val_dims, num_points, val_fespace_ordering);
     129        4250 :       _declared_vals[i_dim].get()[i_point] = _interp_vals[mfem_idx];
     130             :     }
     131             :   }
     132         620 : }
     133             : 
     134             : #endif // MOOSE_MFEM_ENABLED

Generated by: LCOV version 1.14