Skip to content

Commit 0c9462d

Browse files
committed
wip vtkh5
1 parent bce6d0c commit 0c9462d

29 files changed

+1323
-362
lines changed

pyphare/pyphare/pharein/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ def as_paths(rb):
331331
)
332332

333333
if len(simulation.diagnostics) > 0:
334+
if simulation.diag_options is not None and "format" in simulation.diag_options:
335+
add_string(diag_path + "format", simulation.diag_options["format"])
336+
334337
if simulation.diag_options is not None and "options" in simulation.diag_options:
335338
add_string(
336339
diag_path + "filePath", simulation.diag_options["options"]["dir"]

pyphare/pyphare/pharein/simulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def check_directory(directory, key):
489489
# diag_options = {"format":"phareh5", "options": {"dir": "phare_ouputs/"}}
490490
def check_diag_options(**kwargs):
491491
diag_options = kwargs.get("diag_options", None)
492-
formats = ["phareh5"]
492+
formats = ["phareh5", "pharevtkh5"]
493493
if diag_options is not None and "format" in diag_options:
494494
if diag_options["format"] not in formats:
495495
raise ValueError("Error - diag_options format is invalid")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import tovtk
2+
3+
__all__ = ["tovtk"]

pyphare/pyphare/pharesee/tovtk.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
# -*- coding: utf-8 -*-
33

44

5-
import h5py
5+
import os
66
import sys
77
import numpy as np
8-
import os
98

109

1110
def ndim_from(npx, npy, npz):
@@ -15,14 +14,12 @@ def ndim_from(npx, npy, npz):
1514
return 2
1615
elif npx > 2 and npy == 2 and npz == 2:
1716
return 1
18-
else:
19-
raise ValueError(
20-
f" cannot infer dimension from (npx, npy, npz) = {npx} {npy} {npz}"
21-
)
17+
raise ValueError(
18+
f" cannot infer dimension from (npx, npy, npz) = {npx} {npy} {npz}"
19+
)
2220

2321

2422
def BtoFlatPrimal(ph_bx, ph_by, ph_bz, npx, npy, npz, gn=2):
25-
2623
nbrPoints = npx * npy * npz
2724
b = np.zeros((nbrPoints, 3), dtype="f")
2825

@@ -114,7 +111,6 @@ def BtoFlatPrimal(ph_bx, ph_by, ph_bz, npx, npy, npz, gn=2):
114111

115112

116113
def EtoFlatPrimal(ph_ex, ph_ey, ph_ez, npx, npy, npz, gn=2):
117-
118114
nbrPoints = npx * npy * npz
119115
e = np.zeros((nbrPoints, 3), dtype="f")
120116

@@ -168,7 +164,6 @@ def EtoFlatPrimal(ph_ex, ph_ey, ph_ez, npx, npy, npz, gn=2):
168164

169165

170166
def primalScalarToFlatPrimal(ph_scalar, npx, npy, npz, gn=2):
171-
172167
scalar3d = np.zeros((npx, npy, npz), dtype="f")
173168
if ndim_from(npx, npy, npz) == 2:
174169
domain = slice(gn, -gn)
@@ -271,14 +266,9 @@ def make3d(root_spacing):
271266
return root_spacing
272267

273268

274-
def main():
269+
def convert(path):
270+
import h5py # see doc/conventions.md section 2.1.1
275271

276-
if len(sys.argv) != 2 or sys.argv[1] in ["-h", "--help"]:
277-
print(f"Usage: {os.path.basename(sys.argv[0])} <path_to_phare_h5>")
278-
print("Works for EM fields, bulk velocity and density")
279-
sys.exit(1)
280-
281-
path = sys.argv[1]
282272
phare_h5 = h5py.File(path, "r")
283273
root_spacing = make3d(phare_h5.attrs["cell_width"])
284274
times = times_in(phare_h5)
@@ -316,7 +306,6 @@ def main():
316306
steps.create_dataset("Values", data=times[:numberOfTimes])
317307

318308
for ilvl in range(max_nbr_level)[:]:
319-
320309
print(f"Processing level {ilvl}")
321310

322311
lvl = root.create_group(f"Level{ilvl}")
@@ -428,4 +417,9 @@ def main():
428417

429418

430419
if __name__ == "__main__":
431-
main()
420+
if len(sys.argv) != 2 or sys.argv[1] in ["-h", "--help"]:
421+
print(f"Usage: {os.path.basename(sys.argv[0])} <path_to_phare_h5>")
422+
print("Works for EM fields, bulk velocity and density")
423+
sys.exit(1)
424+
425+
convert(sys.argv[1])

src/core/data/grid/gridlayout.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,10 @@ namespace core
897897
return GridLayoutImpl::centering(hybridQuantity);
898898
}
899899

900+
NO_DISCARD constexpr static auto centering(auto const& hasQuantity)
901+
{
902+
return centering(hasQuantity.physicalQuantity());
903+
}
900904

901905
/**
902906
* @brief GridLayout<GridLayoutImpl::dim>::allocSize

src/core/data/ndarray/ndarray_vector.hpp

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,67 +16,63 @@
1616

1717
namespace PHARE::core
1818
{
19-
template<std::size_t dim, bool c_ordering = true, typename DataType = double>
19+
template<std::size_t dim, bool c_ordering = true>
2020
struct NdArrayViewer
2121
{
22-
template<typename NCells, typename... Indexes>
23-
NO_DISCARD static DataType const& at(DataType const* data, NCells const& nCells,
24-
Indexes const&... indexes)
25-
{
26-
auto params = std::forward_as_tuple(indexes...);
27-
static_assert(sizeof...(Indexes) == dim);
28-
// static_assert((... && std::is_unsigned_v<decltype(indexes)>)); TODO : manage later if
29-
// this test should be included
22+
using Id = std::uint16_t;
23+
using Idx = Id const;
3024

25+
template<typename NCells, template<typename, std::size_t> typename Indexes, typename Index>
26+
static inline std::uint32_t idx(NCells const& nCells, Indexes<Index, dim> const& indexes)
27+
{
3128
if constexpr (dim == 1)
32-
{
33-
auto i = std::get<0>(params);
29+
return idx(nCells, indexes[0]);
3430

35-
return data[i];
36-
}
31+
else if constexpr (dim == 2)
32+
return idx(nCells, indexes[0], indexes[1]);
3733

38-
if constexpr (dim == 2)
39-
{
40-
auto i = std::get<0>(params);
41-
auto j = std::get<1>(params);
34+
else if constexpr (dim == 3)
35+
return idx(nCells, indexes[0], indexes[1], indexes[2]);
36+
}
4237

43-
if constexpr (c_ordering)
44-
return data[j + i * nCells[1]];
45-
else
46-
return data[i + j * nCells[0]];
47-
}
38+
static inline std::uint32_t idx(auto const /*nCells*/, Idx i) { return i; }
4839

49-
if constexpr (dim == 3)
50-
{
51-
auto i = std::get<0>(params);
52-
auto j = std::get<1>(params);
53-
auto k = std::get<2>(params);
54-
55-
if constexpr (c_ordering)
56-
return data[k + j * nCells[2] + i * nCells[1] * nCells[2]];
57-
else
58-
return data[i + j * nCells[0] + k * nCells[1] * nCells[0]];
59-
}
40+
static inline std::uint32_t idx(auto const nCells, Idx i, Idx j)
41+
{
42+
if constexpr (c_ordering)
43+
return j + i * nCells[1];
44+
else
45+
return i + j * nCells[0];
46+
}
47+
static inline std::uint32_t idx(auto const nCells, Idx i, Idx j, Idx k)
48+
{
49+
if constexpr (c_ordering)
50+
return k + j * nCells[2] + i * nCells[1] * nCells[2];
51+
else
52+
return i + j * nCells[0] + k * nCells[1] * nCells[0];
6053
}
6154

62-
template<typename NCells, template<typename, std::size_t> typename Indexes, typename Index>
63-
NO_DISCARD static DataType const& at(DataType const* data, NCells const& nCells,
64-
Indexes<Index, dim> const& indexes)
55+
template<template<typename, std::size_t> typename Indexes, typename Index>
56+
NO_DISCARD static inline auto& at(auto* data, auto const& nCells,
57+
Indexes<Index, dim> const& indexes)
6558

6659
{
67-
if constexpr (dim == 1)
68-
return at(data, nCells, indexes[0]);
69-
70-
else if constexpr (dim == 2)
71-
return at(data, nCells, indexes[0], indexes[1]);
60+
auto const& i = idx(nCells, indexes);
61+
assert(i < product(nCells, std::uint32_t{1}));
62+
return data[i];
63+
}
7264

73-
else if constexpr (dim == 3)
74-
return at(data, nCells, indexes[0], indexes[1], indexes[2]);
65+
static inline auto& at(auto* data, auto const nCells, auto const... indexes)
66+
{
67+
auto const& i = idx(nCells, indexes...);
68+
assert(i < product(nCells, std::uint32_t{1}));
69+
return data[i];
7570
}
7671
};
7772

7873

7974

75+
8076
template<typename Array, typename Mask>
8177
class MaskedView
8278
{
@@ -102,7 +98,7 @@ class MaskedView
10298
template<typename... Indexes>
10399
NO_DISCARD DataType const& operator()(Indexes... indexes) const
104100
{
105-
return NdArrayViewer<dimension, true, DataType>::at(array_.data(), shape_, indexes...);
101+
return NdArrayViewer<dimension, true>::at(array_.data(), shape_, indexes...);
106102
}
107103

108104
template<typename... Indexes>
@@ -139,7 +135,7 @@ class NdArrayView
139135
static std::size_t const dimension = dim;
140136
using type = DataType;
141137
using pointer_type = DataType*;
142-
using viewer = NdArrayViewer<dim, c_ordering, DataType>;
138+
using viewer = NdArrayViewer<dim, c_ordering>;
143139

144140
explicit NdArrayView(pointer_type ptr, std::array<std::uint32_t, dim> const& nCells)
145141
: ptr_{ptr}
@@ -272,7 +268,7 @@ class NdArrayVector
272268
template<typename... Indexes>
273269
NO_DISCARD DataType const& operator()(Indexes... indexes) const
274270
{
275-
return NdArrayViewer<dim, c_ordering, DataType>::at(data_.data(), nCells_, indexes...);
271+
return NdArrayViewer<dim, c_ordering>::at(data_.data(), nCells_, indexes...);
276272
}
277273

278274
template<typename... Indexes>
@@ -284,7 +280,7 @@ class NdArrayVector
284280
template<typename Index>
285281
NO_DISCARD DataType const& operator()(std::array<Index, dim> const& indexes) const
286282
{
287-
return NdArrayViewer<dim, c_ordering, DataType>::at(data_.data(), nCells_, indexes);
283+
return NdArrayViewer<dim, c_ordering>::at(data_.data(), nCells_, indexes);
288284
}
289285

290286
template<typename Index>

src/core/utilities/box/box.hpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,27 @@ struct Box
9292
return *this;
9393
}
9494

95+
96+
NO_DISCARD auto shape(std::size_t const i) const { return upper[i] - lower[i] + 1; }
9597
NO_DISCARD auto shape() const { return upper - lower + 1; }
96-
NO_DISCARD auto size() const { return core::product(shape()); }
98+
NO_DISCARD auto size() const { return core::product(shape(), std::size_t{1}); }
9799

100+
NO_DISCARD Type rows() const
101+
{
102+
if constexpr (dim == 1)
103+
return 1;
104+
if constexpr (dim == 2)
105+
return shape(0);
106+
if constexpr (dim == 3)
107+
return shape(0) * shape(1);
108+
}
109+
NO_DISCARD Type slabs() const
110+
{
111+
if constexpr (dim == 1 || dim == 2)
112+
return 1;
113+
if constexpr (dim == 3)
114+
return shape(0);
115+
}
98116

99117
using iterator = box_iterator<Type, dim>;
100118
NO_DISCARD auto begin() { return iterator{this, lower}; }
@@ -188,6 +206,39 @@ class box_iterator
188206
return *this;
189207
}
190208

209+
auto& operator+=(std::uint32_t s)
210+
{
211+
auto lo = box_->upper;
212+
for (std::uint16_t d = 0; d < dim; ++d)
213+
lo[d] += 1 - box_->lower[d];
214+
if constexpr (dim == 3)
215+
{
216+
{
217+
auto const prod = lo[1] * lo[2];
218+
auto const div = s / prod;
219+
s -= div * prod;
220+
index_[0] += div;
221+
}
222+
auto const div = s / lo[2];
223+
s -= div * lo[2];
224+
index_[1] += div;
225+
}
226+
if constexpr (dim == 2)
227+
{
228+
auto const div = s / lo[1];
229+
s -= div * lo[1];
230+
index_[0] += div;
231+
}
232+
index_[dim - 1] += s;
233+
return *this;
234+
}
235+
auto operator+(std::uint32_t const s) const
236+
{
237+
auto copy = *this;
238+
copy += s;
239+
return copy;
240+
}
241+
191242
bool operator!=(box_iterator const& other) const
192243
{
193244
return box_ != other.box_ or index_ != other.index_;

src/core/utilities/mpi_utils.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22
#define PHARE_CORE_UTILITIES_MPI_HPP
33

44
#include "core/def.hpp"
5-
#include <chrono>
5+
#include "core/def/phare_mpi.hpp"
6+
#include "core/utilities/span.hpp"
7+
#include "core/utilities/types.hpp"
8+
69
#include <vector>
710
#include <string>
811
#include <cassert>
912
#include <cstring>
10-
#include <exception>
11-
12-
13-
#include "core/def/phare_mpi.hpp"
14-
#include "core/utilities/span.hpp"
15-
#include "core/utilities/types.hpp"
1613

1714
namespace PHARE::core::mpi
1815
{

src/core/utilities/types.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ namespace core
221221

222222

223223

224+
template<typename T>
225+
NO_DISCARD T from_string(std::string const& s)
226+
{
227+
T t;
228+
std::stringstream ss(s);
229+
ss >> t;
230+
return t;
231+
}
232+
233+
224234
NO_DISCARD inline std::optional<std::string> get_env(std::string const& key)
225235
{
226236
if (char const* val = std::getenv(key.c_str()))
@@ -235,6 +245,14 @@ namespace core
235245
return _default;
236246
}
237247

248+
template<typename T>
249+
NO_DISCARD inline T get_env_as(std::string const& key, T const& t)
250+
{
251+
if (auto e = get_env(key))
252+
return from_string<T>(*e);
253+
return t;
254+
}
255+
238256

239257

240258
} // namespace core

0 commit comments

Comments
 (0)