LCOV - code coverage report
Current view: top level - src/utils - Conversion.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 201 260 77.3 %
Date: 2025-07-17 01:28:37 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       60416 : initCoordinateSystemType()
      38             : {
      39       60416 :   if (coordinate_system_type_to_enum.empty())
      40             :   {
      41       49571 :     coordinate_system_type_to_enum["XYZ"] = COORD_XYZ;
      42       49571 :     coordinate_system_type_to_enum["RZ"] = COORD_RZ;
      43       49571 :     coordinate_system_type_to_enum["RSPHERICAL"] = COORD_RSPHERICAL;
      44             :   }
      45       60416 : }
      46             : 
      47             : void
      48       35337 : initSolveType()
      49             : {
      50       35337 :   if (solve_type_to_enum.empty())
      51             :   {
      52       28968 :     solve_type_to_enum["PJFNK"] = ST_PJFNK;
      53       28968 :     solve_type_to_enum["JFNK"] = ST_JFNK;
      54       28968 :     solve_type_to_enum["NEWTON"] = ST_NEWTON;
      55       28968 :     solve_type_to_enum["FD"] = ST_FD;
      56       28968 :     solve_type_to_enum["LINEAR"] = ST_LINEAR;
      57             :   }
      58       35337 : }
      59             : 
      60             : void
      61         562 : initEigenSolveType()
      62             : {
      63         562 :   if (eigen_solve_type_to_enum.empty())
      64             :   {
      65         562 :     eigen_solve_type_to_enum["POWER"] = EST_POWER;
      66         562 :     eigen_solve_type_to_enum["ARNOLDI"] = EST_ARNOLDI;
      67         562 :     eigen_solve_type_to_enum["KRYLOVSCHUR"] = EST_KRYLOVSCHUR;
      68         562 :     eigen_solve_type_to_enum["JACOBI_DAVIDSON"] = EST_JACOBI_DAVIDSON;
      69         562 :     eigen_solve_type_to_enum["NONLINEAR_POWER"] = EST_NONLINEAR_POWER;
      70         562 :     eigen_solve_type_to_enum["NEWTON"] = EST_NEWTON;
      71         562 :     eigen_solve_type_to_enum["PJFNK"] = EST_PJFNK;
      72         562 :     eigen_solve_type_to_enum["PJFNKMO"] = EST_PJFNKMO;
      73         562 :     eigen_solve_type_to_enum["JFNK"] = EST_JFNK;
      74             :   }
      75         562 : }
      76             : 
      77             : void
      78         562 : initEigenProlemType()
      79             : {
      80         562 :   if (eigen_problem_type_to_enum.empty())
      81             :   {
      82         562 :     eigen_problem_type_to_enum["HERMITIAN"] = EPT_HERMITIAN;
      83         562 :     eigen_problem_type_to_enum["NON_HERMITIAN"] = EPT_NON_HERMITIAN;
      84         562 :     eigen_problem_type_to_enum["GEN_HERMITIAN"] = EPT_GEN_HERMITIAN;
      85         562 :     eigen_problem_type_to_enum["GEN_NON_HERMITIAN"] = EPT_GEN_NON_HERMITIAN;
      86         562 :     eigen_problem_type_to_enum["GEN_INDEFINITE"] = EPT_GEN_INDEFINITE;
      87         562 :     eigen_problem_type_to_enum["POS_GEN_NON_HERMITIAN"] = EPT_POS_GEN_NON_HERMITIAN;
      88         562 :     eigen_problem_type_to_enum["SLEPC_DEFAULT"] = EPT_SLEPC_DEFAULT;
      89             :   }
      90         562 : }
      91             : 
      92             : void
      93          72 : initWhichEigenPairs()
      94             : {
      95          72 :   if (which_eigen_pairs_to_enum.empty())
      96             :   {
      97          72 :     which_eigen_pairs_to_enum["LARGEST_MAGNITUDE"] = WEP_LARGEST_MAGNITUDE;
      98          72 :     which_eigen_pairs_to_enum["SMALLEST_MAGNITUDE"] = WEP_SMALLEST_MAGNITUDE;
      99          72 :     which_eigen_pairs_to_enum["LARGEST_REAL"] = WEP_LARGEST_REAL;
     100          72 :     which_eigen_pairs_to_enum["SMALLEST_REAL"] = WEP_SMALLEST_REAL;
     101          72 :     which_eigen_pairs_to_enum["LARGEST_IMAGINARY"] = WEP_LARGEST_IMAGINARY;
     102          72 :     which_eigen_pairs_to_enum["SMALLEST_IMAGINARY"] = WEP_SMALLEST_IMAGINARY;
     103          72 :     which_eigen_pairs_to_enum["TARGET_MAGNITUDE"] = WEP_TARGET_MAGNITUDE;
     104          72 :     which_eigen_pairs_to_enum["TARGET_REAL"] = WEP_TARGET_REAL;
     105          72 :     which_eigen_pairs_to_enum["TARGET_IMAGINARY"] = WEP_TARGET_IMAGINARY;
     106          72 :     which_eigen_pairs_to_enum["ALL_EIGENVALUES"] = WEP_ALL_EIGENVALUES;
     107          72 :     which_eigen_pairs_to_enum["SLEPC_DEFAULT"] = WEP_SLEPC_DEFAULT;
     108             :   }
     109          72 : }
     110             : 
     111             : void
     112       56744 : initLineSearchType()
     113             : {
     114       56744 :   if (line_search_type_to_enum.empty())
     115             :   {
     116       45062 :     line_search_type_to_enum["DEFAULT"] = LS_DEFAULT;
     117       45062 :     line_search_type_to_enum["NONE"] = LS_NONE;
     118       45062 :     line_search_type_to_enum["BASIC"] = LS_BASIC;
     119             : 
     120       45062 :     line_search_type_to_enum["SHELL"] = LS_SHELL;
     121       45062 :     line_search_type_to_enum["L2"] = LS_L2;
     122       45062 :     line_search_type_to_enum["BT"] = LS_BT;
     123       45062 :     line_search_type_to_enum["CP"] = LS_CP;
     124       45062 :     line_search_type_to_enum["CONTACT"] = LS_CONTACT;
     125       45062 :     line_search_type_to_enum["PROJECT"] = LS_PROJECT;
     126             :   }
     127       56744 : }
     128             : 
     129             : void
     130          66 : initTimeIntegratorsType()
     131             : {
     132          66 :   if (time_integrator_to_enum.empty())
     133             :   {
     134          24 :     time_integrator_to_enum["IMPLICIT_EULER"] = TI_IMPLICIT_EULER;
     135          24 :     time_integrator_to_enum["EXPLICIT_EULER"] = TI_EXPLICIT_EULER;
     136          24 :     time_integrator_to_enum["CRANK_NICOLSON"] = TI_CRANK_NICOLSON;
     137          24 :     time_integrator_to_enum["BDF2"] = TI_BDF2;
     138          24 :     time_integrator_to_enum["EXPLICIT_MIDPOINT"] = TI_EXPLICIT_MIDPOINT;
     139          24 :     time_integrator_to_enum["LSTABLE_DIRK2"] = TI_LSTABLE_DIRK2;
     140          24 :     time_integrator_to_enum["EXPLICIT_TVDRK2"] = TI_EXPLICIT_TVD_RK_2;
     141             :   }
     142          66 : }
     143             : 
     144             : void
     145       70417 : initMffdType()
     146             : {
     147       70417 :   if (mffd_type_to_enum.empty())
     148             :   {
     149       45062 :     mffd_type_to_enum["DS"] = MFFD_DS;
     150       45062 :     mffd_type_to_enum["WP"] = MFFD_WP;
     151             :   }
     152       70417 : }
     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       61962 : stringToEnum<QuadratureType>(const std::string & s)
     169             : {
     170       61962 :   return Utility::string_to_enum<QuadratureType>("Q" + s);
     171             : }
     172             : 
     173             : template <>
     174             : Order
     175      185964 : stringToEnum<Order>(const std::string & s)
     176             : {
     177      185964 :   std::string upper(s);
     178      185964 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     179             : 
     180      185964 :   if (upper.compare("AUTO") == 0)
     181      185398 :     return INVALID_ORDER;
     182             :   else
     183         566 :     return Utility::string_to_enum<Order>(upper);
     184      185964 : }
     185             : 
     186             : template <>
     187             : CoordinateSystemType
     188       60416 : stringToEnum<CoordinateSystemType>(const std::string & s)
     189             : {
     190       60416 :   initCoordinateSystemType();
     191             : 
     192       60416 :   std::string upper(s);
     193       60416 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     194             : 
     195       60416 :   if (!coordinate_system_type_to_enum.count(upper))
     196           0 :     mooseError("Unknown coordinate system type: ", upper);
     197             : 
     198      120832 :   return coordinate_system_type_to_enum[upper];
     199       60416 : }
     200             : 
     201             : template <>
     202             : SolveType
     203       35337 : stringToEnum<SolveType>(const std::string & s)
     204             : {
     205       35337 :   initSolveType();
     206             : 
     207       35337 :   std::string upper(s);
     208       35337 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     209             : 
     210       35337 :   if (!solve_type_to_enum.count(upper))
     211           0 :     mooseError("Unknown solve type: ", upper);
     212             : 
     213       70674 :   return solve_type_to_enum[upper];
     214       35337 : }
     215             : 
     216             : template <>
     217             : EigenSolveType
     218         562 : stringToEnum<EigenSolveType>(const std::string & s)
     219             : {
     220         562 :   initEigenSolveType();
     221             : 
     222         562 :   std::string upper(s);
     223         562 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     224             : 
     225         562 :   if (!eigen_solve_type_to_enum.count(upper))
     226           0 :     mooseError("Unknown eigen solve type: ", upper);
     227             : 
     228        1124 :   return eigen_solve_type_to_enum[upper];
     229         562 : }
     230             : 
     231             : template <>
     232             : EigenProblemType
     233         562 : stringToEnum<EigenProblemType>(const std::string & s)
     234             : {
     235         562 :   initEigenProlemType();
     236             : 
     237         562 :   std::string upper(s);
     238         562 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     239             : 
     240         562 :   if (!eigen_problem_type_to_enum.count(upper))
     241           0 :     mooseError("Unknown eigen problem type: ", upper);
     242             : 
     243        1124 :   return eigen_problem_type_to_enum[upper];
     244         562 : }
     245             : 
     246             : template <>
     247             : WhichEigenPairs
     248          72 : stringToEnum<WhichEigenPairs>(const std::string & s)
     249             : {
     250          72 :   initWhichEigenPairs();
     251             : 
     252          72 :   std::string upper(s);
     253          72 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     254             : 
     255          72 :   if (!which_eigen_pairs_to_enum.count(upper))
     256           0 :     mooseError("Unknown type of WhichEigenPairs: ", upper);
     257             : 
     258         144 :   return which_eigen_pairs_to_enum[upper];
     259          72 : }
     260             : 
     261             : template <>
     262             : LineSearchType
     263       56744 : stringToEnum<LineSearchType>(const std::string & s)
     264             : {
     265       56744 :   initLineSearchType();
     266             : 
     267       56744 :   std::string upper(s);
     268       56744 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     269             : 
     270       56744 :   if (!line_search_type_to_enum.count(upper))
     271           0 :     mooseError("Unknown line search type: ", upper);
     272             : 
     273      113488 :   return line_search_type_to_enum[upper];
     274       56744 : }
     275             : 
     276             : template <>
     277             : TimeIntegratorType
     278          66 : stringToEnum<TimeIntegratorType>(const std::string & s)
     279             : {
     280          66 :   initTimeIntegratorsType();
     281             : 
     282          66 :   std::string upper(s);
     283          66 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     284             : 
     285          66 :   if (!time_integrator_to_enum.count(upper))
     286           0 :     mooseError("Unknown time integrator: ", upper);
     287             : 
     288         132 :   return time_integrator_to_enum[upper];
     289          66 : }
     290             : 
     291             : template <>
     292             : MffdType
     293       70417 : stringToEnum<MffdType>(const std::string & s)
     294             : {
     295       70417 :   initMffdType();
     296             : 
     297       70417 :   std::string upper(s);
     298       70417 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
     299             : 
     300       70417 :   if (!mffd_type_to_enum.count(upper))
     301           0 :     mooseError("Unknown mffd type: ", upper);
     302             : 
     303      140834 :   return mffd_type_to_enum[upper];
     304       70417 : }
     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      547872 : 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      547872 :   if (t == RelationshipManagerType::DEFAULT)
     331           0 :     return "DEFAULT";
     332      547872 :   if (t == RelationshipManagerType::GEOMETRIC)
     333      113668 :     return "GEOMETRIC";
     334      434204 :   if (t == RelationshipManagerType::ALGEBRAIC)
     335       13934 :     return "ALGEBRAIC";
     336      420270 :   if (t == (RelationshipManagerType::GEOMETRIC | RelationshipManagerType::ALGEBRAIC))
     337      118590 :     return "GEOMETRIC and ALGEBRAIC";
     338      301680 :   if (t == (RelationshipManagerType::ALGEBRAIC | RelationshipManagerType::COUPLING))
     339           0 :     return "ALGEBRAIC and COUPLING";
     340      301680 :   if (t == (RelationshipManagerType::GEOMETRIC | RelationshipManagerType::ALGEBRAIC |
     341             :             RelationshipManagerType::COUPLING))
     342       66110 :     return "GEOMETRIC and ALGEBRAIC and COUPLING";
     343      235570 :   if (t == RelationshipManagerType::COUPLING)
     344      235570 :     return "COUPLING";
     345             : 
     346           0 :   mooseError("Unknown RelationshipManagerType");
     347             : }
     348             : 
     349             : std::string
     350          16 : stringify(libMesh::FEFamily f)
     351             : {
     352          16 :   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       50188 : stringify(const SolveType & t)
     360             : {
     361       50188 :   switch (t)
     362             :   {
     363       11099 :     case ST_NEWTON:
     364       11099 :       return "NEWTON";
     365          72 :     case ST_JFNK:
     366          72 :       return "JFNK";
     367       36908 :     case ST_PJFNK:
     368       36908 :       return "Preconditioned JFNK";
     369           1 :     case ST_FD:
     370           1 :       return "FD";
     371        2108 :     case ST_LINEAR:
     372        2108 :       return "Linear";
     373             :   }
     374           0 :   return "";
     375             : }
     376             : 
     377             : std::string
     378         542 : stringify(const EigenSolveType & t)
     379             : {
     380         542 :   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          36 :       return "KRYLOVSCHUR";
     388          24 :     case EST_JACOBI_DAVIDSON:
     389          24 :       return "Jacobi Davidson";
     390          12 :     case EST_NONLINEAR_POWER:
     391          12 :       return "Nonlinear Power";
     392          92 :     case EST_PJFNKMO:
     393          92 :       return "PJFNK with Matrix Only";
     394          34 :     case EST_NEWTON:
     395          34 :       return "Newton";
     396          40 :     case EST_JFNK:
     397          40 :       return "JFNK";
     398         304 :     case EST_PJFNK:
     399         304 :       return "Preconditioned JFNK";
     400             :   }
     401           0 :   return "";
     402             : }
     403             : 
     404             : std::string
     405          16 : stringify(const VarFieldType & t)
     406             : {
     407          16 :   switch (t)
     408             :   {
     409           8 :     case VAR_FIELD_STANDARD:
     410           8 :       return "STANDARD";
     411           8 :     case VAR_FIELD_VECTOR:
     412           8 :       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 :     default:
     433           0 :       mooseError("Unhandled SolutionIterationType");
     434             :   }
     435             : }
     436             : 
     437             : std::string
     438           0 : stringify(ElementType t)
     439             : {
     440           0 :   switch (t)
     441             :   {
     442           0 :     case ElementType::Element:
     443           0 :       return "ELEMENT";
     444           0 :     case ElementType::Neighbor:
     445           0 :       return "NEIGHBOR";
     446           0 :     case ElementType::Lower:
     447           0 :       return "LOWER";
     448           0 :     default:
     449           0 :       mooseError("unrecognized type");
     450             :   }
     451             : }
     452             : 
     453             : std::string
     454         294 : stringify(libMesh::ElemType t)
     455             : {
     456         294 :   return libMesh::Utility::enum_to_string(t);
     457             : }
     458             : 
     459             : std::string
     460     2591548 : stringify(const std::string & s)
     461             : {
     462     2591548 :   return s;
     463             : }
     464             : 
     465             : std::string
     466           7 : stringifyExact(Real t)
     467             : {
     468             :   // this or std::numeric_limits<T>::max_digits10
     469           7 :   const unsigned int max_digits10 =
     470             :       std::floor(std::numeric_limits<Real>::digits * std::log10(2) + 2);
     471             : 
     472           7 :   std::ostringstream os;
     473           7 :   os << std::setprecision(max_digits10) << t;
     474          14 :   return os.str();
     475           7 : }
     476             : 
     477             : Point
     478           0 : toPoint(const std::vector<Real> & pos)
     479             : {
     480             :   mooseAssert(pos.size() == LIBMESH_DIM, "Wrong array size while converting into a point");
     481           0 :   return Point(pos[0], pos[1], pos[2]);
     482             : }
     483             : }

Generated by: LCOV version 1.14