LCOV - code coverage report
Current view: top level - src/utils - Conversion.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 201 262 76.7 %
Date: 2026-05-29 20:35:17 Functions: 26 31 83.9 %
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             : // MOOSE includes
      11             : #include "Conversion.h"
      12             : #include "MooseError.h"
      13             : #include "ExecFlagEnum.h"
      14             : #include "MooseUtils.h"
      15             : 
      16             : #include "libmesh/string_to_enum.h"
      17             : #include "libmesh/point.h"
      18             : 
      19             : // system includes
      20             : #include <iomanip>
      21             : 
      22             : using namespace libMesh;
      23             : 
      24             : namespace Moose
      25             : {
      26             : std::map<std::string, CoordinateSystemType> coordinate_system_type_to_enum;
      27             : std::map<std::string, SolveType> solve_type_to_enum;
      28             : std::map<std::string, EigenSolveType> eigen_solve_type_to_enum;
      29             : std::map<std::string, EigenProblemType> eigen_problem_type_to_enum;
      30             : std::map<std::string, WhichEigenPairs> which_eigen_pairs_to_enum;
      31             : std::map<std::string, LineSearchType> line_search_type_to_enum;
      32             : std::map<std::string, TimeIntegratorType> time_integrator_to_enum;
      33             : std::map<std::string, MffdType> mffd_type_to_enum;
      34             : std::map<std::string, RelationshipManagerType> rm_type_to_enum;
      35             : 
      36             : void
      37       64349 : initCoordinateSystemType()
      38             : {
      39       64349 :   if (coordinate_system_type_to_enum.empty())
      40             :   {
      41      105884 :     coordinate_system_type_to_enum["XYZ"] = COORD_XYZ;
      42      105884 :     coordinate_system_type_to_enum["RZ"] = COORD_RZ;
      43      105884 :     coordinate_system_type_to_enum["RSPHERICAL"] = COORD_RSPHERICAL;
      44             :   }
      45       64349 : }
      46             : 
      47             : void
      48       35286 : initSolveType()
      49             : {
      50       35286 :   if (solve_type_to_enum.empty())
      51             :   {
      52       58946 :     solve_type_to_enum["PJFNK"] = ST_PJFNK;
      53       58946 :     solve_type_to_enum["JFNK"] = ST_JFNK;
      54       58946 :     solve_type_to_enum["NEWTON"] = ST_NEWTON;
      55       58946 :     solve_type_to_enum["FD"] = ST_FD;
      56       58946 :     solve_type_to_enum["LINEAR"] = ST_LINEAR;
      57             :   }
      58       35286 : }
      59             : 
      60             : void
      61         565 : initEigenSolveType()
      62             : {
      63         565 :   if (eigen_solve_type_to_enum.empty())
      64             :   {
      65        1130 :     eigen_solve_type_to_enum["POWER"] = EST_POWER;
      66        1130 :     eigen_solve_type_to_enum["ARNOLDI"] = EST_ARNOLDI;
      67        1130 :     eigen_solve_type_to_enum["KRYLOVSCHUR"] = EST_KRYLOVSCHUR;
      68        1130 :     eigen_solve_type_to_enum["JACOBI_DAVIDSON"] = EST_JACOBI_DAVIDSON;
      69        1130 :     eigen_solve_type_to_enum["NONLINEAR_POWER"] = EST_NONLINEAR_POWER;
      70        1130 :     eigen_solve_type_to_enum["NEWTON"] = EST_NEWTON;
      71        1130 :     eigen_solve_type_to_enum["PJFNK"] = EST_PJFNK;
      72        1130 :     eigen_solve_type_to_enum["PJFNKMO"] = EST_PJFNKMO;
      73        1130 :     eigen_solve_type_to_enum["JFNK"] = EST_JFNK;
      74             :   }
      75         565 : }
      76             : 
      77             : void
      78         565 : initEigenProlemType()
      79             : {
      80         565 :   if (eigen_problem_type_to_enum.empty())
      81             :   {
      82        1130 :     eigen_problem_type_to_enum["HERMITIAN"] = EPT_HERMITIAN;
      83        1130 :     eigen_problem_type_to_enum["NON_HERMITIAN"] = EPT_NON_HERMITIAN;
      84        1130 :     eigen_problem_type_to_enum["GEN_HERMITIAN"] = EPT_GEN_HERMITIAN;
      85        1130 :     eigen_problem_type_to_enum["GEN_NON_HERMITIAN"] = EPT_GEN_NON_HERMITIAN;
      86        1130 :     eigen_problem_type_to_enum["GEN_INDEFINITE"] = EPT_GEN_INDEFINITE;
      87        1130 :     eigen_problem_type_to_enum["POS_GEN_NON_HERMITIAN"] = EPT_POS_GEN_NON_HERMITIAN;
      88        1130 :     eigen_problem_type_to_enum["SLEPC_DEFAULT"] = EPT_SLEPC_DEFAULT;
      89             :   }
      90         565 : }
      91             : 
      92             : void
      93          69 : initWhichEigenPairs()
      94             : {
      95          69 :   if (which_eigen_pairs_to_enum.empty())
      96             :   {
      97         138 :     which_eigen_pairs_to_enum["LARGEST_MAGNITUDE"] = WEP_LARGEST_MAGNITUDE;
      98         138 :     which_eigen_pairs_to_enum["SMALLEST_MAGNITUDE"] = WEP_SMALLEST_MAGNITUDE;
      99         138 :     which_eigen_pairs_to_enum["LARGEST_REAL"] = WEP_LARGEST_REAL;
     100         138 :     which_eigen_pairs_to_enum["SMALLEST_REAL"] = WEP_SMALLEST_REAL;
     101         138 :     which_eigen_pairs_to_enum["LARGEST_IMAGINARY"] = WEP_LARGEST_IMAGINARY;
     102         138 :     which_eigen_pairs_to_enum["SMALLEST_IMAGINARY"] = WEP_SMALLEST_IMAGINARY;
     103         138 :     which_eigen_pairs_to_enum["TARGET_MAGNITUDE"] = WEP_TARGET_MAGNITUDE;
     104         138 :     which_eigen_pairs_to_enum["TARGET_REAL"] = WEP_TARGET_REAL;
     105         138 :     which_eigen_pairs_to_enum["TARGET_IMAGINARY"] = WEP_TARGET_IMAGINARY;
     106         138 :     which_eigen_pairs_to_enum["ALL_EIGENVALUES"] = WEP_ALL_EIGENVALUES;
     107         138 :     which_eigen_pairs_to_enum["SLEPC_DEFAULT"] = WEP_SLEPC_DEFAULT;
     108             :   }
     109          69 : }
     110             : 
     111             : void
     112       59065 : initLineSearchType()
     113             : {
     114       59065 :   if (line_search_type_to_enum.empty())
     115             :   {
     116       94848 :     line_search_type_to_enum["DEFAULT"] = LS_DEFAULT;
     117       94848 :     line_search_type_to_enum["NONE"] = LS_NONE;
     118       94848 :     line_search_type_to_enum["BASIC"] = LS_BASIC;
     119             : 
     120       94848 :     line_search_type_to_enum["SHELL"] = LS_SHELL;
     121       94848 :     line_search_type_to_enum["L2"] = LS_L2;
     122       94848 :     line_search_type_to_enum["BT"] = LS_BT;
     123       94848 :     line_search_type_to_enum["CP"] = LS_CP;
     124       94848 :     line_search_type_to_enum["CONTACT"] = LS_CONTACT;
     125       94848 :     line_search_type_to_enum["PROJECT"] = LS_PROJECT;
     126             :   }
     127       59065 : }
     128             : 
     129             : void
     130          60 : initTimeIntegratorsType()
     131             : {
     132          60 :   if (time_integrator_to_enum.empty())
     133             :   {
     134          44 :     time_integrator_to_enum["IMPLICIT_EULER"] = TI_IMPLICIT_EULER;
     135          44 :     time_integrator_to_enum["EXPLICIT_EULER"] = TI_EXPLICIT_EULER;
     136          44 :     time_integrator_to_enum["CRANK_NICOLSON"] = TI_CRANK_NICOLSON;
     137          44 :     time_integrator_to_enum["BDF2"] = TI_BDF2;
     138          44 :     time_integrator_to_enum["EXPLICIT_MIDPOINT"] = TI_EXPLICIT_MIDPOINT;
     139          44 :     time_integrator_to_enum["LSTABLE_DIRK2"] = TI_LSTABLE_DIRK2;
     140          44 :     time_integrator_to_enum["EXPLICIT_TVDRK2"] = TI_EXPLICIT_TVD_RK_2;
     141             :   }
     142          60 : }
     143             : 
     144             : void
     145       72457 : initMffdType()
     146             : {
     147       72457 :   if (mffd_type_to_enum.empty())
     148             :   {
     149       94848 :     mffd_type_to_enum["DS"] = MFFD_DS;
     150       94848 :     mffd_type_to_enum["WP"] = MFFD_WP;
     151             :   }
     152       72457 : }
     153             : 
     154             : void
     155           0 : initRMType()
     156             : {
     157           0 :   if (rm_type_to_enum.empty())
     158             :   {
     159           0 :     rm_type_to_enum["DEFAULT"] = RelationshipManagerType::DEFAULT;
     160           0 :     rm_type_to_enum["GEOMETRIC"] = RelationshipManagerType::GEOMETRIC;
     161           0 :     rm_type_to_enum["ALGEBRAIC"] = RelationshipManagerType::ALGEBRAIC;
     162           0 :     rm_type_to_enum["COUPLING"] = RelationshipManagerType::COUPLING;
     163             :   }
     164           0 : }
     165             : 
     166             : template <>
     167             : QuadratureType
     168       65999 : stringToEnum<QuadratureType>(const std::string & s)
     169             : {
     170       65999 :   return Utility::string_to_enum<QuadratureType>("Q" + s);
     171             : }
     172             : 
     173             : template <>
     174             : Order
     175      198050 : stringToEnum<Order>(const std::string & s)
     176             : {
     177      198050 :   std::string upper(s);
     178      198050 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     179             : 
     180      198050 :   if (upper.compare("AUTO") == 0)
     181      197396 :     return INVALID_ORDER;
     182             :   else
     183         654 :     return Utility::string_to_enum<Order>(upper);
     184      198050 : }
     185             : 
     186             : template <>
     187             : CoordinateSystemType
     188       64349 : stringToEnum<CoordinateSystemType>(const std::string & s)
     189             : {
     190       64349 :   initCoordinateSystemType();
     191             : 
     192       64349 :   std::string upper(s);
     193       64349 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     194             : 
     195       64349 :   if (!coordinate_system_type_to_enum.count(upper))
     196           0 :     mooseError("Unknown coordinate system type: ", upper);
     197             : 
     198      128698 :   return coordinate_system_type_to_enum[upper];
     199       64349 : }
     200             : 
     201             : template <>
     202             : SolveType
     203       35286 : stringToEnum<SolveType>(const std::string & s)
     204             : {
     205       35286 :   initSolveType();
     206             : 
     207       35286 :   std::string upper(s);
     208       35286 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     209             : 
     210       35286 :   if (!solve_type_to_enum.count(upper))
     211           0 :     mooseError("Unknown solve type: ", upper);
     212             : 
     213       70572 :   return solve_type_to_enum[upper];
     214       35286 : }
     215             : 
     216             : template <>
     217             : EigenSolveType
     218         565 : stringToEnum<EigenSolveType>(const std::string & s)
     219             : {
     220         565 :   initEigenSolveType();
     221             : 
     222         565 :   std::string upper(s);
     223         565 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     224             : 
     225         565 :   if (!eigen_solve_type_to_enum.count(upper))
     226           0 :     mooseError("Unknown eigen solve type: ", upper);
     227             : 
     228        1130 :   return eigen_solve_type_to_enum[upper];
     229         565 : }
     230             : 
     231             : template <>
     232             : EigenProblemType
     233         565 : stringToEnum<EigenProblemType>(const std::string & s)
     234             : {
     235         565 :   initEigenProlemType();
     236             : 
     237         565 :   std::string upper(s);
     238         565 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     239             : 
     240         565 :   if (!eigen_problem_type_to_enum.count(upper))
     241           0 :     mooseError("Unknown eigen problem type: ", upper);
     242             : 
     243        1130 :   return eigen_problem_type_to_enum[upper];
     244         565 : }
     245             : 
     246             : template <>
     247             : WhichEigenPairs
     248          69 : stringToEnum<WhichEigenPairs>(const std::string & s)
     249             : {
     250          69 :   initWhichEigenPairs();
     251             : 
     252          69 :   std::string upper(s);
     253          69 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     254             : 
     255          69 :   if (!which_eigen_pairs_to_enum.count(upper))
     256           0 :     mooseError("Unknown type of WhichEigenPairs: ", upper);
     257             : 
     258         138 :   return which_eigen_pairs_to_enum[upper];
     259          69 : }
     260             : 
     261             : template <>
     262             : LineSearchType
     263       59065 : stringToEnum<LineSearchType>(const std::string & s)
     264             : {
     265       59065 :   initLineSearchType();
     266             : 
     267       59065 :   std::string upper(s);
     268       59065 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     269             : 
     270       59065 :   if (!line_search_type_to_enum.count(upper))
     271           0 :     mooseError("Unknown line search type: ", upper);
     272             : 
     273      118130 :   return line_search_type_to_enum[upper];
     274       59065 : }
     275             : 
     276             : template <>
     277             : TimeIntegratorType
     278          60 : stringToEnum<TimeIntegratorType>(const std::string & s)
     279             : {
     280          60 :   initTimeIntegratorsType();
     281             : 
     282          60 :   std::string upper(s);
     283          60 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     284             : 
     285          60 :   if (!time_integrator_to_enum.count(upper))
     286           0 :     mooseError("Unknown time integrator: ", upper);
     287             : 
     288         120 :   return time_integrator_to_enum[upper];
     289          60 : }
     290             : 
     291             : template <>
     292             : MffdType
     293       72457 : stringToEnum<MffdType>(const std::string & s)
     294             : {
     295       72457 :   initMffdType();
     296             : 
     297       72457 :   std::string upper(s);
     298       72457 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     299             : 
     300       72457 :   if (!mffd_type_to_enum.count(upper))
     301           0 :     mooseError("Unknown mffd type: ", upper);
     302             : 
     303      144914 :   return mffd_type_to_enum[upper];
     304       72457 : }
     305             : 
     306             : template <>
     307             : RelationshipManagerType
     308           0 : stringToEnum<RelationshipManagerType>(const std::string & s)
     309             : {
     310           0 :   initRMType();
     311             : 
     312           0 :   std::string upper(s);
     313           0 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     314             : 
     315           0 :   if (!rm_type_to_enum.count(upper))
     316           0 :     mooseError("Unknown RelationshipManager type: ", upper);
     317             : 
     318           0 :   return rm_type_to_enum[upper];
     319           0 : }
     320             : 
     321             : // Ignore warnings about switching on the |'d type
     322             : #include "libmesh/ignore_warnings.h"
     323             : 
     324             : // Definition in MooseTypes.h
     325             : std::string
     326      576784 : stringify(const RelationshipManagerType & t)
     327             : {
     328             :   // Cannot make a switch statement because the boolean logic doesn't work well with the class type
     329             :   // enumeration and because Cody says so.
     330      576784 :   if (t == RelationshipManagerType::DEFAULT)
     331           0 :     return "DEFAULT";
     332      576784 :   if (t == RelationshipManagerType::GEOMETRIC)
     333      240504 :     return "GEOMETRIC";
     334      456532 :   if (t == RelationshipManagerType::ALGEBRAIC)
     335        6942 :     return "ALGEBRAIC";
     336      453061 :   if (t == (RelationshipManagerType::GEOMETRIC | RelationshipManagerType::ALGEBRAIC))
     337      314268 :     return "GEOMETRIC and ALGEBRAIC";
     338      295927 :   if (t == (RelationshipManagerType::ALGEBRAIC | RelationshipManagerType::COUPLING))
     339           0 :     return "ALGEBRAIC and COUPLING";
     340      295927 :   if (t == (RelationshipManagerType::GEOMETRIC | RelationshipManagerType::ALGEBRAIC |
     341             :             RelationshipManagerType::COUPLING))
     342      106022 :     return "GEOMETRIC and ALGEBRAIC and COUPLING";
     343      242916 :   if (t == RelationshipManagerType::COUPLING)
     344      485832 :     return "COUPLING";
     345             : 
     346           0 :   mooseError("Unknown RelationshipManagerType");
     347             : }
     348             : 
     349             : std::string
     350          12 : stringify(libMesh::FEFamily f)
     351             : {
     352          12 :   return libMesh::Utility::enum_to_string(f);
     353             : }
     354             : 
     355             : // Turn the warnings back on
     356             : #include "libmesh/restore_warnings.h"
     357             : 
     358             : std::string
     359       53043 : stringify(const SolveType & t)
     360             : {
     361       53043 :   switch (t)
     362             :   {
     363       11015 :     case ST_NEWTON:
     364       22030 :       return "NEWTON";
     365          77 :     case ST_JFNK:
     366         154 :       return "JFNK";
     367       39820 :     case ST_PJFNK:
     368       79640 :       return "Preconditioned JFNK";
     369           2 :     case ST_FD:
     370           4 :       return "FD";
     371        2129 :     case ST_LINEAR:
     372        4258 :       return "Linear";
     373             :   }
     374           0 :   return "";
     375             : }
     376             : 
     377             : std::string
     378         550 : stringify(const EigenSolveType & t)
     379             : {
     380         550 :   switch (t)
     381             :   {
     382           0 :     case EST_POWER:
     383           0 :       return "Power";
     384           0 :     case EST_ARNOLDI:
     385           0 :       return "ARNOLDI";
     386          36 :     case EST_KRYLOVSCHUR:
     387          72 :       return "KRYLOVSCHUR";
     388          24 :     case EST_JACOBI_DAVIDSON:
     389          48 :       return "Jacobi Davidson";
     390          12 :     case EST_NONLINEAR_POWER:
     391          24 :       return "Nonlinear Power";
     392          90 :     case EST_PJFNKMO:
     393         180 :       return "PJFNK with Matrix Only";
     394          33 :     case EST_NEWTON:
     395          66 :       return "Newton";
     396          36 :     case EST_JFNK:
     397          72 :       return "JFNK";
     398         319 :     case EST_PJFNK:
     399         638 :       return "Preconditioned JFNK";
     400             :   }
     401           0 :   return "";
     402             : }
     403             : 
     404             : std::string
     405          12 : stringify(const VarFieldType & t)
     406             : {
     407          12 :   switch (t)
     408             :   {
     409           6 :     case VAR_FIELD_STANDARD:
     410          12 :       return "STANDARD";
     411           6 :     case VAR_FIELD_VECTOR:
     412          12 :       return "VECTOR";
     413           0 :     case VAR_FIELD_ARRAY:
     414           0 :       return "ARRAY";
     415           0 :     case VAR_FIELD_SCALAR:
     416           0 :       return "SCALAR";
     417           0 :     case VAR_FIELD_ANY:
     418           0 :       return "ANY";
     419             :   }
     420           0 :   return "";
     421             : }
     422             : 
     423             : std::string
     424           0 : stringify(SolutionIterationType t)
     425             : {
     426           0 :   switch (t)
     427             :   {
     428           0 :     case SolutionIterationType::Time:
     429           0 :       return "time";
     430           0 :     case SolutionIterationType::Nonlinear:
     431           0 :       return "nonlinear";
     432           0 :     case SolutionIterationType::FixedPoint:
     433           0 :       return "fixed_point";
     434           0 :     default:
     435           0 :       mooseError("Unhandled SolutionIterationType");
     436             :   }
     437             : }
     438             : 
     439             : std::string
     440           0 : stringify(ElementType t)
     441             : {
     442           0 :   switch (t)
     443             :   {
     444           0 :     case ElementType::Element:
     445           0 :       return "ELEMENT";
     446           0 :     case ElementType::Neighbor:
     447           0 :       return "NEIGHBOR";
     448           0 :     case ElementType::Lower:
     449           0 :       return "LOWER";
     450           0 :     default:
     451           0 :       mooseError("unrecognized type");
     452             :   }
     453             : }
     454             : 
     455             : std::string
     456         533 : stringify(libMesh::ElemType t)
     457             : {
     458         533 :   return libMesh::Utility::enum_to_string(t);
     459             : }
     460             : 
     461             : std::string
     462     1422044 : stringify(const std::string & s)
     463             : {
     464     1422044 :   return s;
     465             : }
     466             : 
     467             : std::string
     468          14 : stringifyExact(Real t)
     469             : {
     470             :   // this or std::numeric_limits<T>::max_digits10
     471          14 :   const unsigned int max_digits10 =
     472             :       std::floor(std::numeric_limits<Real>::digits * std::log10(2) + 2);
     473             : 
     474          14 :   std::ostringstream os;
     475          14 :   os << std::setprecision(max_digits10) << t;
     476          28 :   return os.str();
     477          14 : }
     478             : 
     479             : Point
     480           0 : toPoint(const std::vector<Real> & pos)
     481             : {
     482             :   mooseAssert(pos.size() == LIBMESH_DIM, "Wrong array size while converting into a point");
     483           0 :   return Point(pos[0], pos[1], pos[2]);
     484             : }
     485             : }

Generated by: LCOV version 1.14