Skip to content

Commit 7ba726f

Browse files
committed
++
1 parent e43f97f commit 7ba726f

File tree

9 files changed

+128
-45
lines changed

9 files changed

+128
-45
lines changed

src/amr/physical_models/hybrid_model.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ class HybridModel : public IPhysicalModel<AMR_Types>
2828
using Interface = IPhysicalModel<AMR_Types>;
2929
using amr_types = AMR_Types;
3030
using electrons_t = Electrons;
31-
using patch_t = typename AMR_Types::patch_t;
32-
using level_t = typename AMR_Types::level_t;
31+
using patch_t = AMR_Types::patch_t;
32+
using level_t = AMR_Types::level_t;
3333
using gridlayout_type = GridLayoutT;
3434
using electromag_type = Electromag;
35-
using vecfield_type = typename Electromag::vecfield_type;
36-
using field_type = typename vecfield_type::field_type;
35+
using vecfield_type = Electromag::vecfield_type;
36+
using field_type = vecfield_type::field_type;
3737
using grid_type = Grid_t;
3838
using ions_type = Ions;
39-
using particle_array_type = typename Ions::particle_array_type;
39+
using particle_array_type = Ions::particle_array_type;
4040
using resources_manager_type = amr::ResourcesManager<gridlayout_type, grid_type>;
4141
using ParticleInitializerFactory
4242
= core::ParticleInitializerFactory<particle_array_type, gridlayout_type>;

src/amr/resources_manager/amr_utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ namespace amr
248248
iLevel++)
249249
{
250250
visitLevel<GridLayout>(*hierarchy.getPatchLevel(iLevel), resman,
251-
std::forward<Action>(action), std::forward<Args...>(args...));
251+
std::forward<Action>(action), std::forward<Args>(args)...);
252252
}
253253
}
254254

src/core/data/grid/gridlayout.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,8 @@ namespace core
11851185
}));
11861186
}
11871187

1188+
1189+
11881190
template<typename Field, typename Fn>
11891191
void evalOnBox(Field& field, Fn&& fn) const
11901192
{

src/core/data/tensorfield/tensorfield.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ class TensorField
207207
NO_DISCARD auto& physicalQuantity() const { return qty_; }
208208
NO_DISCARD auto constexpr static size() { return N; }
209209

210+
NO_DISCARD auto static constexpr size() { return N; }
211+
210212
private:
211213
auto static _get_index_for(Component component)
212214
{

src/core/utilities/algorithm.hpp

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#ifndef PHARE_ALGORITHM_HPP
22
#define PHARE_ALGORITHM_HPP
33

4+
#include "core/data/field/field.hpp"
5+
#include "core/data/grid/gridlayoutdefs.hpp"
6+
#include "core/data/tensorfield/tensorfield.hpp"
47
#include "core/def.hpp"
58
#include "core/utilities/span.hpp"
69

710

811

9-
#include <algorithm>
1012
#include <string>
13+
#include <algorithm>
1114

1215
namespace PHARE
1316
{
@@ -64,6 +67,73 @@ void average(Span const& f1, Span const& f2, Span& avg)
6467
av[i] = (d1[i] + d2[i]) * .5;
6568
}
6669

70+
71+
template<typename PhysicalQuantity>
72+
auto convert_to_primal( //
73+
auto const& src, //
74+
auto const& layout, //
75+
auto const lix, //
76+
PhysicalQuantity const qty //
77+
)
78+
{
79+
using PQ = PhysicalQuantity;
80+
constexpr std::array valid_quantities{PQ::Bx, PQ::By, PQ::Bz, PQ::Ex, PQ::Ey, PQ::Ez};
81+
82+
if (qty == PQ::Ex)
83+
return layout.project(src, lix, layout.ExToMoments());
84+
else if (qty == PQ::Ey)
85+
return layout.project(src, lix, layout.EyToMoments());
86+
else if (qty == PQ::Ez)
87+
return layout.project(src, lix, layout.EzToMoments());
88+
89+
return 1e-5; // todo
90+
}
91+
92+
template<std::size_t dim, typename... Ts>
93+
auto& _convert_to_fortran_primal( // DOES NOT WORK ON GHOST BOX!
94+
Field<dim, Ts...>& dst, // TEMPORARY TYPE - Quantity not valid!
95+
Field<dim, Ts...> const& src, //
96+
auto const& layout //
97+
)
98+
{
99+
bool static constexpr c_ordering = false;
100+
auto const qty = src.physicalQuantity();
101+
dst.setShape(src.shape());
102+
103+
auto lb_view = core::make_array_view<c_ordering>(dst.data(), src.shape());
104+
auto const all_primal
105+
= all(layout.centering(src), [](auto const c) { return c == QtyCentering::primal; });
106+
auto const& amr_gbox = layout.AMRGhostBoxFor(src);
107+
auto const lcl_box = layout.AMRToLocal(layout.AMRBoxFor(src));
108+
109+
if (all_primal)
110+
for (auto const lix : lcl_box)
111+
dst(lix) = src(lix);
112+
else
113+
for (auto const lix : lcl_box)
114+
dst(lix) = convert_to_primal(src, layout, lix, qty);
115+
116+
return dst;
117+
}
118+
119+
template<typename Field_t, typename PQ, std::size_t rank>
120+
auto& _convert_to_fortran_primal( //
121+
TensorField<Field_t, PQ, rank>& dst, //
122+
TensorField<Field_t, PQ, rank> const& src, //
123+
auto const& layout //
124+
)
125+
{
126+
for (std::size_t ci = 0; ci < src.size(); ++ci)
127+
_convert_to_fortran_primal(dst[ci], src[ci], layout);
128+
return dst;
129+
}
130+
131+
auto& convert_to_fortran_primal(auto& dst, auto const& src, auto const& layout)
132+
{
133+
return _convert_to_fortran_primal(dst, src, layout);
134+
}
135+
136+
67137
} // namespace PHARE::core
68138

69139
#endif

src/core/utilities/types.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ constexpr auto for_N_make_array(Fn&& fn)
506506
return for_N<N, for_N_R_mode::make_array>(fn);
507507
}
508508

509+
509510
template<std::uint16_t N, typename Fn>
510511
NO_DISCARD constexpr auto for_N_all(Fn&& fn)
511512
{

src/diagnostic/detail/vtkh5_type_writer.hpp

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define PHARE_DIAGNOSTIC_DETAIL_VTK_H5_TYPE_WRITER_HPP
33

44
#include "core/logger.hpp"
5+
#include "core/utilities/algorithm.hpp"
56
#include "core/utilities/box/box.hpp"
67
#include "core/utilities/mpi_utils.hpp"
78
#include "core/data/tensorfield/tensorfield.hpp"
@@ -18,7 +19,7 @@
1819

1920
// TODO:
2021
// remove logging
21-
// write as column major/Fortran memory ordering
22+
// write as column major/Fortran memory ordering - done. but badly
2223

2324

2425
namespace PHARE::diagnostic::vtkh5::detail
@@ -45,7 +46,7 @@ class H5TypeWriter : public PHARE::diagnostic::TypeWriter
4546

4647
public:
4748
static constexpr auto dimension = Writer::dimension;
48-
using Attributes = typename Writer::Attributes;
49+
using GridLayout = Writer::GridLayout;
4950
using Box_t = core::Box<int, dimension>;
5051

5152
H5TypeWriter(Writer& h5Writer)
@@ -89,8 +90,9 @@ class H5TypeWriter : public PHARE::diagnostic::TypeWriter
8990

9091
struct VTKFileFieldInfo // assumes all primal
9192
{
92-
VTKFileFieldInfo(auto const& field, auto const& layout)
93-
: lvl{std::to_string(layout.levelNumber())}
93+
VTKFileFieldInfo(auto const& field, auto const& lyout)
94+
: lvl{std::to_string(lyout.levelNumber())}
95+
, layout{lyout}
9496
, ghost_box{layout.AMRGhostBoxFor(field)}
9597
, local_box{layout.AMRToLocal(core::grow(ghost_box, -1 * layout.nbrGhosts()))}
9698
{
@@ -116,6 +118,7 @@ class H5TypeWriter : public PHARE::diagnostic::TypeWriter
116118
}
117119

118120
std::string lvl;
121+
GridLayout const& layout;
119122
Box_t const ghost_box;
120123
core::Box<std::uint32_t, dimension> const local_box;
121124
std::uint32_t const primal_row_len = local_box.shape(dimension - 1);
@@ -127,17 +130,12 @@ class H5TypeWriter : public PHARE::diagnostic::TypeWriter
127130
{
128131
void write2D(auto const& field)
129132
{
130-
PHARE_LOG_LINE_SS(finfo.ghost_box << " " << finfo.local_box << " "
131-
<< finfo.primal_row_len);
132133
auto ds = fw->h5file.getDataSet(finfo.path);
133134
auto const write = [&]() {
134135
auto bit = finfo.local_box.begin();
135136
for (std::uint32_t i = 0; i < finfo.local_box.rows();
136137
++i, bit += finfo.primal_row_len, data_offset += finfo.primal_row_len)
137-
{
138-
PHARE_LOG_LINE_SS(data_offset << " " << finfo.primal_row_len << " " << *bit);
139138
ds.select({data_offset}, {finfo.primal_row_len}).write_raw(&field(*bit));
140-
}
141139
};
142140
write();
143141
write();
@@ -155,10 +153,12 @@ class H5TypeWriter : public PHARE::diagnostic::TypeWriter
155153
}
156154
void operator()(auto const& field)
157155
{
156+
auto& tmp = fw->typewriter->h5Writer_.modelView().tmpField();
157+
auto const frimal = core::convert_to_fortran_primal(tmp, field, finfo.layout);
158158
if constexpr (dimension == 2)
159-
write2D(field);
159+
write2D(frimal);
160160
if constexpr (dimension == 3)
161-
write3D(field);
161+
write3D(frimal);
162162
}
163163

164164
VTKFileWriter* fw;
@@ -201,6 +201,8 @@ class H5TypeWriter : public PHARE::diagnostic::TypeWriter
201201
}
202202
void operator()(auto const& tf)
203203
{
204+
auto& tmp = fw->typewriter->h5Writer_.modelView().template tmpTensorField<rank>();
205+
auto const frimal = core::convert_to_fortran_primal(tmp, tf, finfo.layout);
204206
if constexpr (dimension == 2)
205207
write2D(tf);
206208
if constexpr (dimension == 3)
@@ -257,17 +259,13 @@ class H5TypeWriter : public PHARE::diagnostic::TypeWriter
257259

258260
void writeField(auto const& field, auto const& layout)
259261
{
260-
PHARE_LOG_LINE_SS("writeField " << field.name());
261262
VTKFileFieldWriter{this, {field, layout}}(field);
262-
PHARE_LOG_LINE_SS("done");
263263
}
264264

265265
template<std::size_t rank = 2> // TODO convert duals to primals
266266
void writeTensorField(auto const& tf, auto const& layout)
267267
{
268-
PHARE_LOG_LINE_SS("writeTensorField " << tf.name());
269268
VTKFileTensorFieldWriter<rank>{this, {tf[0], layout}}(tf);
270-
PHARE_LOG_LINE_SS("done");
271269
}
272270

273271
template<typename T = FloatType>
@@ -308,15 +306,13 @@ class H5TypeWriter : public PHARE::diagnostic::TypeWriter
308306

309307
void initFieldFileLevel(int const level, auto& boxes)
310308
{
311-
PHARE_LOG_LINE_SS("initFieldFileLevel");
312309
initDSDefault(level_base + std::to_string(level) + "/PointData/data");
313310
resize(level, boxes);
314311
}
315312

316313
template<std::size_t rank = 2>
317314
void initTensorFieldFileLevel(auto const level, auto& boxes)
318315
{
319-
PHARE_LOG_LINE_SS("initTensorFieldFileLevel");
320316
auto constexpr N = core::detail::tensor_field_dim_from_rank<rank>();
321317
auto const path = level_base + std::to_string(level) + "/PointData/data";
322318
h5file.template create_chunked_data_set<FloatType>(
@@ -336,7 +332,6 @@ class H5TypeWriter : public PHARE::diagnostic::TypeWriter
336332
// and 1d data is * 4
337333
constexpr static auto X_TIMES = std::array{4, 2, /* 3d noop */ 1}[dimension - 1];
338334

339-
PHARE_LOG_LINE_SS("resize_data");
340335
auto const lvl = std::to_string(ilvl);
341336
auto const data_path = level_base + lvl + "/PointData/data";
342337
auto point_data_ds = h5file.getDataSet(data_path);
@@ -353,21 +348,17 @@ class H5TypeWriter : public PHARE::diagnostic::TypeWriter
353348
core::sum_from(boxes, [](auto const& b) { return b.size() * X_TIMES; }));
354349
auto const new_size = data_offset + core::sum(rank_data_size);
355350

356-
PHARE_LOG_LINE_SS(new_size);
357351
if constexpr (N == 1)
358352
point_data_ds.resize({new_size});
359353
else
360354
point_data_ds.resize({new_size, N});
361355
for (int i = 0; i < core::mpi::rank(); ++i)
362356
data_offset += rank_data_size[i];
363-
PHARE_LOG_LINE_SS("done");
364357
}
365358

366359

367360
void resize_boxes(auto const ilvl, auto const& boxes)
368361
{
369-
PHARE_LOG_LINE_SS("resize_boxes");
370-
371362
auto const lvl = std::to_string(ilvl);
372363
auto const rank_box_size = core::mpi::collect(boxes.size());
373364
auto const total_boxes = core::sum(rank_box_size);
@@ -389,17 +380,12 @@ class H5TypeWriter : public PHARE::diagnostic::TypeWriter
389380
ds.select({old_size}, {1}).write(box_offset);
390381
}
391382

392-
393383
amrbox_ds.resize({box_offset + total_boxes, 6});
394-
PHARE_LOG_LINE_SS("resize");
395384
for (int i = 0; i < core::mpi::rank(); ++i)
396385
box_offset += rank_box_size[i];
397386

398-
PHARE_LOG_LINE_SS("writeBoxesForLevel");
399387
auto const vtk_boxes = VTKBoxes{boxes};
400388
amrbox_ds.select({box_offset, 0}, {boxes.size(), dimension * 2}).write(vtk_boxes.data);
401-
402-
PHARE_LOG_LINE_SS("done");
403389
}
404390

405391
template<std::size_t N = 1>

0 commit comments

Comments
 (0)