645{
647
649
650 std::vector<std::string> from_prop_ids_to_names;
651 dataLoad(stream, from_prop_ids_to_names,
nullptr);
652
654 dataLoad(stream, from_stateful_prop_id_to_prop_id,
nullptr);
655
657 dataLoad(stream, from_prop_records,
nullptr);
658
659 decltype(storage.
numStates()) num_states;
660 dataLoad(stream, num_states,
nullptr);
661
662 {
663
664 const auto build_maps = [](const auto & prop_records, const auto & ids_to_names)
665 {
666 std::map<std::string, std::set<std::string>> object_to_props, prop_to_objects;
668 if (prop_records[i] && prop_records[i]->stateful())
669 {
670 const auto & prop = ids_to_names[i];
671 for (const auto & declarer : (*prop_records[i]).declarers)
672 {
673 object_to_props[declarer].insert(prop);
674 prop_to_objects[prop].insert(declarer);
675 }
676 }
677
678 return std::make_pair(std::move(object_to_props), std::move(prop_to_objects));
679 };
680
681 const std::vector<std::string> prop_ids_to_names(registry.idsToNamesBegin(),
682 registry.idsToNamesEnd());
683 const auto [object_to_props, prop_to_objects] =
685
686 const auto [from_object_to_props, from_prop_to_objects] =
687 build_maps(from_prop_records, from_prop_ids_to_names);
688
689
690 for (const auto & [object, props] : object_to_props)
691 {
692 const auto find_from_object = from_object_to_props.find(object);
693
694
695
696
697
698 if (find_from_object != from_object_to_props.end())
699 {
700 const auto & from_props = find_from_object->second;
701 if (props != from_props)
702 {
703 std::stringstream error;
704 error << "The stateful material properties in " << object
705 << " that are being restarted do not match the stored properties in the same "
706 "material object from the checkpoint.\n\n";
707 error << "Checkpointed stateful properties:\n";
708 for (const auto & prop : from_props)
709 error << " - " << prop << "\n";
710 error << "\nCurrent stateful properties:\n";
711 for (const auto & prop : props)
712 error << " - " << prop << "\n";
714 }
715 }
716
717
719 {
721 object,
722 " was stored in restart but no longer exists. This is not supported when "
723 "recovering stateful material properties.");
724 }
725 }
726
727
728
729
730
731
732 for (const auto & [from_prop, from_objects] : from_prop_to_objects)
733 if (const auto find_objects = prop_to_objects.
find(from_prop);
734 find_objects != prop_to_objects.end())
735 for (const auto & object : find_objects->second)
736 if (!from_objects.
count(object))
738 "The stateful material property '",
739 from_prop,
740 "' was declared in ",
741 object,
742 " but was not declared in that object on checkpoint.\n\nThis is not currently "
743 "supported due to ambiguity.\n\nPlease contact the development team on "
744 "GitHub if you desire this capability.");
745 }
746
747 std::vector<std::optional<unsigned int>> to_stateful_ids(from_stateful_prop_id_to_prop_id.size());
748
750
751
752 for (auto & record_ptr : to_prop_records)
754 record_ptr->restored = false;
755
756
757 for (
const auto from_stateful_id :
index_range(from_stateful_prop_id_to_prop_id))
758 {
759 const auto from_prop_id = from_stateful_prop_id_to_prop_id[from_stateful_id];
760
761 mooseAssert(from_prop_id < from_prop_records.size(), "Invalid record map");
762 mooseAssert(from_prop_records[from_prop_id], "Not set");
763 const auto & from_record = *from_prop_records[from_prop_id];
764 mooseAssert(from_record.stateful(), "Not stateful");
765
766 mooseAssert(from_prop_id < from_prop_ids_to_names.size(), "Invalid ID map");
767 const auto &
name = from_prop_ids_to_names[from_prop_id];
768
769 if (const auto query_to_prop_id = registry.queryID(name))
770 {
771 const auto to_prop_id = *query_to_prop_id;
772
773 mooseAssert(to_prop_id < to_prop_records.size(), "Invalid record map");
774 mooseAssert(to_prop_records[to_prop_id], "Not set");
775 auto & to_record = *to_prop_records[to_prop_id];
776
777 if (to_record.stateful())
778 {
779 if (from_record.type != to_record.type)
781 "The type for the restarted stateful material property '", name, "' does not match");
782
783
784
785
786 if (from_record.state != to_record.state)
787 mooseError(
"The number of states for the restarted stateful material property '",
788 name,
789 "' do not match.\n\n",
790 "Checkpointed states: ",
791 from_record.state,
792 "\nCurrent states: ",
793 to_record.state);
794
796 mooseAssert(to_stateful_id != invalid_uint, "Not stateful");
797
798 to_stateful_ids[from_stateful_id] = to_stateful_id;
799
800
801 to_record.restored = true;
803
805 mooseAssert(from_stateful_id == to_stateful_id, "Does not have direct mapping");
806 }
807 }
808 }
809
810
811 for (
const auto state :
make_range(num_states))
812 {
813 std::size_t num_elems;
814 dataLoad(stream, num_elems,
nullptr);
815
816 for (std::size_t i_elem = 0; i_elem < num_elems; ++i_elem)
817 {
818 const Elem * elem;
820 mooseAssert(elem, "Null element");
821
822 std::size_t num_sides;
823 dataLoad(stream, num_sides,
nullptr);
824
825 for (std::size_t i_side = 0; i_side < num_sides; ++i_side)
826 {
827 unsigned int side;
829
830 std::size_t num_props;
831 dataLoad(stream, num_props,
nullptr);
832 mooseAssert(num_props <= to_stateful_ids.size(), "Missized map");
833
834 std::size_t num_q_points;
835 dataLoad(stream, num_q_points,
nullptr);
836
837
838 MaterialPropertyStorage::RestartableMapType::mapped_type * restart_entry = nullptr;
840
841
842 for (
const auto from_stateful_id :
make_range(num_props))
843 {
844
845
846 std::stringstream data;
848 data.seekg(0, std::ios::beg);
849
850
851 if (const auto to_stateful_id_ptr = to_stateful_ids[from_stateful_id])
852 {
853 const auto to_stateful_id = *to_stateful_id_ptr;
854
855
857 {
858 if (!in_place_entry)
859 in_place_entry = &storage.
setProps(elem, side, state);
860
861 dataLoad(data, (*in_place_entry)[to_stateful_id],
nullptr);
862 }
863
864
865 else
866 {
867 if (!restart_entry)
869
870 auto & mat_entry = (*restart_entry)[to_stateful_id];
871 if (state >= mat_entry.size())
872 mat_entry.resize(state + 1);
873
874 mat_entry[state] = std::move(data);
875 }
876 }
877 }
878 }
879 }
880 }
881}
void dataLoad(std::istream &stream, MaterialPropertyStorage &storage, void *context)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
if(!dmm->_nl) SETERRQ(PETSC_COMM_WORLD
bool _recovering
Whether or not we're recovering; enforces a one-to-one mapping of stateful properties.
bool _restart_in_place
Whether or not we want to restart stateful properties in place.
std::vector< unsigned int > _stateful_prop_id_to_prop_id
the vector of stateful property ids (the vector index is the map to stateful prop_id)
std::vector< std::optional< PropRecord > > _prop_records
Property records indexed by property id (may be null)
unsigned int numStates() const
bool isRestoredProperty(const std::string &name) const
MaterialProperties & setProps(const Elem *elem, unsigned int side, const unsigned int state=0)
const MaterialPropertyRegistry & getMaterialPropertyRegistry() const
const PropRecord & getPropRecord(const unsigned int id) const
Get the property record associated with the material with id id.
RestartableMapType _restartable_map
The restartable data to be loaded in initStatefulProps() later.
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
std::string name(const ElemQuality q)
auto index_range(const T &sizable)
IntRange< T > make_range(T beg, T end)
unsigned int stateful_id
The stateful id in _storage used for this property, if any.