From 97398d2538bf99ba58d22c2c5e05d3ed35525d04 Mon Sep 17 00:00:00 2001 From: albertNos Date: Tue, 3 May 2022 16:45:53 +0200 Subject: [PATCH 1/4] checkpoint constellations, but inefficient constantProperty Output --- src/ladds/Simulation.cpp | 22 +++++++++++++++------- src/ladds/Simulation.h | 7 +++++-- src/ladds/io/hdf5/HDF5Writer.cpp | 23 +++++++++++++++-------- src/ladds/io/hdf5/HDF5Writer.h | 11 +++++++---- src/ladds/particle/Constellation.cpp | 8 ++++++-- src/ladds/particle/Constellation.h | 2 +- 6 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/ladds/Simulation.cpp b/src/ladds/Simulation.cpp index 3c43d3b3..e8800e24 100644 --- a/src/ladds/Simulation.cpp +++ b/src/ladds/Simulation.cpp @@ -134,7 +134,7 @@ Simulation::initIntegrator(AutoPas_t &autopas, ConfigReader &config) { } std::tuple, std::shared_ptr> Simulation::initWriter( - ConfigReader &config) { + ConfigReader &config, std::set &alreadyExistingIds) { if (config.defines("io/hdf5")) { std::shared_ptr hdf5Writer; const auto hdf5WriteFrequency = config.get("io/hdf5/writeFrequency", 0u); @@ -145,7 +145,7 @@ std::tuple, std::shared_ptr(checkpointPath, false, 0); + hdf5Writer = std::make_shared(checkpointPath, false, 0, alreadyExistingIds); } // ... or write to new HDF5 file .... if (const auto hdf5FileName = config.get("io/hdf5/fileName", ""); not hdf5FileName.empty()) { @@ -155,7 +155,7 @@ std::tuple, std::shared_ptr(hdf5FileName, true, hdf5CompressionLvl); + hdf5Writer = std::make_shared(hdf5FileName, true, hdf5CompressionLvl, alreadyExistingIds); } // check that at least something set a filename if (not hdf5Writer) { @@ -172,7 +172,8 @@ std::tuple, std::shared_ptr &constellations, std::vector &delayedInsertionTotal, - double constellationCutoff) { + double constellationCutoff, + size_t simulationTime) { // first insert delayed particles from previous insertion and collect the repeatedly delayed delayedInsertionTotal = checkedInsert(autopas, delayedInsertionTotal, constellationCutoff); // container collecting delayed particles from one constellation at a time in order to append them to @@ -180,7 +181,7 @@ void Simulation::updateConstellation(AutoPas_t &autopas, std::vector delayedInsertion; for (auto &constellation : constellations) { // new satellites are gradually added to the simulation according to their starting time and operation duration - auto newSatellites = constellation.tick(); + auto newSatellites = constellation.tick(simulationTime); delayedInsertion = checkedInsert(autopas, newSatellites, constellationCutoff); delayedInsertionTotal.insert(delayedInsertionTotal.end(), delayedInsertion.begin(), delayedInsertion.end()); } @@ -222,7 +223,14 @@ size_t Simulation::simulationLoop(AutoPas_t &autopas, const auto vtkWriteFrequency = config.get("io/vtk/writeFrequency", 0ul); - const auto [hdf5WriteFrequency, hdf5Writer, conjuctionWriter] = initWriter(config); + // collects particle ids of potential checkpoint. They need to be passed to the HDF5Writer to avoid redundant output. + std::set alreadyExistingIds{}; + if (config.defines("io/hdf5/checkpoint/file")) { + for (auto &particle : autopas) { + alreadyExistingIds.insert(particle.getID()); + } + } + const auto [hdf5WriteFrequency, hdf5Writer, conjuctionWriter] = initWriter(config, alreadyExistingIds); // set constellation particle IDs and fetch maxExistingParticleId const size_t maxExistingParticleId = setConstellationIDs(autopas, constellations); @@ -252,7 +260,7 @@ size_t Simulation::simulationLoop(AutoPas_t &autopas, timers.constellationInsertion.start(); // new satellites from constellations inserted over time if (iteration % constellationInsertionFrequency == 0) { - updateConstellation(autopas, constellations, delayedInsertion, constellationCutoff); + updateConstellation(autopas, constellations, delayedInsertion, constellationCutoff, iteration); } timers.constellationInsertion.stop(); diff --git a/src/ladds/Simulation.h b/src/ladds/Simulation.h index 7ec078c7..a99ca1b1 100644 --- a/src/ladds/Simulation.h +++ b/src/ladds/Simulation.h @@ -63,10 +63,11 @@ class Simulation { /** * Depending on config initialize readers. * @param config + * @param alreadyExistingIds a list of IDs of already existing particles. Empty if no checkpoint is loaded. * @return tuple */ [[nodiscard]] std::tuple, std::shared_ptr> initWriter( - ConfigReader &config); + ConfigReader &config, std::set &alreadyExistingIds); /** * Tick constellation state machines and if applicable insert new satellites as well as delayed ones from previous @@ -77,11 +78,13 @@ class Simulation { * parameter! * @param constellationCutoff range parameter for checked insertion: if the insertion would be within a distance * of constellationCutoff to any other object the insertion is delayed instead + * @param simulationTime the current iteration */ void updateConstellation(AutoPas_t &autopas, std::vector &constellations, std::vector &delayedInsertion, - double constellationCutoff); + double constellationCutoff, + size_t simulationTime); /** * Check for collisions / conjunctions and write statistics about them. diff --git a/src/ladds/io/hdf5/HDF5Writer.cpp b/src/ladds/io/hdf5/HDF5Writer.cpp index 70eed9a8..b639d79f 100644 --- a/src/ladds/io/hdf5/HDF5Writer.cpp +++ b/src/ladds/io/hdf5/HDF5Writer.cpp @@ -8,7 +8,10 @@ #include "HDF5Definitions.h" -HDF5Writer::HDF5Writer(const std::string &filename, bool replace, unsigned int compressionLevel) +HDF5Writer::HDF5Writer(const std::string &filename, + bool replace, + unsigned int compressionLevel, + const std::set &alreadyExistingIds) #ifdef LADDS_HDF5 : _file(filename, replace ? h5pp::FilePermission::REPLACE : h5pp::FilePermission::READWRITE), collisionInfoH5Type(H5Tcreate(H5T_COMPOUND, sizeof(HDF5Definitions::CollisionInfo))), @@ -20,6 +23,13 @@ HDF5Writer::HDF5Writer(const std::string &filename, bool replace, unsigned int c if (replace) { _file.setCompressionLevel(compressionLevel); } + + // add ids of previous checkpoint to list of existing particle ids in order to avoid adding duplicate + // constantProperties + for (auto &id : alreadyExistingIds) { + addedConstantPropertiesIds.insert(static_cast(id)); + } + // CollisionInfo H5Tinsert(collisionInfoH5Type, "idA", @@ -75,7 +85,6 @@ void HDF5Writer::writeParticles(size_t iteration, const AutoPas_t &autopas) { std::vector> vecPos; std::vector> vecVel; std::vector vecId; - HDF5Definitions::IntType maxParticleId{0}; std::vector newConstantProperties; vecPos.reserve(autopas.getNumberOfParticles()); @@ -96,11 +105,10 @@ void HDF5Writer::writeParticles(size_t iteration, const AutoPas_t &autopas) { static_cast(vel[1]), static_cast(vel[2])}); vecId.emplace_back(id); - // track the highest particle id that was written to the file - // All particles that have a higher id than the highest id from the last time something was written are new - // and their static properties need to be recorded. - maxParticleId = std::max(maxParticleId, id); - if (maxWrittenParticleID == 0 or id > maxWrittenParticleID) { + + // only add properties not yet added + if (addedConstantPropertiesIds.find(id) == addedConstantPropertiesIds.end()) { + addedConstantPropertiesIds.insert(id); newConstantProperties.emplace_back( HDF5Definitions::ParticleConstantProperties{id, particle.getIdentifier().c_str(), @@ -127,7 +135,6 @@ void HDF5Writer::writeParticles(size_t iteration, const AutoPas_t &autopas) { _file.appendTableRecords(newConstantProperties, particleConstantPropertiesFullPath); } - maxWrittenParticleID = maxParticleId; #endif } diff --git a/src/ladds/io/hdf5/HDF5Writer.h b/src/ladds/io/hdf5/HDF5Writer.h index 7120fa75..a9b0661a 100644 --- a/src/ladds/io/hdf5/HDF5Writer.h +++ b/src/ladds/io/hdf5/HDF5Writer.h @@ -29,7 +29,10 @@ class HDF5Writer final : public ConjuctionWriterInterface { * @param replace if true replace an existing file, else append. * @param compressionLevel */ - HDF5Writer(const std::string &filename, bool replace, unsigned int compressionLevel); + HDF5Writer(const std::string &filename, + bool replace, + unsigned int compressionLevel, + const std::set &alreadyExistingIds); ~HDF5Writer() override = default; @@ -49,10 +52,10 @@ class HDF5Writer final : public ConjuctionWriterInterface { private: /** - * Highest partilce ID that was written in any previous iteration. - * For anything below this ID constant particle properties are already written. + * Contains particle IDs in order to add a particles constantProperties only once. */ - HDF5Definitions::IntType maxWrittenParticleID{0}; + std::set addedConstantPropertiesIds{}; + #ifdef LADDS_HDF5 /** * Actual file that will be created. All of the data this writer gets ends up in this one file. diff --git a/src/ladds/particle/Constellation.cpp b/src/ladds/particle/Constellation.cpp index 8c0e23f2..c86b83ed 100644 --- a/src/ladds/particle/Constellation.cpp +++ b/src/ladds/particle/Constellation.cpp @@ -69,6 +69,11 @@ Constellation::Constellation(ConfigReader &constellationConfig, ConfigReader &co schedule[i].push_back(timestamps[i] + j * timeStepSize); } } + + // if checkpoint is loaded, throw away already inserted satellites + if (config.defines("io/hdf5/checkpoint/file")) { + tick(config.get("io/hdf5/checkpoint/iteration", -1, true)); + } } void Constellation::setStartTime(const std::string &startTimeStr, const std::string &refTimeStr) { @@ -95,7 +100,7 @@ void Constellation::setDuration(const std::string &durationStr) { } } -std::vector Constellation::tick() { +std::vector Constellation::tick(size_t simulationTime) { std::vector particles{}; switch (status) { case Status::deployed: @@ -135,7 +140,6 @@ std::vector Constellation::tick() { timeActive += interval; break; } - simulationTime += interval; return particles; } diff --git a/src/ladds/particle/Constellation.h b/src/ladds/particle/Constellation.h index 775ac2cf..e8dff49f 100644 --- a/src/ladds/particle/Constellation.h +++ b/src/ladds/particle/Constellation.h @@ -38,7 +38,7 @@ class Constellation { * and linearly over time. * @return std::vector : satellites to be added to the simulation. */ - std::vector tick(); + std::vector tick(size_t simulationTime); /** * Offsets all local constellation IDs by the parameter baseId to create global IDs. From 0de7a3be92b7549ebd33a828aa787a4e6fcc829d Mon Sep 17 00:00:00 2001 From: albertNos Date: Tue, 3 May 2022 16:47:08 +0200 Subject: [PATCH 2/4] dateformat correction --- cfg/default_cfg.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cfg/default_cfg.yaml b/cfg/default_cfg.yaml index f94d36ac..4845676f 100644 --- a/cfg/default_cfg.yaml +++ b/cfg/default_cfg.yaml @@ -6,7 +6,7 @@ sim: minutes: 0. # as floating points hours: 0. # as floating points days: 0. # as floating points - referenceTime: 2022-01-01 # calendar day associated with simulation start at iteration 0 (yyyy/mm/dd) + referenceTime: 2022-01-01 # calendar day associated with simulation start at iteration 0 (yyyy-mm-dd) maxAltitude: 10000 # Maximum satellite altitude above earth core. This number times two is the simulation box length. [km] minAltitude: 150 # Everything below this altitude above ground will be considered burning up [km] deltaT: 10.0 # [s] From 40308b0b15ea1d4b486c89fccbbfce4d9914e825 Mon Sep 17 00:00:00 2001 From: albertNos Date: Tue, 3 May 2022 17:49:06 +0200 Subject: [PATCH 3/4] updated HDF5WriterReaderTest --- tests/testladds/io/hdf5/HDF5WriterReaderTest.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testladds/io/hdf5/HDF5WriterReaderTest.cpp b/tests/testladds/io/hdf5/HDF5WriterReaderTest.cpp index db20df12..0cf8d7fd 100644 --- a/tests/testladds/io/hdf5/HDF5WriterReaderTest.cpp +++ b/tests/testladds/io/hdf5/HDF5WriterReaderTest.cpp @@ -43,7 +43,7 @@ TEST_F(HDF5WriterReaderTest, WriteReadTestParticleData) { // 2. write data constexpr auto filename = "WriteReadTestParticleData.h5"; constexpr size_t iterationNr = 42; - HDF5Writer hdf5Writer(filename, true, 4); + HDF5Writer hdf5Writer(filename, true, 4,{}); hdf5Writer.writeParticles(iterationNr, autopas); // 3. read data and check that read data is equal to generated data @@ -103,7 +103,7 @@ TEST_F(HDF5WriterReaderTest, WriteReadTestCollisionData) { // 2. write data constexpr auto filename = "WriteReadTestCollisionData.h5"; constexpr size_t iterationNr = 42; - HDF5Writer hdf5Writer(filename, true, 4); + HDF5Writer hdf5Writer(filename, true, 4,{}); hdf5Writer.writeConjunctions(iterationNr, conjunctions); // 3. read data @@ -162,7 +162,7 @@ TEST_F(HDF5WriterReaderTest, AppendCheckpointTest) { constexpr auto filename = "AppendCheckpointTest.h5"; constexpr size_t iterationStepA{42}; { - HDF5Writer hdf5WriterReplace(filename, true, 4); + HDF5Writer hdf5WriterReplace(filename, true, 4,{}); hdf5WriterReplace.writeParticles(iterationStepA, autopas); hdf5WriterReplace.writeConjunctions(iterationStepA, conjunctionsStepA); } @@ -181,7 +181,7 @@ TEST_F(HDF5WriterReaderTest, AppendCheckpointTest) { Particle::calculateBcInv(0., 1., 1., 2.2)}); autopas.addParticle(particles.back()); { - HDF5Writer hdf5WriterAppend(filename, false, 4); + HDF5Writer hdf5WriterAppend(filename, false, 4,{}); hdf5WriterAppend.writeParticles(iterationStepB, autopas); hdf5WriterAppend.writeConjunctions(iterationStepB, conjunctionsStepB); } From 17d49abcfa447b9877f78245d98e48193e167f48 Mon Sep 17 00:00:00 2001 From: albertNos Date: Tue, 3 May 2022 17:50:31 +0200 Subject: [PATCH 4/4] cf --- tests/testladds/io/hdf5/HDF5WriterReaderTest.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testladds/io/hdf5/HDF5WriterReaderTest.cpp b/tests/testladds/io/hdf5/HDF5WriterReaderTest.cpp index 0cf8d7fd..f9494734 100644 --- a/tests/testladds/io/hdf5/HDF5WriterReaderTest.cpp +++ b/tests/testladds/io/hdf5/HDF5WriterReaderTest.cpp @@ -43,7 +43,7 @@ TEST_F(HDF5WriterReaderTest, WriteReadTestParticleData) { // 2. write data constexpr auto filename = "WriteReadTestParticleData.h5"; constexpr size_t iterationNr = 42; - HDF5Writer hdf5Writer(filename, true, 4,{}); + HDF5Writer hdf5Writer(filename, true, 4, {}); hdf5Writer.writeParticles(iterationNr, autopas); // 3. read data and check that read data is equal to generated data @@ -103,7 +103,7 @@ TEST_F(HDF5WriterReaderTest, WriteReadTestCollisionData) { // 2. write data constexpr auto filename = "WriteReadTestCollisionData.h5"; constexpr size_t iterationNr = 42; - HDF5Writer hdf5Writer(filename, true, 4,{}); + HDF5Writer hdf5Writer(filename, true, 4, {}); hdf5Writer.writeConjunctions(iterationNr, conjunctions); // 3. read data @@ -162,7 +162,7 @@ TEST_F(HDF5WriterReaderTest, AppendCheckpointTest) { constexpr auto filename = "AppendCheckpointTest.h5"; constexpr size_t iterationStepA{42}; { - HDF5Writer hdf5WriterReplace(filename, true, 4,{}); + HDF5Writer hdf5WriterReplace(filename, true, 4, {}); hdf5WriterReplace.writeParticles(iterationStepA, autopas); hdf5WriterReplace.writeConjunctions(iterationStepA, conjunctionsStepA); } @@ -181,7 +181,7 @@ TEST_F(HDF5WriterReaderTest, AppendCheckpointTest) { Particle::calculateBcInv(0., 1., 1., 2.2)}); autopas.addParticle(particles.back()); { - HDF5Writer hdf5WriterAppend(filename, false, 4,{}); + HDF5Writer hdf5WriterAppend(filename, false, 4, {}); hdf5WriterAppend.writeParticles(iterationStepB, autopas); hdf5WriterAppend.writeConjunctions(iterationStepB, conjunctionsStepB); }