64namespace ca_npy_detail {
69 using U = std::remove_cv_t<T>;
70 static_assert(std::is_arithmetic_v<U>,
"write_npy requires an arithmetic cell state");
72 if constexpr (std::is_same_v<U, bool>)
74 else if constexpr (std::is_floating_point_v<U>)
75 return (std::endian::native == std::endian::little ?
"<f" :
">f") + std::to_string(
sizeof(
U));
76 else if constexpr (std::is_signed_v<U>)
77 return (
sizeof(
U) == 1 ?
"|i" : (std::endian::native == std::endian::little ?
"<i" :
">i"))
78 + std::to_string(
sizeof(
U));
80 return (
sizeof(
U) == 1 ?
"|u" : (std::endian::native == std::endian::little ?
"<u" :
">u"))
81 + std::to_string(
sizeof(
U));
87 using U = std::remove_cv_t<T>;
88 if constexpr (std::is_same_v<U, bool>)
90 const std::uint8_t
byte =
value ? 1u : 0u;
91 out.write(
reinterpret_cast<const char *
>(&
byte), 1);
95 out.write(
reinterpret_cast<const char *
>(&
value),
static_cast<std::streamsize
>(
sizeof(
U)));
99template <std::
size_t Dim,
typename Lattice>
118template <
typename Lattice>
121 std::ostringstream
out;
150template <
typename Lattice>
155 static_assert(std::is_arithmetic_v<state_t>,
"write_npy requires arithmetic cell states");
157 std::string header =
"{'descr': '" + ca_npy_detail::dtype_descr<state_t>()
160 constexpr std::size_t prelude = 10;
161 const std::size_t
rem = (prelude + header.size() + 1) % 16;
164 header.push_back(
'\n');
168 static constexpr unsigned char magic[] = {0x93,
'N',
'U',
'M',
'P',
'Y'};
169 out.write(
reinterpret_cast<const char *
>(magic), 6);
170 out.put(
static_cast<char>(1));
171 out.put(
static_cast<char>(0));
172 const std::uint16_t len =
static_cast<std::uint16_t
>(header.size());
173 out.put(
static_cast<char>(len & 0xffu));
174 out.put(
static_cast<char>((len >> 8) & 0xffu));
175 out.write(header.data(),
static_cast<std::streamsize
>(header.size()));
178 ca_npy_detail::write_payload_rec<0>(
out, frame, coord);
202 template <
typename Lattice>
206 if (
const auto parent = path.parent_path();
not parent.empty())
207 std::filesystem::create_directories(parent);
208 std::ofstream
out(path, std::ios::binary);
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
size_t size_t int32_t value
size_t size_t int32_t * out
Directory-backed frame sinks for CA trajectories.
Lattice that adds boundary-aware access on top of a storage.
typename Storage::state_type state_type
typename Storage::coord_type coord_type
static constexpr std::size_t rank
ca_size_t size() const noexcept
state_type at(const coord_type &c) const
Strict access: throws if c is out of range.
Path-pattern sink that writes one .npy file per frame.
std::filesystem::path pattern_
Npy_Frame_Sink(std::filesystem::path path_pattern, const std::size_t zero_pad=6)
Build an NPY frame sink.
void accept(const std::size_t step, const Lattice &frame)
Write one NPY frame.
void flush()
NPY frames are written eagerly; this is a no-op.
Storage + topology that carries the cell values.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
std::string dtype_descr()
void write_scalar(std::ostream &out, const T &value)
void write_payload_rec(std::ostream &out, const Lattice &frame, typename Lattice::coord_type &coord)
std::string shape_tuple(const Lattice &frame)
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
std::filesystem::path format_step_path(const std::filesystem::path &pattern, const std::size_t step, const std::size_t zero_pad=6)
Format a step-indexed file path.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
void write_npy(std::ostream &out, const Lattice &frame)
Write a lattice frame as a NumPy .npy array.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
C++20 concepts for the Cellular Automata module.