Skip to content

Commit

Permalink
Change errors to use new system
Browse files Browse the repository at this point in the history
  • Loading branch information
loganharbour committed Feb 1, 2024
1 parent 5e7ce95 commit 53df3b3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 50 deletions.
4 changes: 2 additions & 2 deletions framework/include/materials/MaterialPropertyStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class MaterialPropertyStorage
{
/// Whether or not this property is stateful
bool stateful() const { return state > 0; }
/// The material names that have declared this property; used in restart when pointers are unavailable
std::set<std::pair<std::string, std::string>> declarer_type_and_names;
/// The material (type,name) that have declared this property
std::set<std::string> declarers;
/// The type of this property
std::string type;
/// The stateful id in _stroage used for this property, if any
Expand Down
62 changes: 18 additions & 44 deletions framework/src/materials/MaterialPropertyStorage.C
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ MaterialPropertyStorage::addProperty(const std::string & prop_name,
auto & record = *_prop_records[prop_id];
record.type = type.name();
if (declarer)
record.declarer_type_and_names.emplace(declarer->type(), declarer->name());
record.declarers.emplace(declarer->typeAndName());
record.state = std::max(state, record.state);

// Keep track of stateful props by quick access
Expand Down Expand Up @@ -592,44 +592,33 @@ dataLoad(std::istream & stream, MaterialPropertyStorage & storage, void * contex
decltype(storage._prop_records) from_prop_records;
dataLoad(stream, from_prop_records, nullptr);

// Common type for representing a Material; type and name
using MaterialObject = std::pair<std::string, std::string>;

{
// Build maps of material object -> properties and property -> material object
// Build maps of material object -> properties and property -> material objects
const auto build_maps = [](const auto & prop_records, const auto & ids_to_names)
{
std::map<MaterialObject, std::set<std::string>> object_to_props;
std::map<std::string, MaterialObject> prop_to_object;
std::map<std::string, std::set<std::string>> object_to_props, prop_to_objects;
for (const auto i : index_range(prop_records))
if (prop_records[i] && prop_records[i]->stateful())
{
const auto & record = *prop_records[i];
MaterialObject object;
if (const auto material_ptr = std::get_if<const MaterialBase *>(&record.declarer))
object = std::make_pair((*material_ptr)->type(), (*material_ptr)->name());
else if (const auto material_name = std::get_if<MaterialObject>(&record.declarer))
object = *material_name;
const auto & name = ids_to_names[i];
object_to_props[object].insert(name);
prop_to_object.emplace(name, object);
const auto & prop = ids_to_names[i];
for (const auto & declarer : (*prop_records[i]).declarers)
{
object_to_props[declarer].insert(prop);
prop_to_objects[prop].insert(declarer);
}
}

return std::make_pair(object_to_props, prop_to_object);
return std::make_pair(std::move(object_to_props), std::move(prop_to_objects));
};
// Maps for the current stateful properties
const std::vector<std::string> prop_ids_to_names(registry.idsToNamesBegin(),
registry.idsToNamesEnd());
const auto [object_to_props, prop_to_object] =
const auto [object_to_props, prop_to_objects] =
build_maps(storage._prop_records, prop_ids_to_names);
// Maps for the stored stateful properties
const auto [from_object_to_props, from_prop_to_object] =
const auto [from_object_to_props, from_prop_to_objects] =
build_maps(from_prop_records, from_prop_ids_to_names);

// Helper for printing object names in errors
const auto object_string = [](const MaterialObject & object)
{ return object.first + " '" + object.second + "'"; };

// Enforce our stateful requirements
for (const auto & [object, props] : object_to_props)
{
Expand All @@ -645,7 +634,7 @@ dataLoad(std::istream & stream, MaterialPropertyStorage & storage, void * contex
if (props != from_props)
{
std::stringstream error;
error << "The stateful material properties in " << object_string(object)
error << "The stateful material properties in " << object
<< " that are being restarted do not match the stored properties in the same "
"material object from the checkpoint.\n\n";
error << "Checkpointed stateful properties:\n";
Expand All @@ -662,28 +651,13 @@ dataLoad(std::istream & stream, MaterialPropertyStorage & storage, void * contex
else if (storage._recovering)
{
mooseError("The ",
object_string(object),
object,
" was stored in restart but no longer exists. This is not supported when "
"recovering stateful material properties.");
}
// We have not found a material object that was stored with the same name
// with stateful material properties. Here, we enforce that no other new
// stateful material properties are declared in a new material with the
// same name to avoid ambiguity.
else
{
for (const auto & prop : props)
if (const auto find = from_prop_to_object.find(prop);
from_prop_to_object.find(prop) != from_prop_to_object.end())
mooseError("The stateful material property '",
prop,
"' was stored in checkpoint in ",
object_string(find->second),
" but is now declared in ",
object_string(object),
".");
}
}

// Need to deal with new materials with a property that we previously stored
}

std::vector<std::optional<unsigned int>> to_stateful_ids(storage.statefulProps().size());
Expand Down Expand Up @@ -815,7 +789,7 @@ dataLoad(std::istream & stream, MaterialPropertyStorage & storage, void * contex
void
dataStore(std::ostream & stream, MaterialPropertyStorage::PropRecord & record, void *)
{
dataStore(stream, record.declarer_type_and_names, nullptr);
dataStore(stream, record.declarers, nullptr);
dataStore(stream, record.type, nullptr);
dataStore(stream, record.stateful_id, nullptr);
dataStore(stream, record.state, nullptr);
Expand All @@ -824,7 +798,7 @@ dataStore(std::ostream & stream, MaterialPropertyStorage::PropRecord & record, v
void
dataLoad(std::istream & stream, MaterialPropertyStorage::PropRecord & record, void *)
{
dataLoad(stream, record.declarer_type_and_names, nullptr);
dataLoad(stream, record.declarers, nullptr);
dataLoad(stream, record.type, nullptr);
dataLoad(stream, record.stateful_id, nullptr);
dataLoad(stream, record.state, nullptr);
Expand Down
9 changes: 5 additions & 4 deletions test/tests/restart/advanced_stateful_material/tests
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
input = 'advanced_stateful_material_restart.i'
cli_args = "Materials/test/real_names='a'
Materials/test/real_stateful_names='a'"
expect_err = 'The stateful material properties in RestartStatefulMaterial \'test\' that are being restarted do not match the stored properties in the same material object from the checkpoint.'
expect_err = 'The stateful material properties in RestartStatefulMaterial "test" that are being restarted do not match the stored properties in the same material object from the checkpoint.'
prereq = checkpoint
detail = 'a stateful property for a single material object is removed'
[]
Expand All @@ -25,18 +25,19 @@
input = 'advanced_stateful_material_restart.i'
cli_args = "Materials/test/real_names='a b c'
Materials/test/real_stateful_names='a b c'"
expect_err = 'The stateful material properties in RestartStatefulMaterial \'test\' that are being restarted do not match the stored properties in the same material object from the checkpoint.'
expect_err = 'The stateful material properties in RestartStatefulMaterial "test" that are being restarted do not match the stored properties in the same material object from the checkpoint.'
prereq = checkpoint
detail = 'a stateful property for a single material object is added'
[]
# not sure if this test is valid anymore
[other_object]
type = RunException
type = RunApp
input = 'advanced_stateful_material_restart.i'
cli_args = "Materials/active='new'
Materials/new/type=RestartStatefulMaterial
Materials/new/real_names='a'
Materials/new/real_stateful_names='a'"
expect_err = 'The stateful material property \'a\' was stored in checkpoint in RestartStatefulMaterial \'test\' but is now declared in RestartStatefulMaterial \'new\'.'
# expect_err = 'The stateful material property \'a\' was stored in checkpoint in RestartStatefulMaterial "test" but is now declared in RestartStatefulMaterial \'new\'.'
prereq = checkpoint
detail = 'a stateful object changed names'
[]
Expand Down

0 comments on commit 53df3b3

Please sign in to comment.