feat(thermal): publish fully validated HDF5 histories atomically
This commit is contained in:
@@ -8,6 +8,9 @@
|
||||
|
||||
namespace fesa::hdf5_internal {
|
||||
|
||||
/// @brief Writes every Domain node preserving its source identity.
|
||||
void WriteNodes(hid_t file, const Domain& domain);
|
||||
|
||||
/// @brief Writes metadata and exact model/source identity datasets.
|
||||
void WriteModel(hid_t file, const Domain& domain,
|
||||
const WriterModelData& model_data);
|
||||
|
||||
@@ -14,6 +14,9 @@
|
||||
|
||||
namespace fesa::hdf5_internal {
|
||||
|
||||
/// @brief Writes deterministically ordered diagnostics using the common schema.
|
||||
void WriteDiagnostics(hid_t file, const std::vector<Diagnostic>& diagnostics);
|
||||
|
||||
/// @brief Builds the complete backend-neutral result dataset inventory.
|
||||
Result<WriterPlan> BuildWriterPlan(const std::filesystem::path& output_path,
|
||||
const Domain& domain,
|
||||
|
||||
@@ -0,0 +1,445 @@
|
||||
#include "fesa/io/hdf5/thermal_hdf5_results_writer.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "fesa/build_info.h"
|
||||
#include "io/hdf5/hdf5_atomic_file.h"
|
||||
#include "io/hdf5/hdf5_model_writer.h"
|
||||
#include "io/hdf5/hdf5_primitives.h"
|
||||
#include "io/hdf5/hdf5_raii.h"
|
||||
#include "io/hdf5/hdf5_result_writer.h"
|
||||
#include "io/hdf5/thermal_hdf5_self_check.h"
|
||||
|
||||
namespace fesa {
|
||||
namespace {
|
||||
using hdf5_internal::Hdf5Handle;
|
||||
|
||||
/// @brief Carries node/source-DOF reaction identities in stable row order.
|
||||
struct ReactionRow {
|
||||
std::uint64_t internal_node_id;
|
||||
std::uint64_t source_dof;
|
||||
};
|
||||
/// @brief Carries accepted schedule values independently of stored-time
|
||||
/// differences.
|
||||
struct FrameRow {
|
||||
std::uint64_t frame_index;
|
||||
std::uint64_t increment_index;
|
||||
double time;
|
||||
double time_increment;
|
||||
std::uint8_t is_initial;
|
||||
};
|
||||
/// @brief Carries thermal properties with global Domain material identities.
|
||||
struct MaterialRow {
|
||||
std::uint64_t internal_material_id;
|
||||
const char* name;
|
||||
double conductivity;
|
||||
double density;
|
||||
double specific_heat;
|
||||
};
|
||||
/// @brief Carries section provenance and global material linkage.
|
||||
struct SectionRow {
|
||||
std::uint64_t internal_section_id;
|
||||
const char* source_file;
|
||||
std::uint64_t source_line;
|
||||
const char* source_elset;
|
||||
std::uint64_t material_internal_id;
|
||||
double thickness;
|
||||
};
|
||||
/// @brief Carries DS4 source identities and fixed four-node connectivity.
|
||||
struct ElementRow {
|
||||
std::uint64_t internal_element_id;
|
||||
const char* instance_name;
|
||||
const char* source_label;
|
||||
const char* source_element_type;
|
||||
const char* internal_formulation;
|
||||
std::uint64_t node_internal_ids[4];
|
||||
std::uint64_t shell_section_internal_id;
|
||||
std::uint64_t material_internal_id;
|
||||
};
|
||||
|
||||
/// @brief Writes fixed named fields, preserving zero-length reaction tables.
|
||||
template <class Row>
|
||||
Hdf5Handle Compound(hid_t file, const char* path, const std::vector<Row>& rows,
|
||||
hid_t file_type, hid_t memory_type) {
|
||||
if (rows.empty()) {
|
||||
const hsize_t zero[] = {0U};
|
||||
const hsize_t unlimited[] = {H5S_UNLIMITED};
|
||||
Hdf5Handle space{
|
||||
hdf5_internal::RequireHdf5Id(H5Screate_simple(1, zero, unlimited),
|
||||
"Unable to create empty compound space."),
|
||||
H5Sclose};
|
||||
Hdf5Handle properties{hdf5_internal::RequireHdf5Id(
|
||||
H5Pcreate(H5P_DATASET_CREATE),
|
||||
"Unable to create empty compound properties."),
|
||||
H5Pclose};
|
||||
const hsize_t chunk[] = {1U};
|
||||
hdf5_internal::RequireHdf5(H5Pset_chunk(properties.Get(), 1, chunk),
|
||||
"Unable to configure empty compound chunk.");
|
||||
return Hdf5Handle{
|
||||
hdf5_internal::RequireHdf5Id(
|
||||
H5Dcreate2(file, path, file_type, space.Get(), H5P_DEFAULT,
|
||||
properties.Get(), H5P_DEFAULT),
|
||||
"Unable to create empty compound HDF5 dataset."),
|
||||
H5Dclose};
|
||||
}
|
||||
return hdf5_internal::WriteCompoundDataset(file, path, rows.size(), file_type,
|
||||
memory_type, rows.data());
|
||||
}
|
||||
/// @brief Defines an explicit named field without making padding a read
|
||||
/// contract.
|
||||
void Member(hid_t t, const char* n, size_t off, hid_t f) {
|
||||
hdf5_internal::RequireHdf5(H5Tinsert(t, n, off, f),
|
||||
"Unable to define compound field.");
|
||||
}
|
||||
/// @brief Builds fixed-width connectivity array datatypes.
|
||||
Hdf5Handle Array(hid_t type, int rank, const hsize_t* dims) {
|
||||
return Hdf5Handle{
|
||||
hdf5_internal::RequireHdf5Id(H5Tarray_create2(type, rank, dims),
|
||||
"Unable to create array type."),
|
||||
H5Tclose};
|
||||
}
|
||||
/// @brief Separates fixed little-endian file fields from native memory fields.
|
||||
template <class Row, class Init>
|
||||
void Types(Hdf5Handle& ft, Hdf5Handle& mt, Init init) {
|
||||
ft = Hdf5Handle{
|
||||
hdf5_internal::RequireHdf5Id(H5Tcreate(H5T_COMPOUND, sizeof(Row)),
|
||||
"Unable to create file compound."),
|
||||
H5Tclose};
|
||||
mt = Hdf5Handle{
|
||||
hdf5_internal::RequireHdf5Id(H5Tcreate(H5T_COMPOUND, sizeof(Row)),
|
||||
"Unable to create memory compound."),
|
||||
H5Tclose};
|
||||
init(ft.Get(), mt.Get());
|
||||
}
|
||||
/// @brief Writes all twelve thermal discriminator, source and schedule
|
||||
/// attributes.
|
||||
void Metadata(hid_t file, const Domain& domain,
|
||||
const ThermalAnalysisState& state) {
|
||||
auto g = hdf5_internal::CreateGroup(file, "/metadata");
|
||||
auto a = [&](const char* n, const std::string& v) {
|
||||
hdf5_internal::WriteStringAttribute(g.Get(), n, v);
|
||||
};
|
||||
hdf5_internal::WriteUint64Attribute(g.Get(), "schema_version", 1);
|
||||
a("feature_id", "shell-transient-heat-transfer");
|
||||
a("analysis_procedure", "uncoupled-transient-heat-transfer");
|
||||
a("source_step_name",
|
||||
domain.ThermalStep()->name.empty() ? "Step-1" : domain.ThermalStep()->name);
|
||||
a("solver_version", std::string{SolverVersion()});
|
||||
a("source_input_identity", hdf5_internal::SourceInputIdentity(domain));
|
||||
a("unit_system_label", "user-consistent-unspecified");
|
||||
a("internal_formulation", "FESA-Q4-P2x2-thermal-shell");
|
||||
a("integration_rule", "surface-3x3-gauss; thickness-exact-P2x2");
|
||||
a("time_integration", "backward-euler");
|
||||
hdf5_internal::WriteDoubleAttribute(g.Get(), "nominal_time_increment",
|
||||
state.NominalTimeIncrement());
|
||||
hdf5_internal::WriteDoubleAttribute(g.Get(), "time_period",
|
||||
state.TimePeriod());
|
||||
}
|
||||
/// @brief Writes all geometry with separate active thermal row maps and
|
||||
/// recovery axes.
|
||||
void WriteThermalModel(hid_t file, const Domain& d,
|
||||
const ThermalAnalysisState& s) {
|
||||
auto model = hdf5_internal::CreateGroup(file, "/model");
|
||||
(void)model;
|
||||
// Nodes retain all Domain rows; active result mappings remain separate.
|
||||
Hdf5Handle ft, mt;
|
||||
auto st = hdf5_internal::MakeUtf8StringType();
|
||||
hdf5_internal::WriteNodes(file, d);
|
||||
std::vector<ElementRow> elements;
|
||||
for (size_t i = 0; i < d.ThermalElements().Size(); ++i) {
|
||||
const auto& e = d.ThermalElements()[i];
|
||||
elements.push_back({i,
|
||||
e.SourceId().instance_name.c_str(),
|
||||
e.SourceId().source_label_text.c_str(),
|
||||
"DS4",
|
||||
"FESA-Q4-P2x2-thermal-shell",
|
||||
{e.NodeIndices()[0], e.NodeIndices()[1],
|
||||
e.NodeIndices()[2], e.NodeIndices()[3]},
|
||||
e.ThermalSectionIndex(),
|
||||
e.MaterialIndex()});
|
||||
}
|
||||
hsize_t four[] = {4};
|
||||
auto fu = Array(H5T_STD_U64LE, 1, four),
|
||||
mu = Array(H5T_NATIVE_UINT64, 1, four);
|
||||
Types<ElementRow>(ft, mt, [&](hid_t f, hid_t m) {
|
||||
auto add = [&](hid_t t, hid_t u, hid_t ar) {
|
||||
Member(t, "internal_element_id", HOFFSET(ElementRow, internal_element_id),
|
||||
u);
|
||||
Member(t, "instance_name", HOFFSET(ElementRow, instance_name), st.Get());
|
||||
Member(t, "source_label", HOFFSET(ElementRow, source_label), st.Get());
|
||||
Member(t, "source_element_type", HOFFSET(ElementRow, source_element_type),
|
||||
st.Get());
|
||||
Member(t, "internal_formulation",
|
||||
HOFFSET(ElementRow, internal_formulation), st.Get());
|
||||
Member(t, "node_internal_ids", HOFFSET(ElementRow, node_internal_ids),
|
||||
ar);
|
||||
Member(t, "shell_section_internal_id",
|
||||
HOFFSET(ElementRow, shell_section_internal_id), u);
|
||||
Member(t, "material_internal_id",
|
||||
HOFFSET(ElementRow, material_internal_id), u);
|
||||
};
|
||||
add(f, H5T_STD_U64LE, fu.Get());
|
||||
add(m, H5T_NATIVE_UINT64, mu.Get());
|
||||
});
|
||||
(void)Compound(file, "/model/elements", elements, ft.Get(), mt.Get());
|
||||
std::vector<MaterialRow> mats;
|
||||
for (size_t i = 0; i < d.ThermalMaterials().Size(); ++i) {
|
||||
const auto& m = d.ThermalMaterials()[i];
|
||||
size_t id = 0;
|
||||
for (; id < d.Materials().Size(); ++id)
|
||||
if (&d.Materials()[id] == &m) break;
|
||||
mats.push_back({id, m.Name().c_str(), m.Conductivity(), m.Density(),
|
||||
m.SpecificHeat()});
|
||||
}
|
||||
Types<MaterialRow>(ft, mt, [&](hid_t f, hid_t m) {
|
||||
auto add = [&](hid_t t, hid_t u, hid_t q) {
|
||||
Member(t, "internal_material_id",
|
||||
HOFFSET(MaterialRow, internal_material_id), u);
|
||||
Member(t, "name", HOFFSET(MaterialRow, name), st.Get());
|
||||
Member(t, "conductivity", HOFFSET(MaterialRow, conductivity), q);
|
||||
Member(t, "density", HOFFSET(MaterialRow, density), q);
|
||||
Member(t, "specific_heat", HOFFSET(MaterialRow, specific_heat), q);
|
||||
};
|
||||
add(f, H5T_STD_U64LE, H5T_IEEE_F64LE);
|
||||
add(m, H5T_NATIVE_UINT64, H5T_NATIVE_DOUBLE);
|
||||
});
|
||||
auto md =
|
||||
Compound(file, "/model/thermal_materials", mats, ft.Get(), mt.Get());
|
||||
hdf5_internal::WriteStringAttribute(md.Get(), "conductivity_units_label",
|
||||
"power/(length*temperature)");
|
||||
hdf5_internal::WriteStringAttribute(md.Get(), "density_units_label",
|
||||
"mass/volume");
|
||||
hdf5_internal::WriteStringAttribute(md.Get(), "specific_heat_units_label",
|
||||
"energy/(mass*temperature)");
|
||||
std::vector<SectionRow> secs;
|
||||
std::vector<std::string> files;
|
||||
files.reserve(d.ThermalSections().size());
|
||||
|
||||
for (size_t i = 0; i < d.ThermalSections().size(); ++i) {
|
||||
const auto& q = d.ThermalSections()[i];
|
||||
files.push_back(hdf5_internal::NormalizedPathString(q.location.file));
|
||||
secs.push_back({i, files.back().c_str(), q.location.line, q.name.c_str(),
|
||||
hdf5_internal::GlobalThermalMaterialId(d, q.material_index),
|
||||
q.thickness});
|
||||
}
|
||||
Types<SectionRow>(ft, mt, [&](hid_t f, hid_t m) {
|
||||
auto add = [&](hid_t t, hid_t u, hid_t q) {
|
||||
Member(t, "internal_section_id", HOFFSET(SectionRow, internal_section_id),
|
||||
u);
|
||||
Member(t, "source_file", HOFFSET(SectionRow, source_file), st.Get());
|
||||
Member(t, "source_line", HOFFSET(SectionRow, source_line), u);
|
||||
Member(t, "source_elset", HOFFSET(SectionRow, source_elset), st.Get());
|
||||
Member(t, "material_internal_id",
|
||||
HOFFSET(SectionRow, material_internal_id), u);
|
||||
Member(t, "thickness", HOFFSET(SectionRow, thickness), q);
|
||||
};
|
||||
add(f, H5T_STD_U64LE, H5T_IEEE_F64LE);
|
||||
add(m, H5T_NATIVE_UINT64, H5T_NATIVE_DOUBLE);
|
||||
});
|
||||
auto sd = Compound(file, "/model/shell_sections", secs, ft.Get(), mt.Get());
|
||||
hdf5_internal::WriteStringAttribute(sd.Get(), "thickness_units_label",
|
||||
"length");
|
||||
std::vector<std::uint64_t> nids(s.NodeOrder().begin(), s.NodeOrder().end()),
|
||||
eids(s.ElementOrder().begin(), s.ElementOrder().end());
|
||||
auto U = [&](const char* p, const std::vector<std::uint64_t>& v) {
|
||||
auto sp = hdf5_internal::CreateDatasetSpace({v.size()});
|
||||
Hdf5Handle ds{hdf5_internal::RequireHdf5Id(
|
||||
H5Dcreate2(file, p, H5T_STD_U64LE, sp.Get(), H5P_DEFAULT,
|
||||
H5P_DEFAULT, H5P_DEFAULT),
|
||||
"Unable to create IDs."),
|
||||
H5Dclose};
|
||||
if (!v.empty())
|
||||
hdf5_internal::RequireHdf5(H5Dwrite(ds.Get(), H5T_NATIVE_UINT64, H5S_ALL,
|
||||
H5S_ALL, H5P_DEFAULT, v.data()),
|
||||
"Unable to write IDs.");
|
||||
};
|
||||
U("/model/thermal_node_ids", nids);
|
||||
U("/model/thermal_element_ids", eids);
|
||||
std::vector<std::uint64_t> dofs = {11, 12, 13, 14, 15};
|
||||
U("/model/temperature_dofs", dofs);
|
||||
const std::vector<double> zeta = {-1, -.5, 0, .5, 1},
|
||||
coords = {-1 / std::sqrt(3.), -1 / std::sqrt(3.),
|
||||
1 / std::sqrt(3.), -1 / std::sqrt(3.),
|
||||
1 / std::sqrt(3.), 1 / std::sqrt(3.),
|
||||
-1 / std::sqrt(3.), 1 / std::sqrt(3.)},
|
||||
fz = {-1, -.5, 0, 0, .5, 1};
|
||||
auto D = [&](const char* p, std::vector<hsize_t> sh,
|
||||
const std::vector<double>& v) {
|
||||
(void)hdf5_internal::WriteDoubleValues(file, p, sh, v.data(), v.size());
|
||||
};
|
||||
D("/model/temperature_zeta", {5}, zeta);
|
||||
D("/model/thermal_flux_inplane_coordinates", {4, 2}, coords);
|
||||
D("/model/thermal_flux_zeta", {6}, fz);
|
||||
std::vector<std::int8_t> side = {1, 0, -1, 1, 0, -1};
|
||||
auto sp = hdf5_internal::CreateDatasetSpace({6});
|
||||
Hdf5Handle sid{
|
||||
hdf5_internal::RequireHdf5Id(
|
||||
H5Dcreate2(file, "/model/thermal_flux_trace_side", H5T_STD_I8LE,
|
||||
sp.Get(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT),
|
||||
"Unable to create trace side."),
|
||||
H5Dclose};
|
||||
hdf5_internal::RequireHdf5(H5Dwrite(sid.Get(), H5T_NATIVE_INT8, H5S_ALL,
|
||||
H5S_ALL, H5P_DEFAULT, side.data()),
|
||||
"Unable to write trace side.");
|
||||
std::vector<std::uint8_t> mask;
|
||||
for (const auto& r : s.ConstraintMask())
|
||||
mask.insert(mask.end(), r.begin(), r.end());
|
||||
hdf5_internal::WriteUint8Dataset(file, "/model/thermal_constraint_mask",
|
||||
{s.NodeOrder().size(), 5}, mask.data(),
|
||||
mask.size());
|
||||
std::vector<ReactionRow> reactions;
|
||||
for (const auto& r : s.ReactionDofs())
|
||||
reactions.push_back({r.node, (std::uint64_t)r.source_dof});
|
||||
Types<ReactionRow>(ft, mt, [&](hid_t f, hid_t m) {
|
||||
Member(f, "internal_node_id", HOFFSET(ReactionRow, internal_node_id),
|
||||
H5T_STD_U64LE);
|
||||
Member(f, "source_dof", HOFFSET(ReactionRow, source_dof), H5T_STD_U64LE);
|
||||
Member(m, "internal_node_id", HOFFSET(ReactionRow, internal_node_id),
|
||||
H5T_NATIVE_UINT64);
|
||||
Member(m, "source_dof", HOFFSET(ReactionRow, source_dof),
|
||||
H5T_NATIVE_UINT64);
|
||||
});
|
||||
(void)Compound(file, "/model/thermal_reaction_dofs", reactions, ft.Get(),
|
||||
mt.Get());
|
||||
std::vector<double> frames;
|
||||
for (const auto& e : s.FluxLocalFrames())
|
||||
for (const auto& p : e)
|
||||
for (size_t g = 0; g < 3; ++g)
|
||||
for (size_t a = 0; a < 3; ++a) frames.push_back(p[a][g]);
|
||||
D("/model/thermal_flux_local_frames", {s.ElementOrder().size(), 4, 3, 3},
|
||||
frames);
|
||||
}
|
||||
/// @brief Writes raw initial temperature and completed-only reactions and
|
||||
/// fluxes.
|
||||
void WriteFrames(hid_t file, const ThermalAnalysisState& s) {
|
||||
(void)hdf5_internal::CreateGroup(file, "/steps/Step-1");
|
||||
std::vector<FrameRow> rows;
|
||||
for (const auto& f : s.AcceptedFrames())
|
||||
rows.push_back({f.frame_index, f.increment_index, f.time, f.time_increment,
|
||||
(std::uint8_t)f.is_initial});
|
||||
Hdf5Handle ft, mt;
|
||||
Types<FrameRow>(ft, mt, [&](hid_t x, hid_t y) {
|
||||
auto a = [&](hid_t t, hid_t u, hid_t q) {
|
||||
Member(t, "frame_index", HOFFSET(FrameRow, frame_index), u);
|
||||
Member(t, "increment_index", HOFFSET(FrameRow, increment_index), u);
|
||||
Member(t, "time", HOFFSET(FrameRow, time), q);
|
||||
Member(t, "time_increment", HOFFSET(FrameRow, time_increment), q);
|
||||
Member(t, "is_initial", HOFFSET(FrameRow, is_initial), H5T_STD_U8LE);
|
||||
};
|
||||
a(x, H5T_STD_U64LE, H5T_IEEE_F64LE);
|
||||
a(y, H5T_NATIVE_UINT64, H5T_NATIVE_DOUBLE);
|
||||
});
|
||||
auto af =
|
||||
Compound(file, "/steps/Step-1/accepted_frames", rows, ft.Get(), mt.Get());
|
||||
hdf5_internal::WriteStringAttribute(af.Get(), "time_units_label", "time");
|
||||
for (const auto& f : s.AcceptedFrames()) {
|
||||
std::string root = "/steps/Step-1/frames/" + std::to_string(f.frame_index);
|
||||
auto g = hdf5_internal::CreateGroup(file, root.c_str());
|
||||
(void)hdf5_internal::CreateGroup(file, (root + "/nodal").c_str());
|
||||
hdf5_internal::WriteUint64Attribute(g.Get(), "frame_index", f.frame_index);
|
||||
hdf5_internal::WriteUint64Attribute(g.Get(), "increment_index",
|
||||
f.increment_index);
|
||||
hdf5_internal::WriteDoubleAttribute(g.Get(), "time", f.time);
|
||||
hdf5_internal::WriteDoubleAttribute(g.Get(), "time_increment",
|
||||
f.time_increment);
|
||||
hdf5_internal::WriteUint8Attribute(g.Get(), "is_initial", f.is_initial);
|
||||
auto td = hdf5_internal::WriteDoubleValues(
|
||||
file, root + "/nodal/temperature", {s.NodeOrder().size(), 5},
|
||||
f.temperature.Data(), f.temperature.Size());
|
||||
hdf5_internal::WriteStringAttribute(td.Get(), "component_order",
|
||||
"NT11,NT12,NT13,NT14,NT15");
|
||||
hdf5_internal::WriteStringAttribute(td.Get(), "units_label", "temperature");
|
||||
hdf5_internal::WriteStringAttribute(td.Get(), "coordinate_system",
|
||||
"scalar");
|
||||
hdf5_internal::WriteStringAttribute(td.Get(), "location",
|
||||
"nodal-thickness-stations");
|
||||
if (f.completed) {
|
||||
(void)hdf5_internal::CreateGroup(file, (root + "/element").c_str());
|
||||
auto rd = hdf5_internal::WriteDoubleValues(
|
||||
file, root + "/nodal/reaction_heat_rate", {s.ReactionDofs().size()},
|
||||
f.completed->reaction_heat_rate.Data(),
|
||||
f.completed->reaction_heat_rate.Size());
|
||||
hdf5_internal::WriteStringAttribute(rd.Get(), "component_order", "RFL");
|
||||
hdf5_internal::WriteStringAttribute(rd.Get(), "units_label", "power");
|
||||
hdf5_internal::WriteStringAttribute(rd.Get(), "coordinate_system",
|
||||
"scalar");
|
||||
hdf5_internal::WriteStringAttribute(rd.Get(), "location",
|
||||
"prescribed-temperature-dofs");
|
||||
hdf5_internal::WriteStringAttribute(rd.Get(), "positive_direction",
|
||||
"into-model");
|
||||
std::vector<double> q;
|
||||
for (const auto& e : f.completed->global_heat_flux)
|
||||
for (size_t p = 0; p < 4; ++p)
|
||||
for (size_t z = 0; z < 6; ++z)
|
||||
for (size_t c = 0; c < 3; ++c) q.push_back(e[6 * p + z][c]);
|
||||
auto fd = hdf5_internal::WriteDoubleValues(
|
||||
file, root + "/element/heat_flux", {s.ElementOrder().size(), 4, 6, 3},
|
||||
q.data(), q.size());
|
||||
hdf5_internal::WriteStringAttribute(fd.Get(), "component_order",
|
||||
"QX,QY,QZ");
|
||||
hdf5_internal::WriteStringAttribute(fd.Get(), "units_label",
|
||||
"power/area");
|
||||
hdf5_internal::WriteStringAttribute(fd.Get(), "coordinate_system",
|
||||
"global-cartesian");
|
||||
hdf5_internal::WriteStringAttribute(fd.Get(), "location",
|
||||
"element-inplane-thickness-traces");
|
||||
}
|
||||
}
|
||||
}
|
||||
/// @brief Converts adapter failures into the existing output-failure category.
|
||||
Status Fail(const char* m) {
|
||||
return hdf5_internal::OutputFailure("thermal-output-schema", m);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Status hdf5_internal::WriteThermalFile(
|
||||
const std::filesystem::path& output_path, const Domain& domain,
|
||||
const ThermalAnalysisState& state,
|
||||
const std::vector<Diagnostic>& diagnostics, const ThermalFileHooks& hooks) {
|
||||
const auto inventory = ValidateThermalOutput(domain, state);
|
||||
if (!inventory.IsOk()) return inventory;
|
||||
if (!hooks.candidate_close || !hooks.reader_close)
|
||||
return Fail("Missing thermal file closer.");
|
||||
try {
|
||||
hdf5_internal::Hdf5ErrorSilencer silence;
|
||||
auto candidate = hdf5_internal::MakeCandidatePath(output_path);
|
||||
hdf5_internal::CandidateFileGuard guard{candidate};
|
||||
Hdf5Handle file{hdf5_internal::RequireHdf5Id(
|
||||
H5Fcreate(candidate.string().c_str(), H5F_ACC_EXCL,
|
||||
H5P_DEFAULT, H5P_DEFAULT),
|
||||
"Unable to create thermal candidate."),
|
||||
hooks.candidate_close};
|
||||
if (hooks.before_write) hooks.before_write(file.Get());
|
||||
Metadata(file.Get(), domain, state);
|
||||
WriteThermalModel(file.Get(), domain, state);
|
||||
WriteFrames(file.Get(), state);
|
||||
hdf5_internal::WriteDiagnostics(file.Get(), diagnostics);
|
||||
hdf5_internal::RequireHdf5(H5Fflush(file.Get(), H5F_SCOPE_GLOBAL),
|
||||
"Unable to flush thermal candidate.");
|
||||
hdf5_internal::RequireHdf5(file.CloseChecked(),
|
||||
"Unable to close thermal candidate.");
|
||||
if (hooks.before_self_check) hooks.before_self_check(candidate);
|
||||
const Status ok = hdf5_internal::SelfCheckThermalFile(
|
||||
candidate, domain, state, diagnostics, hooks.reader_close);
|
||||
if (ok.IsOk() && hooks.before_finalize) hooks.before_finalize(candidate);
|
||||
const Status out = hdf5_internal::AtomicFinalizeValidatedCandidate(
|
||||
candidate, output_path, ok);
|
||||
if (out.IsOk()) guard.Release();
|
||||
return out;
|
||||
} catch (const std::exception& e) {
|
||||
return Fail(e.what());
|
||||
}
|
||||
}
|
||||
Status ThermalHdf5ResultsWriter::Write(
|
||||
const std::filesystem::path& output_path, const Domain& domain,
|
||||
const ThermalAnalysisState& state,
|
||||
const std::vector<Diagnostic>& diagnostics) {
|
||||
return hdf5_internal::WriteThermalFile(output_path, domain, state,
|
||||
diagnostics);
|
||||
}
|
||||
} // namespace fesa
|
||||
@@ -0,0 +1,581 @@
|
||||
#include "io/hdf5/thermal_hdf5_self_check.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "fesa/build_info.h"
|
||||
#include "io/hdf5/hdf5_primitives.h"
|
||||
#include "io/hdf5/hdf5_raii.h"
|
||||
|
||||
namespace fesa::hdf5_internal {
|
||||
namespace {
|
||||
using U64 = std::uint64_t;
|
||||
/// @brief Rejects malformed mandatory schema without exposing HDF5 diagnostics.
|
||||
void Require(bool condition, const std::string& message) {
|
||||
if (!condition) throw Hdf5Failure{message};
|
||||
}
|
||||
/// @brief Opens one mandatory dataset and checks its entire declared extent.
|
||||
Hdf5Handle Dataset(hid_t file, const std::string& path,
|
||||
const std::vector<hsize_t>& shape) {
|
||||
Hdf5Handle d{
|
||||
RequireHdf5Id(H5Dopen2(file, path.c_str(), H5P_DEFAULT), path.c_str()),
|
||||
H5Dclose};
|
||||
Hdf5Handle space{RequireHdf5Id(H5Dget_space(d.Get()), "Missing dataspace."),
|
||||
H5Sclose};
|
||||
Require(
|
||||
H5Sget_simple_extent_ndims(space.Get()) == static_cast<int>(shape.size()),
|
||||
path + ": wrong rank");
|
||||
std::vector<hsize_t> actual(shape.size());
|
||||
Require(H5Sget_simple_extent_dims(space.Get(), actual.data(), nullptr) >= 0 &&
|
||||
actual == shape,
|
||||
path + ": wrong extent");
|
||||
return d;
|
||||
}
|
||||
/// @brief Compares every typed scalar against an independent expected
|
||||
/// inventory.
|
||||
template <class T>
|
||||
void Values(hid_t d, hid_t file_type, hid_t memory_type,
|
||||
const std::vector<T>& expected) {
|
||||
Hdf5Handle type{RequireHdf5Id(H5Dget_type(d), "Missing dataset type."),
|
||||
H5Tclose};
|
||||
Require(H5Tequal(type.Get(), file_type) > 0,
|
||||
"Wrong mandatory dataset primitive type.");
|
||||
std::vector<T> values(expected.size());
|
||||
if (!values.empty())
|
||||
RequireHdf5(
|
||||
H5Dread(d, memory_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, values.data()),
|
||||
"Cannot read dataset.");
|
||||
Require(
|
||||
values == expected,
|
||||
"Dataset values or ordered identities differ from the supplied history.");
|
||||
}
|
||||
/// @brief Checks a scalar numeric attribute before reading its one-value
|
||||
/// buffer.
|
||||
template <class T>
|
||||
void Number(hid_t object, const char* name, hid_t file_type, hid_t memory_type,
|
||||
T expected) {
|
||||
Hdf5Handle a{RequireHdf5Id(H5Aopen(object, name, H5P_DEFAULT), name),
|
||||
H5Aclose};
|
||||
Hdf5Handle t{RequireHdf5Id(H5Aget_type(a.Get()), "Missing attribute type."),
|
||||
H5Tclose};
|
||||
Hdf5Handle s{RequireHdf5Id(H5Aget_space(a.Get()), "Missing attribute space."),
|
||||
H5Sclose};
|
||||
Require(H5Tequal(t.Get(), file_type) > 0 &&
|
||||
H5Sget_simple_extent_type(s.Get()) == H5S_SCALAR,
|
||||
std::string{name} + ": wrong scalar attribute type/space");
|
||||
T value{};
|
||||
RequireHdf5(H5Aread(a.Get(), memory_type, &value), "Cannot read attribute.");
|
||||
Require(value == expected, std::string{name} + ": wrong attribute value");
|
||||
}
|
||||
/// @brief Reads either fixed or variable UTF-8 strings, preserving schema
|
||||
/// freedom.
|
||||
void Strings(hid_t string_type, const std::vector<std::string>& expected,
|
||||
const std::function<herr_t(hid_t, void*)>& read, bool compound,
|
||||
const char* field) {
|
||||
Require(H5Tget_class(string_type) == H5T_STRING &&
|
||||
H5Tget_cset(string_type) == H5T_CSET_UTF8,
|
||||
"Mandatory string is not UTF-8.");
|
||||
const auto variable = H5Tis_variable_str(string_type);
|
||||
Require(variable >= 0, "Cannot inspect string storage.");
|
||||
const std::size_t width = variable ? sizeof(char*) : H5Tget_size(string_type);
|
||||
Require(width > 0, "Invalid fixed string width.");
|
||||
Hdf5Handle memory;
|
||||
hid_t read_type = string_type;
|
||||
if (compound) {
|
||||
memory = Hdf5Handle{RequireHdf5Id(H5Tcreate(H5T_COMPOUND, width),
|
||||
"Cannot create string projection."),
|
||||
H5Tclose};
|
||||
RequireHdf5(H5Tinsert(memory.Get(), field, 0, string_type),
|
||||
"Cannot project string field.");
|
||||
read_type = memory.Get();
|
||||
}
|
||||
bool equal = true;
|
||||
if (variable) {
|
||||
std::vector<char*> values(expected.size(), nullptr);
|
||||
const herr_t status = values.empty() ? 0 : read(read_type, values.data());
|
||||
for (std::size_t i = 0; i < values.size(); ++i) {
|
||||
if (!values[i] || !IsValidUtf8(values[i]) || expected[i] != values[i])
|
||||
equal = false;
|
||||
if (values[i] && H5free_memory(values[i]) < 0) equal = false;
|
||||
}
|
||||
RequireHdf5(status, "Cannot read variable strings.");
|
||||
} else {
|
||||
std::size_t bytes = 0;
|
||||
Require(SizeProductFits(width, expected.size(), bytes),
|
||||
"String extent overflows memory.");
|
||||
std::vector<char> values(bytes);
|
||||
if (!values.empty())
|
||||
RequireHdf5(read(read_type, values.data()), "Cannot read fixed strings.");
|
||||
for (std::size_t i = 0; i < expected.size(); ++i) {
|
||||
std::string value(values.data() + i * width, width);
|
||||
const auto end = value.find('\0');
|
||||
if (end != std::string::npos)
|
||||
value.resize(end);
|
||||
else if (H5Tget_strpad(string_type) == H5T_STR_SPACEPAD)
|
||||
while (!value.empty() && value.back() == ' ') value.pop_back();
|
||||
if (!IsValidUtf8(value) || value != expected[i]) equal = false;
|
||||
}
|
||||
}
|
||||
Require(equal,
|
||||
"String field/attribute differs from the expected source identity or "
|
||||
"value.");
|
||||
}
|
||||
/// @brief Checks a mandatory scalar UTF-8 attribute, including its exact value.
|
||||
void Text(hid_t object, const char* name, const std::string& expected) {
|
||||
Hdf5Handle a{RequireHdf5Id(H5Aopen(object, name, H5P_DEFAULT), name),
|
||||
H5Aclose};
|
||||
Hdf5Handle t{RequireHdf5Id(H5Aget_type(a.Get()), "Missing text type."),
|
||||
H5Tclose};
|
||||
Hdf5Handle s{RequireHdf5Id(H5Aget_space(a.Get()), "Missing text space."),
|
||||
H5Sclose};
|
||||
Require(H5Sget_simple_extent_type(s.Get()) == H5S_SCALAR,
|
||||
"Text attribute is not scalar.");
|
||||
Strings(
|
||||
t.Get(), {expected},
|
||||
[&](hid_t type, void* data) { return H5Aread(a.Get(), type, data); },
|
||||
false, name);
|
||||
}
|
||||
/// @brief Locates a named field independently of compound order and padding.
|
||||
Hdf5Handle Field(hid_t dataset, const char* name) {
|
||||
Hdf5Handle t{RequireHdf5Id(H5Dget_type(dataset), "Missing compound type."),
|
||||
H5Tclose};
|
||||
Require(H5Tget_class(t.Get()) == H5T_COMPOUND,
|
||||
"Mandatory table is not compound.");
|
||||
const int index = H5Tget_member_index(t.Get(), name);
|
||||
Require(index >= 0, std::string{"Missing compound field: "} + name);
|
||||
return {
|
||||
RequireHdf5Id(H5Tget_member_type(t.Get(), static_cast<unsigned>(index)),
|
||||
"Cannot read compound member."),
|
||||
H5Tclose};
|
||||
}
|
||||
/// @brief Checks typed compound columns, including fixed-array element extents.
|
||||
template <class T>
|
||||
void Column(hid_t d, const char* name, const std::vector<T>& expected,
|
||||
hid_t primitive, hid_t native, hsize_t extent = 1) {
|
||||
auto disk = Field(d, name);
|
||||
Hdf5Handle fa, ma;
|
||||
hid_t ft = primitive, mt = native;
|
||||
if (extent != 1) {
|
||||
fa = {RequireHdf5Id(H5Tarray_create2(primitive, 1, &extent),
|
||||
"Cannot create file array projection."),
|
||||
H5Tclose};
|
||||
ma = {RequireHdf5Id(H5Tarray_create2(native, 1, &extent),
|
||||
"Cannot create memory array projection."),
|
||||
H5Tclose};
|
||||
ft = fa.Get();
|
||||
mt = ma.Get();
|
||||
}
|
||||
Require(H5Tequal(disk.Get(), ft) > 0,
|
||||
std::string{name} + ": wrong compound member type/extent");
|
||||
Hdf5Handle memory{
|
||||
RequireHdf5Id(
|
||||
H5Tcreate(H5T_COMPOUND, sizeof(T) * static_cast<std::size_t>(extent)),
|
||||
"Cannot create numeric projection."),
|
||||
H5Tclose};
|
||||
RequireHdf5(H5Tinsert(memory.Get(), name, 0, mt),
|
||||
"Cannot project numeric field.");
|
||||
std::vector<T> values(expected.size());
|
||||
if (!values.empty())
|
||||
RequireHdf5(
|
||||
H5Dread(d, memory.Get(), H5S_ALL, H5S_ALL, H5P_DEFAULT, values.data()),
|
||||
"Cannot read compound column.");
|
||||
Require(values == expected,
|
||||
std::string{name} + ": compound values or identities differ");
|
||||
}
|
||||
/// @brief Checks a named string column with independent expected source
|
||||
/// strings.
|
||||
void ColumnText(hid_t d, const char* name,
|
||||
const std::vector<std::string>& expected) {
|
||||
auto type = Field(d, name);
|
||||
Strings(
|
||||
type.Get(), expected,
|
||||
[&](hid_t t, void* p) {
|
||||
return H5Dread(d, t, H5S_ALL, H5S_ALL, H5P_DEFAULT, p);
|
||||
},
|
||||
true, name);
|
||||
}
|
||||
/// @brief Checks unsigned identity columns without depending on C++ row layout.
|
||||
void Id(hid_t d, const char* name, const std::vector<U64>& expected) {
|
||||
Column(d, name, expected, H5T_STD_U64LE, H5T_NATIVE_UINT64);
|
||||
}
|
||||
/// @brief Checks finite double columns against immutable Domain/history values.
|
||||
void Real(hid_t d, const char* name, const std::vector<double>& expected) {
|
||||
Column(d, name, expected, H5T_IEEE_F64LE, H5T_NATIVE_DOUBLE);
|
||||
}
|
||||
/// @brief Requires exact optional-child absence without treating API errors as
|
||||
/// absence.
|
||||
void Absent(hid_t file, const std::string& parent, const char* child) {
|
||||
const htri_t exists = H5Lexists(file, parent.c_str(), H5P_DEFAULT);
|
||||
Require(exists >= 0, "Cannot inspect optional recovery parent.");
|
||||
if (exists == 0) return;
|
||||
Require(H5Lexists(file, (parent + "/" + child).c_str(), H5P_DEFAULT) == 0,
|
||||
"Initial recovery is present or cannot be inspected.");
|
||||
}
|
||||
/// @brief Checks source tables against Domain rows, including global material
|
||||
/// IDs.
|
||||
void ModelTables(hid_t f, const Domain& domain) {
|
||||
std::vector<U64> ids, connectivity, sections, materials, lines;
|
||||
std::vector<std::string> instances, labels, types, formulations, names, files,
|
||||
elsets;
|
||||
std::vector<double> xyz, k, rho, c, h;
|
||||
for (std::size_t i = 0; i < domain.Nodes().size(); ++i) {
|
||||
const auto& node = domain.Nodes()[i];
|
||||
ids.push_back(i);
|
||||
instances.push_back(node.source_id.instance_name);
|
||||
labels.push_back(node.source_id.source_label_text);
|
||||
xyz.insert(xyz.end(), node.coordinates.begin(), node.coordinates.end());
|
||||
}
|
||||
auto nodes = Dataset(f, "/model/nodes", {ids.size()});
|
||||
Id(nodes.Get(), "internal_node_id", ids);
|
||||
ColumnText(nodes.Get(), "instance_name", instances);
|
||||
ColumnText(nodes.Get(), "source_label", labels);
|
||||
Column(nodes.Get(), "coordinates", xyz, H5T_IEEE_F64LE, H5T_NATIVE_DOUBLE, 3);
|
||||
Text(nodes.Get(), "units_label", "length");
|
||||
ids.clear();
|
||||
instances.clear();
|
||||
labels.clear();
|
||||
for (std::size_t i = 0; i < domain.ThermalElements().Size(); ++i) {
|
||||
const auto& e = domain.ThermalElements()[i];
|
||||
ids.push_back(i);
|
||||
instances.push_back(e.SourceId().instance_name);
|
||||
labels.push_back(e.SourceId().source_label_text);
|
||||
types.push_back("DS4");
|
||||
formulations.push_back("FESA-Q4-P2x2-thermal-shell");
|
||||
for (const auto node : e.NodeIndices()) connectivity.push_back(node);
|
||||
sections.push_back(e.ThermalSectionIndex());
|
||||
materials.push_back(e.MaterialIndex());
|
||||
}
|
||||
auto elements = Dataset(f, "/model/elements", {ids.size()});
|
||||
Id(elements.Get(), "internal_element_id", ids);
|
||||
ColumnText(elements.Get(), "instance_name", instances);
|
||||
ColumnText(elements.Get(), "source_label", labels);
|
||||
ColumnText(elements.Get(), "source_element_type", types);
|
||||
ColumnText(elements.Get(), "internal_formulation", formulations);
|
||||
Column(elements.Get(), "node_internal_ids", connectivity, H5T_STD_U64LE,
|
||||
H5T_NATIVE_UINT64, 4);
|
||||
Id(elements.Get(), "shell_section_internal_id", sections);
|
||||
Id(elements.Get(), "material_internal_id", materials);
|
||||
ids.clear();
|
||||
for (std::size_t i = 0; i < domain.ThermalMaterials().Size(); ++i) {
|
||||
const auto& material = domain.ThermalMaterials()[i];
|
||||
ids.push_back(GlobalThermalMaterialId(domain, i));
|
||||
names.push_back(material.Name());
|
||||
k.push_back(material.Conductivity());
|
||||
rho.push_back(material.Density());
|
||||
c.push_back(material.SpecificHeat());
|
||||
}
|
||||
auto mat = Dataset(f, "/model/thermal_materials", {ids.size()});
|
||||
Id(mat.Get(), "internal_material_id", ids);
|
||||
ColumnText(mat.Get(), "name", names);
|
||||
Real(mat.Get(), "conductivity", k);
|
||||
Real(mat.Get(), "density", rho);
|
||||
Real(mat.Get(), "specific_heat", c);
|
||||
Text(mat.Get(), "conductivity_units_label", "power/(length*temperature)");
|
||||
Text(mat.Get(), "density_units_label", "mass/volume");
|
||||
Text(mat.Get(), "specific_heat_units_label", "energy/(mass*temperature)");
|
||||
ids.clear();
|
||||
materials.clear();
|
||||
for (std::size_t i = 0; i < domain.ThermalSections().size(); ++i) {
|
||||
const auto& section = domain.ThermalSections()[i];
|
||||
ids.push_back(i);
|
||||
files.push_back(NormalizedPathString(section.location.file));
|
||||
lines.push_back(section.location.line);
|
||||
elsets.push_back(section.name);
|
||||
materials.push_back(
|
||||
GlobalThermalMaterialId(domain, section.material_index));
|
||||
h.push_back(section.thickness);
|
||||
}
|
||||
auto sec = Dataset(f, "/model/shell_sections", {ids.size()});
|
||||
Id(sec.Get(), "internal_section_id", ids);
|
||||
ColumnText(sec.Get(), "source_file", files);
|
||||
Id(sec.Get(), "source_line", lines);
|
||||
ColumnText(sec.Get(), "source_elset", elsets);
|
||||
Id(sec.Get(), "material_internal_id", materials);
|
||||
Real(sec.Get(), "thickness", h);
|
||||
Text(sec.Get(), "thickness_units_label", "length");
|
||||
}
|
||||
/// @brief Checks all scalar metadata values, including actual input byte
|
||||
/// identity.
|
||||
void Metadata(hid_t file, const Domain& domain,
|
||||
const ThermalAnalysisState& state) {
|
||||
Hdf5Handle m{RequireHdf5Id(H5Gopen2(file, "/metadata", H5P_DEFAULT),
|
||||
"Missing metadata."),
|
||||
H5Gclose};
|
||||
Number(m.Get(), "schema_version", H5T_STD_U64LE, H5T_NATIVE_UINT64, U64{1});
|
||||
Text(m.Get(), "feature_id", "shell-transient-heat-transfer");
|
||||
Text(m.Get(), "analysis_procedure", "uncoupled-transient-heat-transfer");
|
||||
Text(m.Get(), "source_step_name",
|
||||
domain.ThermalStep()->name.empty() ? "Step-1"
|
||||
: domain.ThermalStep()->name);
|
||||
Text(m.Get(), "solver_version", std::string{SolverVersion()});
|
||||
Text(m.Get(), "source_input_identity", SourceInputIdentity(domain));
|
||||
Text(m.Get(), "unit_system_label", "user-consistent-unspecified");
|
||||
Text(m.Get(), "internal_formulation", "FESA-Q4-P2x2-thermal-shell");
|
||||
Text(m.Get(), "integration_rule", "surface-3x3-gauss; thickness-exact-P2x2");
|
||||
Text(m.Get(), "time_integration", "backward-euler");
|
||||
Number(m.Get(), "nominal_time_increment", H5T_IEEE_F64LE, H5T_NATIVE_DOUBLE,
|
||||
state.NominalTimeIncrement());
|
||||
Number(m.Get(), "time_period", H5T_IEEE_F64LE, H5T_NATIVE_DOUBLE,
|
||||
state.TimePeriod());
|
||||
}
|
||||
/// @brief Checks active maps, station/trace conventions and the global-axis R
|
||||
/// layout.
|
||||
void Mappings(hid_t f, const ThermalAnalysisState& s) {
|
||||
const auto u = [&](const char* p, std::vector<U64> v) {
|
||||
auto d = Dataset(f, p, {v.size()});
|
||||
Values(d.Get(), H5T_STD_U64LE, H5T_NATIVE_UINT64, v);
|
||||
};
|
||||
const auto real = [&](const char* p, std::vector<hsize_t> sh,
|
||||
const std::vector<double>& v) {
|
||||
auto d = Dataset(f, p, sh);
|
||||
Values(d.Get(), H5T_IEEE_F64LE, H5T_NATIVE_DOUBLE, v);
|
||||
};
|
||||
u("/model/thermal_node_ids", {s.NodeOrder().begin(), s.NodeOrder().end()});
|
||||
u("/model/thermal_element_ids",
|
||||
{s.ElementOrder().begin(), s.ElementOrder().end()});
|
||||
u("/model/temperature_dofs", {11, 12, 13, 14, 15});
|
||||
real("/model/temperature_zeta", {5}, {-1, -.5, 0, .5, 1});
|
||||
std::vector<std::uint8_t> mask;
|
||||
for (const auto& row : s.ConstraintMask())
|
||||
mask.insert(mask.end(), row.begin(), row.end());
|
||||
auto md =
|
||||
Dataset(f, "/model/thermal_constraint_mask", {s.NodeOrder().size(), 5});
|
||||
Values(md.Get(), H5T_STD_U8LE, H5T_NATIVE_UINT8, mask);
|
||||
std::vector<U64> nodes, dofs;
|
||||
for (const auto& r : s.ReactionDofs()) {
|
||||
nodes.push_back(r.node);
|
||||
dofs.push_back(r.source_dof);
|
||||
}
|
||||
auto reaction = Dataset(f, "/model/thermal_reaction_dofs", {nodes.size()});
|
||||
Id(reaction.Get(), "internal_node_id", nodes);
|
||||
Id(reaction.Get(), "source_dof", dofs);
|
||||
const double g = 1 / std::sqrt(3.);
|
||||
real("/model/thermal_flux_inplane_coordinates", {4, 2},
|
||||
{-g, -g, g, -g, g, g, -g, g});
|
||||
real("/model/thermal_flux_zeta", {6}, {-1, -.5, 0, 0, .5, 1});
|
||||
auto side = Dataset(f, "/model/thermal_flux_trace_side", {6});
|
||||
Values(side.Get(), H5T_STD_I8LE, H5T_NATIVE_INT8,
|
||||
std::vector<std::int8_t>{1, 0, -1, 1, 0, -1});
|
||||
std::vector<double> frames;
|
||||
for (const auto& element : s.FluxLocalFrames())
|
||||
for (const auto& point : element)
|
||||
for (std::size_t component = 0; component < 3; ++component)
|
||||
for (std::size_t axis = 0; axis < 3; ++axis)
|
||||
frames.push_back(point[axis][component]);
|
||||
real("/model/thermal_flux_local_frames", {s.ElementOrder().size(), 4, 3, 3},
|
||||
frames);
|
||||
}
|
||||
/// @brief Checks result values and exact component/unit/location attributes.
|
||||
void ResultValues(hid_t file, const std::string& path,
|
||||
std::vector<hsize_t> shape, const std::vector<double>& values,
|
||||
const char* components, const char* units,
|
||||
const char* coordinates, const char* location) {
|
||||
auto dataset = Dataset(file, path, shape);
|
||||
Values(dataset.Get(), H5T_IEEE_F64LE, H5T_NATIVE_DOUBLE, values);
|
||||
Text(dataset.Get(), "component_order", components);
|
||||
Text(dataset.Get(), "units_label", units);
|
||||
Text(dataset.Get(), "coordinate_system", coordinates);
|
||||
Text(dataset.Get(), "location", location);
|
||||
}
|
||||
/// @brief Checks frame inventory and actual equation deltas without recomputing
|
||||
/// time differences.
|
||||
void Frames(hid_t f, const ThermalAnalysisState& s) {
|
||||
std::vector<U64> index, increment;
|
||||
std::vector<double> time, delta;
|
||||
std::vector<std::uint8_t> initial;
|
||||
for (const auto& row : s.AcceptedFrames()) {
|
||||
index.push_back(row.frame_index);
|
||||
increment.push_back(row.increment_index);
|
||||
time.push_back(row.time);
|
||||
delta.push_back(row.time_increment);
|
||||
initial.push_back(row.is_initial);
|
||||
}
|
||||
auto table = Dataset(f, "/steps/Step-1/accepted_frames", {index.size()});
|
||||
Id(table.Get(), "frame_index", index);
|
||||
Id(table.Get(), "increment_index", increment);
|
||||
Real(table.Get(), "time", time);
|
||||
Real(table.Get(), "time_increment", delta);
|
||||
Column(table.Get(), "is_initial", initial, H5T_STD_U8LE, H5T_NATIVE_UINT8);
|
||||
Text(table.Get(), "time_units_label", "time");
|
||||
Hdf5Handle group{
|
||||
RequireHdf5Id(H5Gopen2(f, "/steps/Step-1/frames", H5P_DEFAULT),
|
||||
"Missing frames group."),
|
||||
H5Gclose};
|
||||
H5G_info_t info{};
|
||||
RequireHdf5(H5Gget_info(group.Get(), &info), "Cannot enumerate frames.");
|
||||
Require(info.nlinks == index.size(), "Missing or extra frame groups.");
|
||||
for (const auto& frame : s.AcceptedFrames()) {
|
||||
const auto root =
|
||||
"/steps/Step-1/frames/" + std::to_string(frame.frame_index);
|
||||
Hdf5Handle g{
|
||||
RequireHdf5Id(H5Gopen2(f, root.c_str(), H5P_DEFAULT), "Missing frame."),
|
||||
H5Gclose};
|
||||
Number(g.Get(), "frame_index", H5T_STD_U64LE, H5T_NATIVE_UINT64,
|
||||
frame.frame_index);
|
||||
Number(g.Get(), "increment_index", H5T_STD_U64LE, H5T_NATIVE_UINT64,
|
||||
frame.increment_index);
|
||||
Number(g.Get(), "time", H5T_IEEE_F64LE, H5T_NATIVE_DOUBLE, frame.time);
|
||||
Number(g.Get(), "time_increment", H5T_IEEE_F64LE, H5T_NATIVE_DOUBLE,
|
||||
frame.time_increment);
|
||||
Number(g.Get(), "is_initial", H5T_STD_U8LE, H5T_NATIVE_UINT8,
|
||||
static_cast<std::uint8_t>(frame.is_initial));
|
||||
ResultValues(f, root + "/nodal/temperature", {s.NodeOrder().size(), 5},
|
||||
{frame.temperature.Data(),
|
||||
frame.temperature.Data() + frame.temperature.Size()},
|
||||
"NT11,NT12,NT13,NT14,NT15", "temperature", "scalar",
|
||||
"nodal-thickness-stations");
|
||||
if (frame.is_initial) {
|
||||
Absent(f, root + "/nodal", "reaction_heat_rate");
|
||||
Absent(f, root + "/element", "heat_flux");
|
||||
} else {
|
||||
const auto& payload = *frame.completed;
|
||||
std::vector<double> reaction(payload.reaction_heat_rate.Size());
|
||||
for (std::size_t i = 0; i < reaction.size(); ++i)
|
||||
reaction[i] = payload.reaction_heat_rate[i];
|
||||
ResultValues(f, root + "/nodal/reaction_heat_rate", {reaction.size()},
|
||||
reaction, "RFL", "power", "scalar",
|
||||
"prescribed-temperature-dofs");
|
||||
auto rd =
|
||||
Dataset(f, root + "/nodal/reaction_heat_rate", {reaction.size()});
|
||||
Text(rd.Get(), "positive_direction", "into-model");
|
||||
std::vector<double> flux;
|
||||
for (const auto& element : payload.global_heat_flux)
|
||||
for (const auto& q : element)
|
||||
for (const double value : q.Components()) flux.push_back(value);
|
||||
ResultValues(f, root + "/element/heat_flux",
|
||||
{s.ElementOrder().size(), 4, 6, 3}, flux, "QX,QY,QZ",
|
||||
"power/area", "global-cartesian",
|
||||
"element-inplane-thickness-traces");
|
||||
}
|
||||
}
|
||||
}
|
||||
/// @brief Checks sorted diagnostics with all established provenance fields.
|
||||
void Diagnostics(hid_t f, std::vector<Diagnostic> diagnostics) {
|
||||
SortDiagnostics(diagnostics);
|
||||
std::vector<std::string> severity, code, file, keyword, entity, message;
|
||||
std::vector<U64> line;
|
||||
for (const auto& d : diagnostics) {
|
||||
severity.push_back(d.severity == Severity::kWarning ? "warning" : "error");
|
||||
code.push_back(d.code);
|
||||
file.push_back(NormalizedPathString(d.location.file));
|
||||
line.push_back(d.location.line);
|
||||
keyword.push_back(d.keyword);
|
||||
entity.push_back(d.entity_identity);
|
||||
message.push_back(d.message);
|
||||
}
|
||||
auto dataset = Dataset(f, "/diagnostics", {diagnostics.size()});
|
||||
ColumnText(dataset.Get(), "severity", severity);
|
||||
ColumnText(dataset.Get(), "code", code);
|
||||
ColumnText(dataset.Get(), "file", file);
|
||||
Id(dataset.Get(), "line", line);
|
||||
ColumnText(dataset.Get(), "keyword", keyword);
|
||||
ColumnText(dataset.Get(), "entity_identity", entity);
|
||||
ColumnText(dataset.Get(), "message", message);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
U64 GlobalThermalMaterialId(const Domain& domain, std::size_t local) {
|
||||
Require(local < domain.ThermalMaterials().Size(),
|
||||
"Invalid local thermal material index.");
|
||||
const auto* thermal = &domain.ThermalMaterials()[local];
|
||||
for (std::size_t i = 0; i < domain.Materials().Size(); ++i)
|
||||
if (&domain.Materials()[i] == thermal) return i;
|
||||
throw Hdf5Failure{"Thermal material has no global Domain identity."};
|
||||
}
|
||||
|
||||
Status ValidateThermalOutput(const Domain& domain,
|
||||
const ThermalAnalysisState& state) {
|
||||
try {
|
||||
Require(state.IsComplete() && domain.ThermalStep() != nullptr &&
|
||||
domain.Steps().Size() == 0,
|
||||
"Thermal output requires one complete thermal history.");
|
||||
auto model = AnalysisModel::Create(domain);
|
||||
Require(model.HasValue() && model.Value().IsThermal(),
|
||||
"Invalid thermal Domain.");
|
||||
auto dofs = DofManager::CreateThermal(model.Value());
|
||||
Require(dofs.HasValue(), "Invalid thermal equation inventory.");
|
||||
Require(state.NodeOrder() == model.Value().ActiveThermalNodes() &&
|
||||
state.ElementOrder() == model.Value().ActiveThermalElements(),
|
||||
"Active Domain/state inventories differ.");
|
||||
Require(state.NominalTimeIncrement() ==
|
||||
domain.ThermalStep()->nominal_increment &&
|
||||
state.TimePeriod() == domain.ThermalStep()->time_period,
|
||||
"Domain/state schedules differ.");
|
||||
const auto grid =
|
||||
ThermalTimeGrid::Create(domain.ThermalStep()->time_period,
|
||||
domain.ThermalStep()->nominal_increment,
|
||||
domain.ThermalStep()->maximum_increments);
|
||||
Require(grid.HasValue() && grid.Value().Count() == state.TimeGrid().Count(),
|
||||
"Domain/state increment inventory differs.");
|
||||
for (std::size_t i = 0; i < state.NodeOrder().size(); ++i)
|
||||
Require(SameIdentity(state.NodeSourceIds()[i],
|
||||
domain.Nodes()[state.NodeOrder()[i]].source_id),
|
||||
"Node source identity differs.");
|
||||
for (std::size_t i = 0; i < state.ElementOrder().size(); ++i)
|
||||
Require(SameIdentity(
|
||||
state.ElementSourceIds()[i],
|
||||
domain.ThermalElements()[state.ElementOrder()[i]].SourceId()),
|
||||
"Element source identity differs.");
|
||||
std::vector<std::array<std::uint8_t, 5>> mask(state.NodeOrder().size());
|
||||
for (const auto row : dofs.Value().ConstrainedDofs())
|
||||
mask[row / 5][row % 5] = 1;
|
||||
Require(mask == state.ConstraintMask(),
|
||||
"Prescribed temperature inventory differs.");
|
||||
for (const auto& frame : state.AcceptedFrames())
|
||||
if (!frame.is_initial)
|
||||
for (std::size_t i = 0; i < dofs.Value().ConstrainedDofs().size(); ++i)
|
||||
Require(frame.temperature[dofs.Value().ConstrainedDofs()[i]] ==
|
||||
dofs.Value().PrescribedValues()[i],
|
||||
"Completed prescribed temperature differs from Domain.");
|
||||
Vector raw{dofs.Value().FullDofCount()};
|
||||
for (const auto& initial : domain.ThermalInitialConditions()) {
|
||||
const auto at = std::find(state.NodeOrder().begin(),
|
||||
state.NodeOrder().end(), initial.node_index);
|
||||
if (at != state.NodeOrder().end()) {
|
||||
const auto row =
|
||||
static_cast<std::size_t>(at - state.NodeOrder().begin());
|
||||
for (std::size_t station = 0; station < 5; ++station)
|
||||
raw[5 * row + station] = initial.station_values[station];
|
||||
}
|
||||
}
|
||||
const auto& actual = state.AcceptedFrames().front().temperature;
|
||||
Require(actual.Size() == raw.Size(), "Raw initial shape differs.");
|
||||
for (std::size_t i = 0; i < raw.Size(); ++i)
|
||||
Require(actual[i] == raw[i],
|
||||
"Raw initial temperatures differ from Domain.");
|
||||
return Status::Ok();
|
||||
} catch (const std::exception& e) {
|
||||
return OutputFailure("invalid-thermal-output-inventory", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
Status SelfCheckThermalFile(const std::filesystem::path& path,
|
||||
const Domain& domain,
|
||||
const ThermalAnalysisState& state,
|
||||
const std::vector<Diagnostic>& diagnostics,
|
||||
herr_t (*close_reader)(hid_t)) {
|
||||
const auto inventory = ValidateThermalOutput(domain, state);
|
||||
if (!inventory.IsOk()) return inventory;
|
||||
try {
|
||||
Hdf5ErrorSilencer silence;
|
||||
Require(close_reader != nullptr, "Missing thermal reader closer.");
|
||||
Hdf5Handle file{RequireHdf5Id(H5Fopen(path.string().c_str(), H5F_ACC_RDONLY,
|
||||
H5P_DEFAULT),
|
||||
"Cannot reopen thermal candidate."),
|
||||
close_reader};
|
||||
Metadata(file.Get(), domain, state);
|
||||
ModelTables(file.Get(), domain);
|
||||
Mappings(file.Get(), state);
|
||||
Frames(file.Get(), state);
|
||||
Diagnostics(file.Get(), diagnostics);
|
||||
RequireHdf5(file.CloseChecked(), "Cannot close checked thermal candidate.");
|
||||
return Status::Ok();
|
||||
} catch (const std::exception& e) {
|
||||
return OutputFailure("invalid-thermal-hdf5-candidate", e.what());
|
||||
}
|
||||
}
|
||||
} // namespace fesa::hdf5_internal
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef FESA_IO_HDF5_THERMAL_HDF5_SELF_CHECK_H_
|
||||
#define FESA_IO_HDF5_THERMAL_HDF5_SELF_CHECK_H_
|
||||
|
||||
#include <hdf5.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#include "fesa/analysis/thermal_analysis_state.h"
|
||||
#include "fesa/core/status.h"
|
||||
#include "fesa/model/domain.h"
|
||||
|
||||
namespace fesa {
|
||||
struct Diagnostic;
|
||||
}
|
||||
namespace fesa::hdf5_internal {
|
||||
/// @brief Supplies private lifecycle fault seams around the real HDF5
|
||||
/// operations.
|
||||
/// @note Production uses default closers and no callbacks. Test closers must
|
||||
/// release their handle even when reporting an injected close failure.
|
||||
struct ThermalFileHooks {
|
||||
herr_t (*candidate_close)(hid_t) = H5Fclose;
|
||||
herr_t (*reader_close)(hid_t) = H5Fclose;
|
||||
std::function<void(hid_t)> before_write;
|
||||
std::function<void(const std::filesystem::path&)> before_self_check;
|
||||
std::function<void(const std::filesystem::path&)> before_finalize;
|
||||
};
|
||||
/// @brief Executes the actual candidate/write/check/close/finalize lifecycle.
|
||||
Status WriteThermalFile(const std::filesystem::path& output_path,
|
||||
const Domain& domain, const ThermalAnalysisState& state,
|
||||
const std::vector<Diagnostic>& diagnostics,
|
||||
const ThermalFileHooks& hooks = {});
|
||||
/// @brief Maps section-local thermal material indices into the global Domain
|
||||
/// table.
|
||||
std::uint64_t GlobalThermalMaterialId(const Domain& domain, std::size_t local);
|
||||
/// @brief Checks supplied immutable source/initial/schedule inventories before
|
||||
/// writing.
|
||||
Status ValidateThermalOutput(const Domain& domain,
|
||||
const ThermalAnalysisState& state);
|
||||
/// @brief Checks every mandatory thermal field, value and attribute before
|
||||
/// publication.
|
||||
Status SelfCheckThermalFile(const std::filesystem::path& path,
|
||||
const Domain& domain,
|
||||
const ThermalAnalysisState& state,
|
||||
const std::vector<Diagnostic>& diagnostics,
|
||||
herr_t (*close_reader)(hid_t) = H5Fclose);
|
||||
} // namespace fesa::hdf5_internal
|
||||
#endif // FESA_IO_HDF5_THERMAL_HDF5_SELF_CHECK_H_
|
||||
Reference in New Issue
Block a user