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 "VariableValueElementSubdomainModifier.h" 11 : 12 : registerMooseObject("MooseApp", VariableValueElementSubdomainModifier); 13 : 14 : InputParameters 15 14307 : VariableValueElementSubdomainModifier::validParams() 16 : { 17 14307 : InputParameters params = ElementSubdomainModifier::validParams(); 18 : 19 14307 : params.addRequiredCoupledVar( 20 : "coupled_var", "Coupled variable whose value is used to calculate the desired subdomain ID."); 21 14307 : params.addClassDescription( 22 : "Modify the element subdomain ID based on the provided variable value."); 23 14307 : return params; 24 0 : } 25 : 26 22 : VariableValueElementSubdomainModifier::VariableValueElementSubdomainModifier( 27 22 : const InputParameters & parameters) 28 22 : : ElementSubdomainModifier(parameters), _v(coupledValue("coupled_var")) 29 : { 30 22 : } 31 : 32 : SubdomainID 33 3136 : VariableValueElementSubdomainModifier::computeSubdomainID() 34 : { 35 : // Calculate the desired subdomain ID for the current element. 36 3136 : Real val = 0.0; 37 6272 : for (const auto qp : make_range(_qrule->n_points())) 38 3136 : val += _v[qp] * _JxW[qp] * _coord[qp]; 39 3136 : val /= _current_elem_volume; 40 : 41 3136 : SubdomainID sid = (unsigned int)(round(val)); 42 : 43 : // Verify whether the specified target subdomain ID is present within the mesh. If it is not 44 : // found, locate the smallest subdomain ID in the mesh that matches or exceeds the target 45 : // subdomain ID. Or select the largest subdomain ID present in the mesh if all subdomain IDs are 46 : // smaller than the target. 47 3136 : if (_mesh.meshSubdomains().find(sid) == _mesh.meshSubdomains().end()) 48 : { 49 896 : auto it = _mesh.meshSubdomains().lower_bound(sid); 50 : SubdomainID lower_bound_id = 51 896 : it == _mesh.meshSubdomains().end() ? *_mesh.meshSubdomains().rbegin() : *it; 52 : 53 : // Store the target subdomain ID if it hasn't been previously requested. 54 : // Lock the _void_sids for thread-safe operations. 55 896 : std::lock_guard<std::mutex> lock(_void_sids_mutex); 56 896 : if (_void_sids.find(sid) == _void_sids.end()) 57 : { 58 11 : mooseWarning("Requested subdomain ", 59 : sid, 60 : " does not exist. Subdomain ID ", 61 : lower_bound_id, 62 : " is assigned. Please ensure the passed variable falls within the range of the " 63 : "mesh's subdomain IDs for the expected behavior."); 64 11 : _void_sids.insert(sid); 65 : } 66 896 : return lower_bound_id; 67 896 : } 68 2240 : return sid; 69 : }