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 : // MOOSE includes 13 : #include "NodalUserObject.h" 14 : #include "ElementUserObject.h" 15 : 16 : // Forward declaration 17 : class ChemicalCompositionAction; 18 : 19 : template <bool is_nodal> 20 : using ThermochimicaDataBaseParent = 21 : typename std::conditional<is_nodal, NodalUserObject, ElementUserObject>::type; 22 : 23 : /** 24 : * User object that performs a Gibbs energy minimization at each node by 25 : * calling the Thermochimica code. This object can only be added through 26 : * the ChemicalCompositionAction which also sets up the variables for the 27 : * calculation. 28 : */ 29 : template <bool is_nodal> 30 : class ThermochimicaDataBase : public ThermochimicaDataBaseParent<is_nodal> 31 : { 32 : public: 33 : static InputParameters validParams(); 34 : ThermochimicaDataBase(const InputParameters & parameters); 35 : ~ThermochimicaDataBase(); 36 : 37 : virtual void initialize() override; 38 : virtual void execute() override; 39 308 : virtual void finalize() override {} 40 66 : virtual void threadJoin(const UserObject &) override {} 41 : 42 : /** 43 : * Function to get re-initialization data from Thermochimica and save 44 : * it in member variables of this UserObject. 45 : */ 46 : void reinitDataMooseFromTc(); 47 : 48 : /** 49 : * Function to load re-initialization data saved in this UserObject 50 : * back into Thermochimica. 51 : */ 52 : void reinitDataMooseToTc(); 53 : 54 : struct Data 55 : { 56 : int _reinit_available; 57 : int _elements; 58 : int _species; 59 : std::vector<int> _elements_used; 60 : std::vector<int> _assemblage; 61 : std::vector<double> _moles_phase; 62 : std::vector<double> _element_potential; 63 : std::vector<double> _chemical_potential; 64 : std::vector<double> _mol_fraction; 65 : }; 66 : 67 : const Data & getNodalData(dof_id_type id) const; 68 : const Data & getElementData(dof_id_type id) const; 69 : 70 : // universal data access 71 : const Data & getData(dof_id_type id) const; 72 : 73 : protected: 74 : // child process routine to dispatch thermochimica calculations 75 : void server(); 76 : 77 : // helper to wait on a socket read 78 : template <typename T> 79 : void expect(T expect_msg); 80 : 81 : // send message to socket 82 : template <typename T> 83 : void notify(T send_msg); 84 : 85 : const VariableValue & _pressure; 86 : const VariableValue & _temperature; 87 : 88 : const std::size_t _n_phases; 89 : const std::size_t _n_species; 90 : const std::size_t _n_elements; 91 : const std::size_t _n_vapor_species; 92 : const std::size_t _n_phase_elements; 93 : const std::size_t _n_potentials; 94 : 95 : std::vector<const VariableValue *> _el; 96 : 97 : const ChemicalCompositionAction & _action; 98 : std::vector<unsigned int> _el_ids; 99 : 100 : // re-initialization data 101 : const enum class ReinitializationType { NONE, TIME, NODE } _reinit; 102 : 103 : const std::vector<std::string> & _ph_names; 104 : const std::vector<std::string> & _element_potentials; 105 : const std::vector<std::pair<std::string, std::string>> & _species_phase_pairs; 106 : const std::vector<std::pair<std::string, std::string>> & _vapor_phase_pairs; 107 : const std::vector<std::pair<std::string, std::string>> & _phase_element_pairs; 108 : 109 : /// Nodal data (Used only for reinitialization) 110 : std::unordered_map<dof_id_type, Data> _data; 111 : 112 : ///@{ Element chemical potential output 113 : const bool _output_element_potentials; 114 : const bool _output_vapor_pressures; 115 : const bool _output_element_phases; 116 : ///@} 117 : 118 : /// Writable phase amount variables 119 : std::vector<MooseWritableVariable *> _ph; 120 : 121 : /// Writable species amount variables 122 : std::vector<MooseWritableVariable *> _sp; 123 : 124 : /// Writable vapour pressures for each element 125 : std::vector<MooseWritableVariable *> _vp; 126 : 127 : /// Writable chemical potential variables for each element 128 : std::vector<MooseWritableVariable *> _el_pot; 129 : 130 : /// Writable variable for molar amounts of each element in specified phase 131 : std::vector<MooseWritableVariable *> _el_ph; 132 : 133 : /// Mass unit for output species 134 : const enum class OutputMassUnit { MOLES, FRACTION } _output_mass_unit; 135 : 136 : /// communication socket 137 : int _socket; 138 : 139 : /// child PID 140 : pid_t _pid; 141 : 142 : /// shared memory pointer for dof_id_type values 143 : dof_id_type * _shared_dofid_mem; 144 : 145 : /// shared memory pointer for Real values 146 : Real * _shared_real_mem; 147 : 148 : // current node or element ID 149 : dof_id_type _current_id; 150 : 151 : using ThermochimicaDataBaseParent<is_nodal>::isCoupled; 152 : using ThermochimicaDataBaseParent<is_nodal>::isParamValid; 153 : using ThermochimicaDataBaseParent<is_nodal>::coupledValue; 154 : using ThermochimicaDataBaseParent<is_nodal>::coupledComponents; 155 : using ThermochimicaDataBaseParent<is_nodal>::writableVariable; 156 : }; 157 : 158 : typedef ThermochimicaDataBase<true> ThermochimicaNodalData; 159 : typedef ThermochimicaDataBase<false> ThermochimicaElementData;