bool BlockRestrictable::isBlockSubset(std::vector<SubdomainID> ids) const { std::set<SubdomainID> ids_set(ids.begin(), ids.end()); return isBlockSubset(ids_set); }
void BlockRestrictable::initializeBlockRestrictable(const InputParameters & parameters) { mooseAssert(!_initialized, "BlockRestrictable already initialized"); // The name and id of the object const std::string name = parameters.get<std::string>("_object_name"); // If the mesh pointer is not defined, but FEProblem is, get it from there if (_blk_feproblem != NULL && _blk_mesh == NULL) _blk_mesh = &_blk_feproblem->mesh(); // Check that the mesh pointer was defined, it is required for this class to operate if (_blk_mesh == NULL) mooseError("The input parameters must contain a pointer to FEProblem via '_fe_problem' or a pointer to the MooseMesh via '_mesh'"); // Populate the MaterialData pointer if (_blk_feproblem != NULL) _blk_material_data = _blk_feproblem->getMaterialData(Moose::BLOCK_MATERIAL_DATA, _blk_tid); /** * The _initialized value needs to be set early so that calls to helper functions during * initialization may succeed. */ _initialized = true; // The 'block' input is defined if (parameters.isParamValid("block")) { // Extract the blocks from the input _blocks = parameters.get<std::vector<SubdomainName> >("block"); // Get the IDs from the supplied names std::vector<SubdomainID> vec_ids = _blk_mesh->getSubdomainIDs(_blocks); // Store the IDs, handling ANY_BLOCK_ID if supplied if (std::find(_blocks.begin(), _blocks.end(), "ANY_BLOCK_ID") != _blocks.end()) _blk_ids.insert(Moose::ANY_BLOCK_ID); else _blk_ids.insert(vec_ids.begin(), vec_ids.end()); // Check that supplied blocks are within the variable domain if (parameters.isParamValid("variable") && (parameters.have_parameter<NonlinearVariableName>("variable") || parameters.have_parameter<AuxVariableName>("variable"))) { // A pointer to the variable class std::set<SubdomainID> var_ids = variableSubdomainIDs(parameters); // Test if the variable blockIDs are valid for this object if (!isBlockSubset(var_ids)) mooseError("In object " << name << " the defined blocks are outside of the domain of the variable"); } } // The 'block' input parameter is undefined, if the object contains a variable, set the subdomain ids to those of the variable else if (parameters.isParamValid("variable") && (parameters.have_parameter<NonlinearVariableName>("variable") || parameters.have_parameter<AuxVariableName>("variable"))) _blk_ids = variableSubdomainIDs(parameters); // Produce error if the object is not allowed to be both block and boundary restricted if (!_blk_dual_restrictable && !_boundary_ids.empty() && !_boundary_ids.empty()) if (!_boundary_ids.empty() && _boundary_ids.find(Moose::ANY_BOUNDARY_ID) == _boundary_ids.end()) mooseError("Attempted to restrict the object '" << name << "' to a block, but the object is already restricted by boundary"); // If no blocks were defined above, specify that it is valid on all blocks if (_blk_ids.empty()) { _blk_ids.insert(Moose::ANY_BLOCK_ID); _blocks = {"ANY_BLOCK_ID"}; } // If this object is block restricted, check that defined blocks exist on the mesh if (_blk_ids.find(Moose::ANY_BLOCK_ID) == _blk_ids.end()) { const std::set<SubdomainID> & valid_ids = _blk_mesh->meshSubdomains(); std::vector<SubdomainID> diff; std::set_difference(_blk_ids.begin(), _blk_ids.end(), valid_ids.begin(), valid_ids.end(), std::back_inserter(diff)); if (!diff.empty()) { std::ostringstream msg; msg << "The object '" << name << "' contains the following block ids that do no exist on the mesh:"; for (const auto & id : diff) msg << " " << id; mooseError(msg.str()); } } }
BlkResTestDiffusion::BlkResTestDiffusion(const InputParameters & parameters) : Kernel(modifyParams(parameters)) { // Get an test enum from the kernel parameters MooseEnum test = parameters.get<MooseEnum>("test"); // test that hasBlocks is working if (test == "hasBlocks") { // Define a SubdomainName vector for testing against std::vector<SubdomainName> id_names(2); id_names[0] = "1"; id_names[1] = "2"; // Define a SubdomainID vector for testing against std::vector<SubdomainID> ids(2); ids[0] = 1; ids[1] = 2; // Define a SubdomainID set for testing against std::set<SubdomainID> id_set(ids.begin(), ids.end()); // Test true single SudomainName input if (!hasBlocks("1")) mooseError("Test 1: hasBlocks(SubdomainName) = true failed"); // Test false of single Subdomain input if (hasBlocks("3")) mooseError("Test 2: hasBlocks(SubdomainName) = false failed"); // Test true vector SubdomainName input if (!hasBlocks(id_names)) mooseError("Test 3: hasBlocks(std::vector<SubdomainName>) = true failed"); // Test false vector SudomainName input id_names.push_back("3"); if (hasBlocks(id_names)) mooseError("Test 4: hasBlocks(std::vector<SubdomainName>) = false failed"); // Test true single SubdomainID input if (!hasBlocks(1)) mooseError("Test 5: hasBlocks(SubdomainID) = true failed"); // Test false single SubdomainID input if (hasBlocks(5)) mooseError("Test 6: hasBlocks(SubdomainID) = false failed"); // Test true for std::vector<SubdomainID> if (!hasBlocks(ids)) mooseError("Test 7: hasBlocks(std::vector<SubdomainID>) = true failed"); // Test false for std::vector<SubdomainID> ids.push_back(4); if (hasBlocks(ids)) mooseError("Test 8: hasBlocks(std::vector<SubdomainID>) = false failed"); // Test true for std::set<SubdomainID> if (!hasBlocks(id_set)) mooseError("Test 9: hasBlocks(std::set<SubdomainID) = true failed"); // Test false for std::set<SubdomainID> id_set.insert(12); if (hasBlocks(id_set)) mooseError("Test 10: hasBlocks(std::set<SubdomainID>) = false failed"); // This is the expected error, all the above tests passed mooseError("hasBlocks testing passed"); } // Test of stored ANY_BLOCK_ID on object else if (test == "hasBlocks_ANY_BLOCK_ID") { // Test that ANY_BLOCK_ID is working if (hasBlocks(1)) mooseError("hasBlocks_ANY_BLOCK_ID test passed"); else mooseError("hasBlocks_ANY_BLOCK_ID test failed"); } // Test that the blocks() method is working else if (test == "blocks") { const std::vector<SubdomainName> & blks = blocks(); if (blks[0] == "1" && blks[1] == "2" && blks.size() == 2) mooseError("Blocks testing passed"); // expected error else mooseError("Blocks testing failed"); } // Test that the getSubdomains() is working else if (test == "blockIDs") { const std::set<SubdomainID> & ids = blockIDs(); if (ids.count(1) == 1 && ids.count(2) == 1) mooseError("blockIDs testing passed"); // expected error else mooseError("blockIDs testing failed"); } // Test that the isSubset() is working else if (test == "isBlockSubset") { std::set<SubdomainID> sub_id; sub_id.insert(10); sub_id.insert(1); sub_id.insert(4); sub_id.insert(2); if (isBlockSubset(sub_id)) mooseError("isBlockSubset testing passed"); // expected error else mooseError("isBlockSubset testing failed"); } // Test that hasMaterialPropertyBlock is working properly else if (test == "hasBlockMaterialProperty_true") { if (hasBlockMaterialProperty<Real>("a")) mooseError("hasBlockMaterialProperty is true, test passed"); // expected error else mooseError("hasBlockMaterialProperty is false, test failed"); } else if (test == "hasBlockMaterialProperty_false") { if (hasBlockMaterialProperty<Real>("b")) mooseError("hasBlockMaterialProperty is true, test failed"); else mooseError("hasBlockMaterialProperty is false, test passed"); // expected error } }