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 : #include "ParsedSubdomainGeneratorBase.h" 11 : #include "Conversion.h" 12 : #include "MooseMeshUtils.h" 13 : #include "CastUniquePointer.h" 14 : 15 : #include "libmesh/fparser_ad.hh" 16 : #include "libmesh/elem.h" 17 : 18 : InputParameters 19 10793 : ParsedSubdomainGeneratorBase::validParams() 20 : { 21 10793 : InputParameters params = MeshGenerator::validParams(); 22 10793 : params += FunctionParserUtils<false>::validParams(); 23 : 24 43172 : params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify"); 25 43172 : params.addRequiredParam<std::string>( 26 : "expression", "Parsed expression to determine the subdomain id of each involved element"); 27 43172 : params.addParam<std::vector<SubdomainName>>( 28 : "excluded_subdomains", 29 : "A set of subdomain names that will be immune to change if set. This cannot be used together " 30 : "with included_subdomains."); 31 64758 : params.addDeprecatedParam<std::vector<subdomain_id_type>>( 32 : "excluded_subdomain_ids", 33 : "A set of subdomain ids that will not changed even if " 34 : "they are inside/outside the combinatorial geometry", 35 : "excluded_subdomain_ids is deprecated, use excluded_subdomains (ids or names accepted)"); 36 43172 : params.addParam<std::vector<SubdomainName>>( 37 : "included_subdomains", 38 : "A set of subdomain names that will only be subjected to change if set. This cannot be used " 39 : "together with excluded_subdomains."); 40 43172 : params.addParam<std::vector<std::string>>( 41 : "constant_names", {}, "Vector of constants used in the parsed function"); 42 43172 : params.addParam<std::vector<std::string>>( 43 : "constant_expressions", 44 : {}, 45 : "Vector of values for the constants in constant_names (can be an FParser expression)"); 46 43172 : params.addParam<std::vector<ExtraElementIDName>>( 47 : "extra_element_id_names", {}, "Extra element integers used in the parsed expression"); 48 10793 : params.addClassDescription("A base class for mesh generators that Use a parsed expression to " 49 : "assign new subdomain id(s)."); 50 : 51 10793 : return params; 52 0 : } 53 : 54 2337 : ParsedSubdomainGeneratorBase::ParsedSubdomainGeneratorBase(const InputParameters & parameters) 55 : : MeshGenerator(parameters), 56 : FunctionParserUtils<false>(parameters), 57 2337 : _input(getMesh("input")), 58 2337 : _function(parameters.get<std::string>("expression")), 59 7011 : _excluded_ids(isParamValid("excluded_subdomain_ids") 60 2337 : ? parameters.get<std::vector<subdomain_id_type>>("excluded_subdomain_ids") 61 : : std::vector<subdomain_id_type>()), 62 9348 : _eeid_names(getParam<std::vector<ExtraElementIDName>>("extra_element_id_names")) 63 : { 64 14006 : if ((isParamValid("excluded_subdomains") || isParamValid("excluded_subdomain_ids")) && 65 2385 : (isParamValid("included_subdomains"))) 66 6 : paramError("excluded_subdomains", 67 : "You cannot use both excluded_subdomains and included_subdomains at the same time."); 68 : 69 2334 : functionInitialize(_function); 70 2334 : } 71 : 72 : std::unique_ptr<MeshBase> 73 2280 : ParsedSubdomainGeneratorBase::generate() 74 : { 75 2280 : std::unique_ptr<MeshBase> mesh = std::move(_input); 76 : 77 : // Make sure subdomain caches are up to date 78 2280 : if (!mesh->preparation().has_cached_elem_data) 79 2156 : mesh->cache_elem_data(); 80 : 81 : // the extra element ids would not have existed at construction so we only do this now 82 2300 : for (const auto & eeid_name : _eeid_names) 83 20 : _eeid_indices.push_back(mesh->get_elem_integer_index(eeid_name)); 84 : 85 6840 : if (isParamValid("excluded_subdomains")) 86 : { 87 13 : auto excluded_subdomains = parameters().get<std::vector<SubdomainName>>("excluded_subdomains"); 88 : 89 : // check that the subdomains exist in the mesh 90 23 : for (const auto & name : excluded_subdomains) 91 13 : if (!MooseMeshUtils::hasSubdomainName(*mesh, name)) 92 6 : paramError("excluded_subdomains", "The block '", name, "' was not found in the mesh"); 93 : 94 10 : _excluded_ids = MooseMeshUtils::getSubdomainIDs(*mesh, excluded_subdomains); 95 10 : } 96 6801 : else if (isParamValid("included_subdomains")) 97 : { 98 13 : auto included_subdomains = parameters().get<std::vector<SubdomainName>>("included_subdomains"); 99 : 100 : // check that the subdomains exist in the mesh 101 33 : for (const auto & name : included_subdomains) 102 23 : if (!MooseMeshUtils::hasSubdomainName(*mesh, name)) 103 6 : paramError("included_subdomains", "The block '", name, "' was not found in the mesh"); 104 : 105 10 : auto included_ids = MooseMeshUtils::getSubdomainIDs(*mesh, included_subdomains); 106 : 107 : // collect all subdomain ids in the mesh 108 10 : std::set<subdomain_id_type> mesh_sids; 109 10 : mesh->subdomain_ids(mesh_sids); 110 : 111 : // set excluded ids to all mesh sids that are not in included ids 112 40 : for (const auto & sid : mesh_sids) 113 30 : if (std::find(included_ids.begin(), included_ids.end(), sid) == included_ids.end()) 114 10 : _excluded_ids.push_back(sid); 115 10 : } 116 : 117 : // Loop over the elements 118 710769 : for (const auto & elem : mesh->active_element_ptr_range()) 119 710769 : assignElemSubdomainID(elem); 120 : 121 : // Assign block name, if applicable 122 2271 : setBlockName(mesh); 123 : 124 2271 : mesh->unset_is_prepared(); 125 4542 : return dynamic_pointer_cast<MeshBase>(mesh); 126 2271 : } 127 : 128 : void 129 2334 : ParsedSubdomainGeneratorBase::functionInitialize(const std::string & function_expression) 130 : { 131 : // add the extra element integers 132 2334 : std::string symbol_str = "x,y,z"; 133 2354 : for (const auto & eeid_name : _eeid_names) 134 20 : symbol_str += "," + eeid_name; 135 : 136 : // Create parsed function 137 2334 : _func_F = std::make_shared<SymFunction>(); 138 9336 : parsedFunctionSetup(_func_F, 139 : function_expression, 140 : symbol_str, 141 : getParam<std::vector<std::string>>("constant_names"), 142 : getParam<std::vector<std::string>>("constant_expressions"), 143 : comm()); 144 : 145 2334 : _func_params.resize(3 + _eeid_names.size()); 146 2334 : }