LCOV - code coverage report
Current view: top level - include/utils - ParameterMesh.h (source / functions) Hit Total Coverage
Test: idaholab/moose optimization: #31405 (292dce) with base fef103 Lines: 1 1 100.0 %
Date: 2025-09-04 07:54:57 Functions: 0 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             : #pragma once
      11             : 
      12             : #include "MooseTypes.h"
      13             : #include "KDTree.h"
      14             : 
      15             : #include "libmesh/id_types.h"
      16             : #include <unordered_map>
      17             : #include "libmesh/parallel.h"
      18             : #include "libmesh/fe_type.h"
      19             : #include "libmesh/point.h"
      20             : #include "libmesh/replicated_mesh.h"
      21             : #include "libmesh/equation_systems.h"
      22             : #include "libmesh/point_locator_base.h"
      23             : #include "libmesh/exodusII_io.h"
      24             : #include <vector>
      25             : 
      26             : namespace libMesh
      27             : {
      28             : class System;
      29             : }
      30             : 
      31             : using libMesh::RealGradient;
      32             : 
      33             : /**
      34             :  * Utility class to use an Exodus mesh to define controllable parameters for optimization problems
      35             :  * This class will:
      36             :  *  - Ensure that controllable parameters defined by the mesh are correctly ordered on optimiation
      37             :  * main app and forward or adjoint sub-apps
      38             :  *  - Read initial conditions and bounds from an exodus file
      39             :  *  - Define the parameter space (nodal/elemental and shape function)
      40             :  *  - Interpolate parameter values to the forward problem mesh using nodal/elemental shape function
      41             :  */
      42             : class ParameterMesh
      43             : {
      44             : public:
      45             :   ParameterMesh(const libMesh::FEType & param_type,
      46             :                 const std::string & exodus_mesh,
      47             :                 const std::vector<std::string> & var_names = {},
      48             :                 const bool find_closest = false,
      49             :                 const unsigned int kdtree_candidates = 5);
      50             : 
      51             :   /**
      52             :    * @return the number of parameters read from the mesh for a single timestep
      53             :    */
      54    13306803 :   dof_id_type size() const { return _param_dofs; }
      55             :   /**
      56             :    * Interpolate parameters onto the computational mesh
      57             :    * getIndexAndWeight is only used by ParameterMeshFunction
      58             :    * @param pt location to compute elemnent dof_indices weights
      59             :    * @param dof_indices return dof indices for element containing pt
      60             :    * @param weights returns element shape function weights at pt
      61             :    */
      62             :   void getIndexAndWeight(const Point & pt,
      63             :                          std::vector<dof_id_type> & dof_indices,
      64             :                          std::vector<Real> & weights) const;
      65             :   /**
      66             :    * Performs inner products of parameters with functions on the computational mesh
      67             :    * getIndexAndWeight is only used by ParameterMeshFunction
      68             :    * @param pt location to compute elemnent dof_indices weights
      69             :    * @param dof_indices return dof indices for element containing pt
      70             :    * @param weights returns element shape function gradient weights at pt
      71             :    */
      72             :   void getIndexAndWeight(const Point & pt,
      73             :                          std::vector<dof_id_type> & dof_indices,
      74             :                          std::vector<RealGradient> & weights) const;
      75             :   /**
      76             :    * Initializes parameter data and sets bounds in the main optmiization application
      77             :    * getParameterValues is only used by ParameterMeshOptimization
      78             :    * @param var_name  variable to read off mesh
      79             :    * @param timestep  timestep to read variable off mesh
      80             :    * @return vector of variables read off mesh at timestep
      81             :    */
      82             :   std::vector<Real> getParameterValues(std::string var_name, unsigned int timestep) const;
      83             : 
      84             : protected:
      85             :   libMesh::Parallel::Communicator _communicator;
      86             :   libMesh::ReplicatedMesh _mesh;
      87             :   /// Find closest projection points
      88             :   const bool _find_closest;
      89             :   std::unique_ptr<libMesh::EquationSystems> _eq;
      90             :   libMesh::System * _sys;
      91             :   std::unique_ptr<libMesh::PointLocatorBase> _point_locator;
      92             :   std::unique_ptr<libMesh::ExodusII_IO> _exodusII_io;
      93             : 
      94             :   dof_id_type _param_dofs;
      95             : 
      96             :   /// Node-based KDTree optimization
      97             :   std::vector<Point> _mesh_nodes;
      98             :   std::unique_ptr<KDTree> _node_kdtree;
      99             :   std::unordered_map<dof_id_type, std::set<const libMesh::Elem *>> _node_to_elements;
     100             :   unsigned int _kdtree_candidates;
     101             : 
     102             : private:
     103             :   /**
     104             :    * Returns the point on the parameter mesh that is projected from the test point
     105             :    * @param p test point
     106             :    * @return Point
     107             :    */
     108             :   Point projectToMesh(const Point & p) const;
     109             : 
     110             :   /**
     111             :    * Find closest point on the element to the given point
     112             :    * @param elem
     113             :    * @param p
     114             :    * @return Point
     115             :    */
     116             :   Point closestPoint(const Elem & elem, const Point & p) const;
     117             : };

Generated by: LCOV version 1.14