feat(mean_field): added dimensions, discritization, and start of eos
The full rewrite of mean_field into something maintainable is progressing. dimensions is mostly done, discritization (domain, blocks, and fields) is done, and eos is progressing quickly
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "serif/discretization/domain/schema/traits.hpp"
|
||||
|
||||
namespace serif::discretization::domain::schema {
|
||||
template <typename T>
|
||||
concept IsSchema = is_schema_v<T>;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "serif/discretization/domain/concepts.hpp"
|
||||
#include "serif/discretization/domain/ids/lists/concepts.hpp"
|
||||
#include "serif/discretization/domain/relation/lists/concepts.hpp"
|
||||
#include "serif/discretization/domain/relation/validation/concepts.hpp"
|
||||
#include "serif/discretization/domain/resolver.hpp"
|
||||
|
||||
|
||||
namespace serif::discretization::domain::schema {
|
||||
using ids::lists::IsDomainIDList;
|
||||
using ids::lists::IsBoundaryIDList;
|
||||
using relation::lists::IsRelationList;
|
||||
|
||||
using relation::validation::HaveValidRelationEntities;
|
||||
|
||||
template <IsDomainIDList DomainIDs, IsBoundaryIDList BoundaryIDs, IsRelationList Relations>
|
||||
requires HaveValidRelationEntities<DomainIDs, BoundaryIDs, Relations>
|
||||
struct DomainSchema {
|
||||
using domain_ids_type = DomainIDs;
|
||||
using boundary_ids_type = BoundaryIDs;
|
||||
using relations_type = Relations;
|
||||
|
||||
static constexpr std::size_t domain_id_count = DomainIDs::count;
|
||||
static constexpr std::size_t boundary_id_count = BoundaryIDs::count;
|
||||
static constexpr std::size_t relation_count = Relations::count;
|
||||
|
||||
[[nodiscard]] static constexpr auto domain_descriptors() noexcept {
|
||||
return DomainIDs::descriptors();
|
||||
}
|
||||
|
||||
[[nodiscard]] static constexpr auto boundary_descriptors() noexcept {
|
||||
return BoundaryIDs::descriptors();
|
||||
}
|
||||
|
||||
template <IsDomainOrSet DomainT>
|
||||
[[nodiscard]] static consteval bool contains_domain() noexcept {
|
||||
return DomainIDResolver<DomainT, DomainIDs>::registered;
|
||||
}
|
||||
|
||||
// Does an element attribute read from a mesh belong to DomainT?
|
||||
template <IsDomainOrSet DomainT>
|
||||
[[nodiscard]] static constexpr bool domain_id_belongs_to(const int domainID) noexcept {
|
||||
static_assert(contains_domain<DomainT>(), "DomainT is not registered in this schema.");
|
||||
|
||||
return DomainIDResolver<DomainT, DomainIDs>::contains_id(domainID);
|
||||
}
|
||||
|
||||
// The single element attribute of a single volume.
|
||||
template <IsDomain DomainT>
|
||||
[[nodiscard]] static consteval int domain_id() noexcept {
|
||||
static_assert(contains_domain<DomainT>(), "DomainT is not registered in this schema.");
|
||||
return DomainIDResolver<DomainT, DomainIDs>::id();
|
||||
}
|
||||
|
||||
template <IsBoundary BoundaryT>
|
||||
[[nodiscard]] static consteval bool contains_boundary() noexcept {
|
||||
return BoundaryIDResolver<BoundaryT, BoundaryIDs>::registered;
|
||||
}
|
||||
|
||||
template <IsBoundary BoundaryT>
|
||||
[[nodiscard]] static consteval int boundary_id() noexcept {
|
||||
static_assert(contains_boundary<BoundaryT>(), "Requested boundary is not registered in this schema.");
|
||||
return BoundaryIDResolver<BoundaryT, BoundaryIDs>::id();
|
||||
}
|
||||
|
||||
// Does a boundary attribute read from a mesh belong to BoundaryT?
|
||||
template <IsBoundary BoundaryT>
|
||||
[[nodiscard]] static constexpr bool boundary_id_matches(const int boundaryID) noexcept {
|
||||
static_assert(contains_boundary<BoundaryT>(), "Requested boundary is not registered in this schema.");
|
||||
|
||||
return BoundaryIDResolver<BoundaryT, BoundaryIDs>::contains_id(boundaryID);
|
||||
}
|
||||
};
|
||||
}
|
||||
50
src/include/serif/discretization/domain/schema/schemas.hpp
Normal file
50
src/include/serif/discretization/domain/schema/schemas.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include "serif/discretization/domain/ids/boundary.hpp"
|
||||
#include "serif/discretization/domain/ids/domain.hpp"
|
||||
#include "serif/discretization/domain/ids/lists/lists.hpp"
|
||||
#include "serif/discretization/domain/physical_domains.hpp"
|
||||
#include "serif/discretization/domain/relation/lists/relation_list.hpp"
|
||||
#include "serif/discretization/domain/relation/relations.hpp"
|
||||
#include "serif/discretization/domain/schema/domain_schema.hpp"
|
||||
#include "serif/discretization/domain/types.hpp"
|
||||
|
||||
namespace serif::discretization::domain::schema {
|
||||
using ids::DomainID;
|
||||
using ids::BoundaryID;
|
||||
|
||||
using ids::lists::DomainIDList;
|
||||
using ids::lists::BoundaryIDList;
|
||||
|
||||
using relation::lists::RelationList;
|
||||
|
||||
// A star (core inside envelope) surrounded by a vacuum region, with a
|
||||
// tagged stellar surface and a tagged outer surface.
|
||||
using CoreEnvelopeVacuumDomainSchema = DomainSchema<
|
||||
DomainIDList<
|
||||
DomainID<CoreDomain, 1>,
|
||||
DomainID<EnvelopeDomain, 2>,
|
||||
DomainID<VacuumDomain, 3>
|
||||
>,
|
||||
BoundaryIDList<
|
||||
BoundaryID<StellarSurfaceBoundary, 1>,
|
||||
BoundaryID<InfinitySurfaceBoundary, 2>
|
||||
>,
|
||||
RelationList<
|
||||
// No domain may arrive as two disconnected blobs.
|
||||
relation::FullyConnected<CoreDomain>,
|
||||
relation::FullyConnected<EnvelopeDomain>,
|
||||
relation::FullyConnected<VacuumDomain>,
|
||||
|
||||
// The core sits inside the envelope, and the star as a whole sits
|
||||
// inside the vacuum region.
|
||||
relation::Inscribed<CoreDomain, EnvelopeDomain>,
|
||||
relation::Inscribed<StellarDomains, VacuumDomain>,
|
||||
|
||||
// The stellar surface is the star/vacuum interface; the infinity
|
||||
// surface is the outer edge of the vacuum region.
|
||||
relation::DomainBoundary<StellarSurfaceBoundary, StellarDomains, VacuumDomain>,
|
||||
relation::DomainBoundary<InfinitySurfaceBoundary, VacuumDomain>
|
||||
>
|
||||
>;
|
||||
}
|
||||
11
src/include/serif/discretization/domain/schema/traits.hpp
Normal file
11
src/include/serif/discretization/domain/schema/traits.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "serif/discretization/domain/schema/domain_schema.hpp"
|
||||
|
||||
namespace serif::discretization::domain::schema {
|
||||
template <typename S>
|
||||
constexpr bool is_schema_v = false;
|
||||
|
||||
template <IsDomainIDList DomainIDs, IsBoundaryIDList BoundaryIDs, IsRelationList Relations>
|
||||
constexpr bool is_schema_v<DomainSchema<DomainIDs, BoundaryIDs, Relations>> = true;
|
||||
}
|
||||
24
src/include/serif/discretization/domain/schema/utils.hpp
Normal file
24
src/include/serif/discretization/domain/schema/utils.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <mfem.hpp>
|
||||
|
||||
#include "serif/discretization/domain/concepts.hpp"
|
||||
#include "serif/discretization/domain/schema/concepts.hpp"
|
||||
|
||||
namespace serif::discretization::domain::schema {
|
||||
// Build the 0/1 marker array MFEM wants when an integrator or an essential
|
||||
// BC should apply only on part of the mesh.
|
||||
template <IsDomainOrSet DomainT, IsSchema SchemaT>
|
||||
[[nodiscard]] mfem::Array<int> make_attribute_marker(const mfem::Mesh &mesh) {
|
||||
static_assert(
|
||||
SchemaT::template contains_domain<DomainT>(),
|
||||
"Requested marker domain is not completely registered in the supplied DomainSchema."
|
||||
);
|
||||
|
||||
mfem::Array<int> marker(mesh.attributes.Max());
|
||||
for (int domainID = 1; domainID <= marker.Size(); ++domainID) {
|
||||
marker[domainID - 1] = SchemaT::template domain_id_belongs_to<DomainT>(domainID) ? 1 : 0;
|
||||
}
|
||||
return marker;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <mfem.hpp>
|
||||
|
||||
#include "serif/discretization/domain/mesh/topology.hpp"
|
||||
#include "serif/discretization/domain/schema/concepts.hpp"
|
||||
#include "serif/discretization/domain/schema/validation/results.hpp"
|
||||
#include "serif/discretization/domain/schema/validation/validator.hpp"
|
||||
|
||||
// Every RelationValidator specialization must be visible before
|
||||
// SchemaRelationValidator is instantiated, so they are all included here and
|
||||
// validate_schema is defined only in this header. Include this file to validate
|
||||
// a mesh; including validator.hpp alone is not enough.
|
||||
#include "serif/discretization/domain/schema/validation/boundary.hpp"
|
||||
#include "serif/discretization/domain/schema/validation/connected.hpp"
|
||||
#include "serif/discretization/domain/schema/validation/inscribed.hpp"
|
||||
|
||||
namespace serif::discretization::domain::schema::validation {
|
||||
// Validate against connectivity that has already been extracted. Prefer
|
||||
// this overload when validating several schemas against the same mesh.
|
||||
template <IsSchema SchemaT>
|
||||
[[nodiscard]] SchemaValidationResult validate_schema(const mesh::MeshTopology &topology) {
|
||||
using RelationsT = typename SchemaT::relations_type;
|
||||
return SchemaRelationValidator<SchemaT, RelationsT>::validate(topology);
|
||||
}
|
||||
|
||||
// Extract the mesh connectivity once, then check every relation against it.
|
||||
template <IsSchema SchemaT>
|
||||
[[nodiscard]] SchemaValidationResult validate_schema(const mfem::Mesh &mfemMesh) {
|
||||
const mesh::MeshTopology topology{mfemMesh};
|
||||
return validate_schema<SchemaT>(topology);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include "serif/discretization/domain/concepts.hpp"
|
||||
#include "serif/discretization/domain/mesh/topology.hpp"
|
||||
#include "serif/discretization/domain/relation/relations.hpp"
|
||||
#include "serif/discretization/domain/relation/validation/runtime.hpp"
|
||||
#include "serif/discretization/domain/schema/concepts.hpp"
|
||||
#include "serif/discretization/domain/schema/validation/validator.hpp"
|
||||
|
||||
namespace serif::discretization::domain::schema::validation {
|
||||
using relation::DomainBoundary;
|
||||
|
||||
using relation::validation::RelationValidationResult;
|
||||
using relation::validation::RelationValidationFailure;
|
||||
|
||||
template <IsBoundary BoundaryT, IsDomainOrSet... DomainTs>
|
||||
struct RelationValidator<DomainBoundary<BoundaryT, DomainTs...>> {
|
||||
template <IsSchema SchemaT>
|
||||
[[nodiscard]] static RelationValidationResult validate(const mesh::MeshTopology &topology) {
|
||||
static_assert(
|
||||
sizeof...(DomainTs) == 1 || sizeof...(DomainTs) == 2,
|
||||
"DomainBoundary requires exactly one or two domains."
|
||||
);
|
||||
|
||||
static_assert(
|
||||
SchemaT::template contains_boundary<BoundaryT>(),
|
||||
"DomainBoundary refers to a boundary which is not registered in the supplied DomainSchema."
|
||||
);
|
||||
|
||||
static_assert(
|
||||
(SchemaT::template contains_domain<DomainTs>() && ...),
|
||||
"DomainBoundary refers to a domain which is not completely registered in the supplied DomainSchema."
|
||||
);
|
||||
|
||||
constexpr int expectedBoundaryID = SchemaT::template boundary_id<BoundaryT>();
|
||||
|
||||
using DomainsTuple = std::tuple<DomainTs...>;
|
||||
|
||||
const auto make_diagnostics = [&](const int faceID, const int boundaryElementID, const std::optional<int> actualBoundaryID) {
|
||||
RelationValidationResult::DomainBoundaryDiagnostics diagnostics{
|
||||
.faceID = faceID,
|
||||
.boundaryElementID = boundaryElementID,
|
||||
.expectedBoundaryID = expectedBoundaryID,
|
||||
.actualBoundaryID = actualBoundaryID
|
||||
};
|
||||
|
||||
if (faceID < 0 || faceID >= topology.face_count()) {
|
||||
return diagnostics;
|
||||
}
|
||||
|
||||
const mesh::FaceElements faceElements = topology.face_elements(faceID);
|
||||
|
||||
diagnostics.firstElementID = faceElements.firstElementID;
|
||||
diagnostics.secondElementID = faceElements.secondElementID;
|
||||
|
||||
if (diagnostics.firstElementID >= 0) {
|
||||
diagnostics.firstDomainID = topology.element_domain_id(diagnostics.firstElementID);
|
||||
}
|
||||
if (diagnostics.secondElementID >= 0) {
|
||||
diagnostics.secondDomainID = topology.element_domain_id(diagnostics.secondElementID);
|
||||
}
|
||||
|
||||
return diagnostics;
|
||||
};
|
||||
|
||||
// Check only the cardinality/topological shape required by the relation.
|
||||
//
|
||||
// One-domain form:
|
||||
//
|
||||
// Domain | computational exterior
|
||||
//
|
||||
// Exactly one adjacent volume element must exist.
|
||||
//
|
||||
// Two-domain form:
|
||||
//
|
||||
// DomainA | DomainB
|
||||
//
|
||||
// Both adjacent volume elements must exist.
|
||||
const auto has_required_topology = [](const int firstElementID, const int secondElementID) noexcept {
|
||||
if constexpr (sizeof...(DomainTs) == 1) {
|
||||
const bool firstExists = firstElementID >= 0;
|
||||
const bool secondExists = secondElementID >= 0;
|
||||
|
||||
return firstExists != secondExists;
|
||||
} else {
|
||||
return firstElementID >= 0 && secondElementID >= 0;
|
||||
}
|
||||
};
|
||||
|
||||
// Determine whether a face is exactly one of the faces described by
|
||||
// DomainBoundary<BoundaryT, DomainTs...>. For two domains, ordering
|
||||
// is intentionally irrelevant.
|
||||
const auto face_matches_domains = [&topology](const int firstElementID, const int secondElementID) noexcept {
|
||||
if constexpr (sizeof...(DomainTs) == 1) {
|
||||
using DomainT = std::tuple_element_t<0, DomainsTuple>;
|
||||
|
||||
const bool firstExists = firstElementID >= 0;
|
||||
const bool secondExists = secondElementID >= 0;
|
||||
|
||||
if (firstExists == secondExists) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int elementID = firstExists ? firstElementID : secondElementID;
|
||||
const int domainID = topology.element_domain_id(elementID);
|
||||
|
||||
return SchemaT::template domain_id_belongs_to<DomainT>(domainID);
|
||||
} else {
|
||||
using FirstDomainT = std::tuple_element_t<0, DomainsTuple>;
|
||||
using SecondDomainT = std::tuple_element_t<1, DomainsTuple>;
|
||||
|
||||
if (firstElementID < 0 || secondElementID < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int firstDomainID = topology.element_domain_id(firstElementID);
|
||||
const int secondDomainID = topology.element_domain_id(secondElementID);
|
||||
|
||||
const bool forwardMatch = SchemaT::template domain_id_belongs_to<FirstDomainT>(firstDomainID) &&
|
||||
SchemaT::template domain_id_belongs_to<SecondDomainT>(secondDomainID);
|
||||
|
||||
const bool reverseMatch = SchemaT::template domain_id_belongs_to<SecondDomainT>(firstDomainID) &&
|
||||
SchemaT::template domain_id_belongs_to<FirstDomainT>(secondDomainID);
|
||||
|
||||
return forwardMatch || reverseMatch;
|
||||
}
|
||||
};
|
||||
|
||||
bool foundTaggedBoundary = false;
|
||||
|
||||
// Forward validation: every boundary element carrying BoundaryT must
|
||||
// lie on exactly the interface declared by DomainBoundary.
|
||||
for (int boundaryElementID = 0; boundaryElementID < topology.boundary_element_count(); ++boundaryElementID) {
|
||||
const int boundaryID = topology.boundary_element_boundary_id(boundaryElementID);
|
||||
|
||||
if (boundaryID != expectedBoundaryID) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foundTaggedBoundary = true;
|
||||
|
||||
const int faceID = topology.boundary_element_face(boundaryElementID);
|
||||
|
||||
const auto [firstElementID, secondElementID] = topology.face_elements(faceID);
|
||||
|
||||
if (!has_required_topology(firstElementID, secondElementID)) {
|
||||
return {
|
||||
.failure = RelationValidationFailure::DomainBoundaryTaggedFaceHasWrongTopology,
|
||||
.domainBoundaryDiagnostics =
|
||||
std::make_optional<RelationValidationResult::DomainBoundaryDiagnostics>(
|
||||
make_diagnostics(faceID, boundaryElementID, boundaryID)
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
if (!face_matches_domains(firstElementID, secondElementID)) {
|
||||
return {
|
||||
.failure = RelationValidationFailure::DomainBoundaryTaggedFaceTouchesUnexpectedDomain,
|
||||
.domainBoundaryDiagnostics =
|
||||
std::make_optional<RelationValidationResult::DomainBoundaryDiagnostics>(
|
||||
make_diagnostics(faceID, boundaryElementID, boundaryID)
|
||||
)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
bool foundExpectedFace = false;
|
||||
|
||||
// Reverse validation: every face having the declared domain
|
||||
// adjacency must carry BoundaryT.
|
||||
for (int faceID = 0; faceID < topology.face_count(); ++faceID) {
|
||||
const auto [firstElementID, secondElementID] = topology.face_elements(faceID);
|
||||
|
||||
if (!face_matches_domains(firstElementID, secondElementID)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foundExpectedFace = true;
|
||||
|
||||
const std::vector<int> &boundaryElementIDs = topology.boundary_elements_on_face(faceID);
|
||||
|
||||
if (boundaryElementIDs.empty()) {
|
||||
return {
|
||||
.failure = RelationValidationFailure::DomainBoundaryExpectedFaceIsUntagged,
|
||||
.domainBoundaryDiagnostics =
|
||||
std::make_optional<RelationValidationResult::DomainBoundaryDiagnostics>(
|
||||
make_diagnostics(faceID, -1, std::nullopt)
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
for (const int boundaryElementID : boundaryElementIDs) {
|
||||
const int actualBoundaryID = topology.boundary_element_boundary_id(boundaryElementID);
|
||||
|
||||
if (actualBoundaryID == expectedBoundaryID) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return {
|
||||
.failure = RelationValidationFailure::DomainBoundaryExpectedFaceHasWrongID,
|
||||
.domainBoundaryDiagnostics =
|
||||
std::make_optional<RelationValidationResult::DomainBoundaryDiagnostics>(
|
||||
make_diagnostics(faceID, boundaryElementID, actualBoundaryID)
|
||||
)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// If neither a correctly tagged boundary nor a face having the
|
||||
// required semantic topology exists, the declared DomainBoundary
|
||||
// simply is not realized by this mesh.
|
||||
if (!foundTaggedBoundary || !foundExpectedFace) {
|
||||
return {
|
||||
.failure = RelationValidationFailure::DomainBoundaryAbsent,
|
||||
.domainBoundaryDiagnostics =
|
||||
std::make_optional<RelationValidationResult::DomainBoundaryDiagnostics>(
|
||||
make_diagnostics(-1, -1, std::nullopt)
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "serif/discretization/domain/concepts.hpp"
|
||||
#include "serif/discretization/domain/mesh/topology.hpp"
|
||||
#include "serif/discretization/domain/relation/relations.hpp"
|
||||
#include "serif/discretization/domain/relation/validation/runtime.hpp"
|
||||
#include "serif/discretization/domain/schema/concepts.hpp"
|
||||
#include "serif/discretization/domain/schema/validation/validator.hpp"
|
||||
|
||||
// FullyConnected<DomainT>: walk the domain from one of its elements and check we
|
||||
// reach all of them.
|
||||
namespace serif::discretization::domain::schema::validation {
|
||||
using relation::FullyConnected;
|
||||
|
||||
using relation::validation::RelationValidationResult;
|
||||
using relation::validation::RelationValidationFailure;
|
||||
|
||||
template <IsDomainOrSet DomainT>
|
||||
struct RelationValidator<FullyConnected<DomainT>> {
|
||||
template <IsSchema SchemaT>
|
||||
[[nodiscard]] static RelationValidationResult validate(const mesh::MeshTopology &topology) {
|
||||
static_assert(SchemaT::template contains_domain<DomainT>(), "Domain not present, cannot determine connectedness.");
|
||||
|
||||
const int elementCount = topology.element_count();
|
||||
|
||||
// We should be careful here that I have not accidentally introduced an issue due to the specialized vector bool override. I think this is safe though.
|
||||
std::vector<bool> belongsToDomain(static_cast<std::size_t>(elementCount), false);
|
||||
|
||||
int domainElementCount = 0;
|
||||
int firstDomainElement = -1;
|
||||
|
||||
for (int elementID = 0; elementID < elementCount; ++elementID) {
|
||||
const int domainID = topology.element_domain_id(elementID);
|
||||
const bool belongs = SchemaT::template domain_id_belongs_to<DomainT>(domainID);
|
||||
|
||||
belongsToDomain[static_cast<std::size_t>(elementID)] = belongs;
|
||||
|
||||
if (!belongs) {
|
||||
continue;
|
||||
}
|
||||
|
||||
++domainElementCount;
|
||||
if (firstDomainElement < 0) {
|
||||
firstDomainElement = elementID;
|
||||
}
|
||||
}
|
||||
|
||||
if (domainElementCount == 0) {
|
||||
return {
|
||||
.failure = RelationValidationFailure::DomainAbsent,
|
||||
.connectedDiagnostics = std::make_optional<RelationValidationResult::ConnectedDiagnostics>({
|
||||
.domainElementCount = 0,
|
||||
.visitedElementCount = 0
|
||||
})};
|
||||
}
|
||||
|
||||
std::vector<bool> visited(static_cast<std::size_t>(elementCount), false);
|
||||
std::vector<int> pending;
|
||||
|
||||
pending.reserve(static_cast<std::size_t>(domainElementCount));
|
||||
pending.push_back(firstDomainElement);
|
||||
|
||||
int visitedElementCount = 0;
|
||||
|
||||
while (!pending.empty()) {
|
||||
const int elementID = pending.back();
|
||||
pending.pop_back();
|
||||
|
||||
if (visited[static_cast<std::size_t>(elementID)]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
visited[static_cast<std::size_t>(elementID)] = true;
|
||||
++visitedElementCount;
|
||||
|
||||
for (const int neighborElementID : topology.element_neighbors(elementID)) {
|
||||
const std::size_t neighborIndex = static_cast<std::size_t>(neighborElementID);
|
||||
|
||||
if (belongsToDomain[neighborIndex] && !visited[neighborIndex]) {
|
||||
pending.push_back(neighborElementID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (visitedElementCount == domainElementCount) {
|
||||
return {
|
||||
.connectedDiagnostics = std::make_optional<RelationValidationResult::ConnectedDiagnostics>(
|
||||
{.domainElementCount = domainElementCount, .visitedElementCount = visitedElementCount}
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
int disconnectedElementID = -1;
|
||||
|
||||
for (int elementID = 0; elementID < elementCount; ++elementID) {
|
||||
const std::size_t index = static_cast<std::size_t>(elementID);
|
||||
|
||||
if (belongsToDomain[index] && !visited[index]) {
|
||||
disconnectedElementID = elementID;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
.failure = RelationValidationFailure::DomainDisconnected,
|
||||
.connectedDiagnostics = std::make_optional<RelationValidationResult::ConnectedDiagnostics>({
|
||||
.elementID = disconnectedElementID,
|
||||
.domainElementCount = domainElementCount,
|
||||
.visitedElementCount = visitedElementCount
|
||||
})};
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include "serif/discretization/domain/concepts.hpp"
|
||||
#include "serif/discretization/domain/mesh/topology.hpp"
|
||||
#include "serif/discretization/domain/relation/relations.hpp"
|
||||
#include "serif/discretization/domain/relation/validation/runtime.hpp"
|
||||
#include "serif/discretization/domain/schema/concepts.hpp"
|
||||
#include "serif/discretization/domain/schema/validation/validator.hpp"
|
||||
|
||||
// Inscribed<InnerT, OuterT>: every face where the inner domain stops must have
|
||||
// the outer domain on the far side.
|
||||
namespace serif::discretization::domain::schema::validation {
|
||||
using relation::Inscribed;
|
||||
|
||||
using relation::validation::RelationValidationResult;
|
||||
using relation::validation::RelationValidationFailure;
|
||||
|
||||
template <IsDomainOrSet InnerT, IsDomainOrSet OuterT>
|
||||
struct RelationValidator<Inscribed<InnerT, OuterT>> {
|
||||
template <IsSchema SchemaT>
|
||||
[[nodiscard]] static RelationValidationResult validate(const mesh::MeshTopology &topology) {
|
||||
static_assert(
|
||||
SchemaT::template contains_domain<InnerT>(), "The inner domain of an Inscribed relation is not present in the supplied schema. Inscribed cannot be enforced"
|
||||
);
|
||||
|
||||
static_assert(
|
||||
SchemaT::template contains_domain<OuterT>(), "The outer domain of an Inscribed relation is not present in the supplied schema. Inscribed cannot be enforced"
|
||||
);
|
||||
|
||||
bool foundInnerElement = false;
|
||||
bool foundOuterElement = false;
|
||||
bool foundInnerBoundary = false;
|
||||
|
||||
for (int elementID = 0; elementID < topology.element_count(); ++elementID) {
|
||||
const int domainID = topology.element_domain_id(elementID);
|
||||
|
||||
foundInnerElement = foundInnerElement || SchemaT::template domain_id_belongs_to<InnerT>(domainID);
|
||||
foundOuterElement = foundOuterElement || SchemaT::template domain_id_belongs_to<OuterT>(domainID);
|
||||
}
|
||||
|
||||
if (!foundInnerElement) {
|
||||
return {.failure = RelationValidationFailure::InnerDomainAbsent};
|
||||
}
|
||||
|
||||
if (!foundOuterElement) {
|
||||
return {.failure = RelationValidationFailure::OuterDomainAbsent};
|
||||
}
|
||||
|
||||
for (int faceID = 0; faceID < topology.face_count(); ++faceID) {
|
||||
const mesh::FaceElements faceElements = topology.face_elements(faceID);
|
||||
|
||||
const int firstElementID = faceElements.firstElementID;
|
||||
const int secondElementID = faceElements.secondElementID;
|
||||
|
||||
const bool firstIsInner = firstElementID >= 0 &&
|
||||
SchemaT::template domain_id_belongs_to<InnerT>(topology.element_domain_id(firstElementID));
|
||||
const bool secondIsInner = secondElementID >= 0 &&
|
||||
SchemaT::template domain_id_belongs_to<InnerT>(topology.element_domain_id(secondElementID));
|
||||
|
||||
if (firstIsInner == secondIsInner) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foundInnerBoundary = true;
|
||||
|
||||
const int innerElementID = firstIsInner ? firstElementID : secondElementID;
|
||||
const int adjacentElementID = firstIsInner ? secondElementID : firstElementID;
|
||||
|
||||
if (adjacentElementID < 0) {
|
||||
return {
|
||||
.failure = RelationValidationFailure::InnerDomainTouchesMeshBoundary,
|
||||
.inscribedDiagnostics = std::make_optional<RelationValidationResult::InscribedDiagnostics>({
|
||||
.faceID = faceID,
|
||||
.innerElementID = innerElementID,
|
||||
})};
|
||||
}
|
||||
|
||||
const int adjacentDomainID = topology.element_domain_id(adjacentElementID);
|
||||
|
||||
if (!SchemaT::template domain_id_belongs_to<OuterT>(adjacentDomainID)) {
|
||||
return {
|
||||
.failure = RelationValidationFailure::InnerDomainTouchesUnexpectedDomain,
|
||||
.inscribedDiagnostics = std::make_optional<RelationValidationResult::InscribedDiagnostics>({
|
||||
.faceID = faceID,
|
||||
.innerElementID = innerElementID,
|
||||
.adjacentElementID = adjacentElementID,
|
||||
.adjacentDomainID = adjacentDomainID
|
||||
})};
|
||||
}
|
||||
}
|
||||
|
||||
if (!foundInnerBoundary) {
|
||||
return {.failure = RelationValidationFailure::InnerDomainHasNoBoundary};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <format>
|
||||
#include <string>
|
||||
|
||||
#include "serif/discretization/domain/relation/validation/runtime.hpp"
|
||||
|
||||
// The aggregate result of validating a whole schema against a mesh
|
||||
namespace serif::discretization::domain::schema::validation {
|
||||
struct SchemaRelationValidationResult {
|
||||
std::size_t relationIndex{0};
|
||||
std::string_view relationName;
|
||||
relation::validation::RelationValidationResult result;
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept;
|
||||
[[nodiscard]] explicit operator bool() const noexcept;
|
||||
};
|
||||
|
||||
struct SchemaValidationResult {
|
||||
std::vector<SchemaRelationValidationResult> relationResults;
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept;
|
||||
[[nodiscard]] explicit operator bool() const noexcept;
|
||||
[[nodiscard]] std::size_t relation_count() const noexcept;
|
||||
[[nodiscard]] std::size_t failed_relation_count() const noexcept;
|
||||
[[nodiscard]] std::size_t passed_relation_count() const noexcept;
|
||||
[[nodiscard]] std::optional<std::size_t> first_failed_relation_index() const noexcept;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
template <>
|
||||
struct std::formatter<serif::discretization::domain::schema::validation::SchemaValidationResult> {
|
||||
std::formatter<std::string> string_formatter;
|
||||
|
||||
constexpr auto parse(std::format_parse_context& ctx) {
|
||||
return string_formatter.parse(ctx);
|
||||
}
|
||||
|
||||
auto format(const serif::discretization::domain::schema::validation::SchemaValidationResult& result, std::format_context& ctx) const {
|
||||
std::string output;
|
||||
output += "SchemaValidationResult:\n";
|
||||
output += std::format(" Valid: {}\n", result.valid());
|
||||
output += std::format(" Relation count: {}\n", result.relation_count());
|
||||
output += std::format(" Passed relation count: {}\n", result.passed_relation_count());
|
||||
output += std::format(" Failed relation count: {}\n", result.failed_relation_count());
|
||||
if (auto first_failed_index = result.first_failed_relation_index()) {
|
||||
output += std::format(" First failed relation index: {}\n", *first_failed_index);
|
||||
} else {
|
||||
output += " No failed relations.\n";
|
||||
}
|
||||
return string_formatter.format(output, ctx);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "serif/discretization/domain/mesh/topology.hpp"
|
||||
#include "serif/discretization/domain/relation/concepts.hpp"
|
||||
#include "serif/discretization/domain/relation/lists/relation_list.hpp"
|
||||
#include "serif/discretization/domain/relation/validation/runtime.hpp"
|
||||
#include "serif/discretization/domain/schema/concepts.hpp"
|
||||
#include "serif/discretization/domain/schema/validation/results.hpp"
|
||||
|
||||
// The primary RelationValidator template, plus the fold that runs one validator
|
||||
// per declared relation.
|
||||
//
|
||||
// IMPORTANT: every relation kind is validated by an explicit specialization of
|
||||
// RelationValidator, and those specializations live in sibling headers
|
||||
// (inscribed.hpp, connected.hpp, boundary.hpp). Under headers, instantiating
|
||||
// SchemaRelationValidator in a translation unit that has not yet seen all of
|
||||
// those specializations is ill-formed with no diagnostic required. That is why
|
||||
// validate_schema lives in all.hpp, which includes this header and every
|
||||
// specialization. Include all.hpp, not this header, to validate a mesh.
|
||||
namespace serif::discretization::domain::schema::validation {
|
||||
using relation::IsRelation;
|
||||
|
||||
template <IsRelation RelationT>
|
||||
struct RelationValidator;
|
||||
|
||||
template <IsSchema SchemaT, typename RelationListT>
|
||||
struct SchemaRelationValidator;
|
||||
|
||||
template <IsSchema SchemaT, IsRelation... RelationTs>
|
||||
struct SchemaRelationValidator<SchemaT, relation::lists::RelationList<RelationTs...>> {
|
||||
|
||||
[[nodiscard]] static SchemaValidationResult validate(const mesh::MeshTopology &topology) {
|
||||
SchemaValidationResult schemaResult;
|
||||
|
||||
schemaResult.relationResults.reserve(sizeof...(RelationTs));
|
||||
std::size_t relationIndex = 0;
|
||||
|
||||
(schemaResult.relationResults.push_back(SchemaRelationValidationResult{
|
||||
.relationIndex = relationIndex++,
|
||||
.relationName = RelationTs::name,
|
||||
.result = RelationValidator<RelationTs>::template validate<SchemaT>(topology)
|
||||
}), ...);
|
||||
|
||||
return schemaResult;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user