Skip to content

Commit 927ba3d

Browse files
committed
Examples: Fix Types of Constants & Attributes
Backports from openPMD#1316 and openPMD#1510
1 parent 5de95b3 commit 927ba3d

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Bug Fixes
1919
"""""""""
2020

2121
- Don't require unitSI when reading a patch record component #1470
22+
- Examples: Fix types of particle constant records #1316 #1510
2223
- Python:
2324

2425
- DataFrame to ASCII: Header of First Column in CSV bug documentation third party

examples/7_extended_write_serial.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ int main()
8383
{{io::UnitDimension::M, 1}});
8484
electrons["displacement"]["x"].setUnitSI(1e-6);
8585
electrons.erase("displacement");
86-
electrons["weighting"][io::RecordComponent::SCALAR].makeConstant(
87-
1.e-5);
86+
electrons["weighting"][io::RecordComponent::SCALAR]
87+
.resetDataset({io::Datatype::FLOAT, {1}})
88+
.makeConstant(1.e-5);
8889
}
8990

9091
io::Mesh mesh = cur_it.meshes["lowRez_2D_field"];

examples/7_extended_write_serial.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@
9090
electrons["displacement"].unit_dimension = {Unit_Dimension.M: 1}
9191
electrons["displacement"]["x"].unit_SI = 1.e-6
9292
del electrons["displacement"]
93-
electrons["weighting"][SCALAR].make_constant(1.e-5)
93+
electrons["weighting"][SCALAR] \
94+
.reset_dataset(Dataset(np.dtype("float32"), extent=[1])) \
95+
.make_constant(1.e-5)
9496

9597
mesh = cur_it.meshes["lowRez_2D_field"]
9698
mesh.axis_labels = ["x", "y"]

test/CoreTest.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
using namespace openPMD;
2727

28+
Dataset globalDataset(Datatype::CHAR, {1});
29+
2830
TEST_CASE("versions_test", "[core]")
2931
{
3032
auto const apiVersion = getVersion();
@@ -439,11 +441,11 @@ TEST_CASE("record_constructor_test", "[core]")
439441
ps["position"][RecordComponent::SCALAR].resetDataset(dset);
440442
ps["positionOffset"][RecordComponent::SCALAR].resetDataset(dset);
441443

442-
REQUIRE(r["x"].unitSI() == 1);
444+
REQUIRE(r["x"].resetDataset(dset).unitSI() == 1);
443445
REQUIRE(r["x"].numAttributes() == 1); /* unitSI */
444-
REQUIRE(r["y"].unitSI() == 1);
446+
REQUIRE(r["y"].resetDataset(dset).unitSI() == 1);
445447
REQUIRE(r["y"].numAttributes() == 1); /* unitSI */
446-
REQUIRE(r["z"].unitSI() == 1);
448+
REQUIRE(r["z"].resetDataset(dset).unitSI() == 1);
447449
REQUIRE(r["z"].numAttributes() == 1); /* unitSI */
448450
std::array<double, 7> zeros{{0., 0., 0., 0., 0., 0., 0.}};
449451
REQUIRE(r.unitDimension() == zeros);
@@ -488,13 +490,15 @@ TEST_CASE("recordComponent_modification_test", "[core]")
488490

489491
r["x"].setUnitSI(2.55999e-7);
490492
r["y"].setUnitSI(4.42999e-8);
491-
REQUIRE(r["x"].unitSI() == static_cast<double>(2.55999e-7));
493+
REQUIRE(
494+
r["x"].resetDataset(dset).unitSI() == static_cast<double>(2.55999e-7));
492495
REQUIRE(r["x"].numAttributes() == 1); /* unitSI */
493-
REQUIRE(r["y"].unitSI() == static_cast<double>(4.42999e-8));
496+
REQUIRE(
497+
r["y"].resetDataset(dset).unitSI() == static_cast<double>(4.42999e-8));
494498
REQUIRE(r["y"].numAttributes() == 1); /* unitSI */
495499

496500
r["z"].setUnitSI(1);
497-
REQUIRE(r["z"].unitSI() == static_cast<double>(1));
501+
REQUIRE(r["z"].resetDataset(dset).unitSI() == static_cast<double>(1));
498502
REQUIRE(r["z"].numAttributes() == 1); /* unitSI */
499503
}
500504

@@ -505,13 +509,13 @@ TEST_CASE("mesh_constructor_test", "[core]")
505509
Mesh &m = o.iterations[42].meshes["E"];
506510

507511
std::vector<double> pos{0};
508-
REQUIRE(m["x"].unitSI() == 1);
512+
REQUIRE(m["x"].resetDataset(globalDataset).unitSI() == 1);
509513
REQUIRE(m["x"].numAttributes() == 2); /* unitSI, position */
510514
REQUIRE(m["x"].position<double>() == pos);
511-
REQUIRE(m["y"].unitSI() == 1);
515+
REQUIRE(m["y"].resetDataset(globalDataset).unitSI() == 1);
512516
REQUIRE(m["y"].numAttributes() == 2); /* unitSI, position */
513517
REQUIRE(m["y"].position<double>() == pos);
514-
REQUIRE(m["z"].unitSI() == 1);
518+
REQUIRE(m["z"].resetDataset(globalDataset).unitSI() == 1);
515519
REQUIRE(m["z"].numAttributes() == 2); /* unitSI, position */
516520
REQUIRE(m["z"].position<double>() == pos);
517521
REQUIRE(m.geometry() == Mesh::Geometry::cartesian);
@@ -534,9 +538,9 @@ TEST_CASE("mesh_modification_test", "[core]")
534538
Series o = Series("./MyOutput_%T.json", Access::CREATE);
535539

536540
Mesh &m = o.iterations[42].meshes["E"];
537-
m["x"];
538-
m["y"];
539-
m["z"];
541+
m["x"].resetDataset(globalDataset);
542+
m["y"].resetDataset(globalDataset);
543+
m["z"].resetDataset(globalDataset);
540544

541545
m.setGeometry(Mesh::Geometry::spherical);
542546
REQUIRE(m.geometry() == Mesh::Geometry::spherical);

test/SerialIOTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6647,11 +6647,19 @@ void unfinished_iteration_test(
66476647
*/
66486648
it5.setAttribute("__openPMD_internal_fail", "asking for trouble");
66496649
auto it10 = write.writeIterations()[10];
6650+
Dataset ds(Datatype::INT, {10});
66506651
auto E_x = it10.meshes["E"]["x"];
66516652
auto e_density = it10.meshes["e_density"][RecordComponent::SCALAR];
66526653
auto electron_x = it10.particles["e"]["position"]["x"];
66536654
auto electron_mass =
66546655
it10.particles["e"]["mass"][RecordComponent::SCALAR];
6656+
6657+
RecordComponent *resetThese[] = {
6658+
&E_x, &e_density, &electron_x, &electron_mass};
6659+
for (RecordComponent *rc : resetThese)
6660+
{
6661+
rc->resetDataset(ds);
6662+
}
66556663
}
66566664
auto tryReading = [&config, file, encoding](
66576665
Access access,

0 commit comments

Comments
 (0)