From 75bb2e4d857364ec23d00127c6e579293140f5e8 Mon Sep 17 00:00:00 2001 From: csendlinger Date: Thu, 5 Sep 2024 09:33:12 +0200 Subject: [PATCH] small fixes and cosmetic changes --- include/IO/hdf5_reader.h | 1 + include/IO/hdf5_writer.h | 1 - include/IO/restart.h | 2 +- include/simulation/density_field.h | 3 +- src/IO/hdf5_reader.cpp | 30 ++++++++++ src/IO/hdf5_writer.cpp | 90 +---------------------------- src/IO/restart.cpp | 36 +++++------- src/simulation/cpu_synchronizer.cpp | 3 + src/simulation/density_field.cpp | 17 ++---- src/simulation/injector.cpp | 11 ++-- src/simulation/simulation.cpp | 6 +- 11 files changed, 69 insertions(+), 131 deletions(-) diff --git a/include/IO/hdf5_reader.h b/include/IO/hdf5_reader.h index 6f1718f..e341175 100755 --- a/include/IO/hdf5_reader.h +++ b/include/IO/hdf5_reader.h @@ -13,6 +13,7 @@ class Hdf5Reader { blitz::Array readD3Double(std::string path); blitz::Array readD2Double(std::string path); blitz::Array readD1Double(std::string path); + double readDouble(std::string path); blitz::Array readD4LongInt(std::string path); diff --git a/include/IO/hdf5_writer.h b/include/IO/hdf5_writer.h index bd823c3..f69e19b 100755 --- a/include/IO/hdf5_writer.h +++ b/include/IO/hdf5_writer.h @@ -23,7 +23,6 @@ class Hdf5Writer { void write(double data, std::string path); void write(const std::string& data, std::string path); void writeRNG(const std::string& data, std::string path_base); - // void writeRNG(const std::string path_base); std::string AbsoluteFileName() const; private: diff --git a/include/IO/restart.h b/include/IO/restart.h index 9668821..2257fa8 100644 --- a/include/IO/restart.h +++ b/include/IO/restart.h @@ -16,7 +16,7 @@ class DynamicDensityField; class PoissonSolver; class RandomNumbers; -// Note: The restarts for the DensirtField and the PoissonSolver require that the +// Note: The restarts for the DensityField and the PoissonSolver require that the // partitioning of the simulation space does not change between the original and // the restarted simulation // Note: think if it's better (or more consistent) to set the dynamic electric field diff --git a/include/simulation/density_field.h b/include/simulation/density_field.h index 785b29e..5c4d322 100755 --- a/include/simulation/density_field.h +++ b/include/simulation/density_field.h @@ -63,6 +63,8 @@ class DynamicDensityField : public DensityField { friend class CpuSynchronizer; + void setDensityDistribution(); + private: // coordinate ordering: r, phi, z, E // bin numbering @@ -80,7 +82,6 @@ class DynamicDensityField : public DensityField { BinCoordinate densityBin(const CylinderCoordinate& position) const; void recalculateDensityArray(); void initPositionDistributions(); - void setDensityDistribution(); blitz::Array time_in_bin_s_; blitz::Array deleted_particles_; diff --git a/src/IO/hdf5_reader.cpp b/src/IO/hdf5_reader.cpp index 0b42d60..c404c33 100755 --- a/src/IO/hdf5_reader.cpp +++ b/src/IO/hdf5_reader.cpp @@ -102,6 +102,36 @@ blitz::Array Hdf5Reader::readD1Double(std::string path) { return data.copy(); } +double Hdf5Reader::readDouble(std::string path) { + hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); + + // H5Eset_auto(H5E_DEFAULT, NULL, NULL); + hid_t dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT); + hid_t dataspace = H5Dget_space(dataset); + if (dataset < 0) { + ERROR("The given dataset could not be opened"); + } + // turn error report back onby hdf5 because of check H5Dopen2 + // H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT); + + const int ndims = H5Sget_simple_extent_ndims(dataspace); + hsize_t dims[ndims]; + if (ndims != 1) { + ERROR("The given dataset is not one dimensional. It has dims: " + ndims); + } + H5Sget_simple_extent_dims(dataspace, dims, NULL); + + double data; + + H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, + &data); + + H5Sclose(dataspace); + H5Dclose(dataset); + H5Fclose(file); + return data; +} + blitz::Array Hdf5Reader::readD4Double(std::string path) { hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); diff --git a/src/IO/hdf5_writer.cpp b/src/IO/hdf5_writer.cpp index 2bfd9a6..dee8509 100755 --- a/src/IO/hdf5_writer.cpp +++ b/src/IO/hdf5_writer.cpp @@ -463,7 +463,7 @@ void Hdf5Writer::writeRNG(const std::string& data, std::string path_base) { out.insert(out.end(), (SimConstants::engine_string_length_max - stateStringSize), '0'); - // apperently it's not possible to use 'H5T_VARIABLE' with the MPI-I/O file driver + // apparently it's not possible to use 'H5T_VARIABLE' with the MPI-I/O file driver // (since it doesn't allow for variable size objects to be created, see: https://davis.lbl.gov/Manuals/HDF5-1.8.7/TechNotes/VLTypes.html) hid_t dtype = H5Tcreate(H5T_STRING, SimConstants::engine_string_length_max); @@ -518,90 +518,4 @@ void Hdf5Writer::writeRNG(const std::string& data, std::string path_base) { // Debug utility std::string Hdf5Writer::AbsoluteFileName() const { return absolute_filename_; -} - -// testing function for the parallel output - -// void Hdf5Writer::writeRNG(std::string path_base) { -// MPI_Info info; -// MPI_Info_create(&info); -// hid_t fapl = H5Pcreate(H5P_FILE_ACCESS); -// H5Pset_fapl_mpio(fapl, MPI_COMM_WORLD, info); -// // MPI_Info_free(&info); - -// hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDWR, fapl); - -// if(file < 0) { -// SIM_INFO("file could not be opened"); -// } -// hsize_t scalar = 1; -// hid_t filespace = H5Screate_simple(1, &scalar, NULL); - -// hid_t lcpl = H5Pcreate(H5P_LINK_CREATE); -// H5Pset_create_intermediate_group(lcpl, 1); - -// int mpi_rank = MpiCommunication::mpiRank(); -// std::string out = "Hello from proc " + std::to_string(mpi_rank); -// out.insert(out.end(), mpi_rank, '0' + mpi_rank); - -// const size_t maxSize = 25; -// size_t stringSize = out.size(); - -// if(stringSize > maxSize) { -// ERROR("Max Size (" + std::to_string(maxSize) + ") too small. String size: " + std::to_string(stringSize)); -// } - -// out.insert(out.end(), (maxSize - stringSize), '0'); - -// // apperently it's not possible to use 'H5T_VARIABLE' with the MPI-I/O file driver -// // (since it doesn't allow for variable size objects to be created, see: https://davis.lbl.gov/Manuals/HDF5-1.8.7/TechNotes/VLTypes.html) -// hid_t dtype = H5Tcreate(H5T_STRING, maxSize); - -// // store old error stack info -// H5E_auto2_t oldfunc; -// void* old_client_data; -// H5Eget_auto2(H5E_DEFAULT, &oldfunc, &old_client_data); - -// // turn of error report by hdf5 because of check H5Dopen2 -// H5Eset_auto(H5E_DEFAULT, NULL, NULL); - -// for(int i = 0; i < MpiCommunication::mpiSize(); ++i) { -// std::string path = path_base + std::to_string(i); -// DEBUG_LEVEL_3(path); -// hid_t dataset_string = H5Dcreate2(file, (path + "/string").c_str(), dtype, filespace, lcpl, H5P_DEFAULT, H5P_DEFAULT); -// // this sadly seems to be the option that takes the least amount of effort. -// // H5Lexits() would be an option but (!) each group would need to be checked for existance first, which sucks -// if(dataset_string < 0) { -// DEBUG_LEVEL_3("Dataset could not be created"); -// dataset_string = H5Dopen2(file, (path + "/string").c_str(), H5P_DEFAULT); -// if(dataset_string < 0) { -// ERROR("Dataset for string can neither be created nor opened"); -// } -// } - -// hid_t dataset_length = H5Dcreate2(file, (path + "/length").c_str(), H5T_NATIVE_HSIZE, filespace, lcpl, H5P_DEFAULT, H5P_DEFAULT); -// if(dataset_length < 0) { -// DEBUG_LEVEL_3("Dataset could not be created"); -// dataset_length = H5Dopen2(file, (path + "/length").c_str(), H5P_DEFAULT); -// if(dataset_length < 0) { -// ERROR("Dataset for length can neither be created nor opened"); -// } -// } - -// if(i == mpi_rank) { -// const char* out_ptr = out.c_str(); -// H5Dwrite(dataset_string, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, out_ptr); -// H5Dwrite(dataset_length, H5T_NATIVE_HSIZE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &stringSize); -// } -// H5Dclose(dataset_string); -// H5Dclose(dataset_length); -// } -// // turn error report back onby hdf5 because of check H5Dopen2 -// H5Eset_auto(H5E_DEFAULT, oldfunc, old_client_data); - -// H5Tclose(dtype); -// H5Pclose(lcpl); -// H5Sclose(filespace); -// H5Fclose(file); -// H5Pclose(fapl); -// } \ No newline at end of file +} \ No newline at end of file diff --git a/src/IO/restart.cpp b/src/IO/restart.cpp index c5a3978..2fa48d7 100644 --- a/src/IO/restart.cpp +++ b/src/IO/restart.cpp @@ -39,40 +39,36 @@ void Restart::restart(std::mapdensityForDrawing(), path); } - } void Restart::restartRandomNumbers(RandomNumbers* random) { - std::string path = "/RestartInfo/" + restart_identifier_ - + "/CPU_" + std::to_string(MpiCommunication::mpiRank()); - std::stringstream engine_state = reader_.readEngineString(path); - std::mt19937* engine = random->engine(); - engine_state >> *engine; + std::string path = "/RestartInfo/" + restart_identifier_ + + "/CPU_" + std::to_string(MpiCommunication::mpiRank()); + std::stringstream engine_state = reader_.readEngineString(path); + std::mt19937* engine = random->engine(); + engine_state >> *engine; - std::stringstream check; - check << *engine; + std::stringstream check; + check << *engine; - if(check.str() != engine_state.str()) { - ERROR("Something went wrong in the initialization of the " - "random engine."); - } - // DEBUG_LEVEL_3("Restart of random engine worked"); - // std::uniform_real_distribution dist(0,1); - // DEBUG_LEVEL_3("first value after loading rng: " + std::to_string(dist(*engine))); + if(check.str() != engine_state.str()) { + ERROR("Something went wrong in the initialization of the " + "random engine."); + } } // only restart the time from which the simulation is restarted. -// The "timestep" is calculated from the json input data (same as no restart). void Restart::restartTimer(species::TrackedSpecie* reference_specie) { double time; double delta_t; + // physical time { std::string path = reference_specie->name() + "/DensityField/SimulationData/" + restart_identifier_ + "/PhysicalTime"; DEBUG_LEVEL_3(path); // probably add a function in the reader to read a single double value instead of - // a 1d array of doubles + // an 1d array of doubles blitz::Array restart_time = reader_.readD1Double(path); if(restart_time.size() != 1) { ERROR("Physical time should be a single double value but " + @@ -81,6 +77,7 @@ void Restart::restartTimer(species::TrackedSpecie* reference_specie) { } time = restart_time(0); } + // delta t { std::string path = reference_specie->name() + "/CurrentCollector/SimulationData/" + @@ -101,7 +98,7 @@ void Restart::restartTimer(species::TrackedSpecie* reference_specie) { } void Restart::restartDrawingDensityField(DynamicDensityField* field, std::string path) { - // restart TimeInBin-field + // restart TimeInBin field { std::string path_to_data = path + "/TimeInBin"; DEBUG_LEVEL_3(path_to_data); @@ -126,8 +123,5 @@ void Restart::restartPoissonSolver(PoissonSolver* solver) { "/Potential/"; DEBUG_LEVEL_3(path); blitz::Array potential = reader_.readD3Double(path); - if(!blitz::all(potential.length() == solver->potential().length())) { - DEBUG_LEVEL_3("Restart Poissonsolver: wrong sizes of the potential"); - } solver->setPotential(potential); } \ No newline at end of file diff --git a/src/simulation/cpu_synchronizer.cpp b/src/simulation/cpu_synchronizer.cpp index f832fc2..2bf51f6 100755 --- a/src/simulation/cpu_synchronizer.cpp +++ b/src/simulation/cpu_synchronizer.cpp @@ -79,6 +79,9 @@ void CpuSynchronizer::synchronize(DynamicDensityField* field, if (reset) { density_for_drawing->time_in_bin_s_ = field->time_in_bin_s_; density_for_drawing->deleted_particles_ = field->deleted_particles_; + // try setting the distribution for drawing the density here, since ideally it + // the density array and the distribution only have to be set once + density_for_drawing->setDensityDistribution(); } } } diff --git a/src/simulation/density_field.cpp b/src/simulation/density_field.cpp index 348bcb8..eb2cbe8 100755 --- a/src/simulation/density_field.cpp +++ b/src/simulation/density_field.cpp @@ -227,19 +227,18 @@ blitz::TinyVector DynamicDensityField::drawPositionAndEnergy( std::mt19937* engine) { // if the density is empty, then return a TinyVector filled with 0s as // an indiction of that (probably not the best solution) - recalculateDensityArray(); + + // stuff below not optimal, since it's done for every particle that is drawn, + // but maybe not certain that otherwise the density is right... + // recalculateDensityArray(); if (isEqual(blitz::max(density_per_m3_), 0.)) { DEBUG_LEVEL_3("Density is empty"); return blitz::TinyVector (0,0,0,0); } - setDensityDistribution(); + // setDensityDistribution(); double random_r, random_z, random_phi, random_E, random_n; double density_in_random_cell; - // random_r = 0.01; - // random_phi = 0.; - // random_z = 1.; - // draw random position (independent of the Energy) do { DEBUG_LEVEL_3("Missed."); random_r = std::sqrt(r_dist_(*engine)); @@ -327,7 +326,7 @@ void DynamicDensityField::resetDynamicDensityField() { void DynamicDensityField::restartTimer(double time, double delta_t) { // make sure that the timer can only be restarted once (per dynamicDensityField object) - // probably not necessary, but whatever + // probably not necessary, but whatever... if(timer_restarted_) { return; } @@ -344,11 +343,7 @@ void DynamicDensityField::restartTimeInBin(blitz::Array time_in_bin) out << " for time_in_bin array"; ERROR(out.str()); } - std::stringstream out; - out << "Before: " << time_in_bin_s_; time_in_bin_s_ = time_in_bin.copy(); - out << "After: " << time_in_bin_s_; - // DEBUG_LEVEL_0(out.str()); } void DynamicDensityField::restartDeletedParticlesArray( diff --git a/src/simulation/injector.cpp b/src/simulation/injector.cpp index 4cc1107..63d6b7b 100755 --- a/src/simulation/injector.cpp +++ b/src/simulation/injector.cpp @@ -80,7 +80,6 @@ void Injector::rollNewParticleFromDensityField(std::mt19937* engine, const double injection_time) { blitz::TinyVector particle_specs = specie->densityForDrawing()->drawPositionAndEnergy(engine); - // blitz::TinyVector particle_specs(0.01, 0, 1, 1); // check if density was empty (i.e. Energy component is 0) if (particle_specs[3] == 0) { @@ -97,7 +96,7 @@ void Injector::rollNewParticleFromDensityField(std::mt19937* engine, const double theta = distribution(*engine) * 2 * M_PI; const double phi = std::acos(1. - 2.*distribution(*engine)); - // for now draw the particles with an isotropic velocity distribution, + // for now, draw the particles with an isotropic velocity distribution, // is not realistic but as of now there is no information about the // velocity vector distribution in the respective cell blitz::TinyVector v_normal(std::cos(theta) * std::sin(phi), @@ -105,11 +104,11 @@ void Injector::rollNewParticleFromDensityField(std::mt19937* engine, std::cos(phi)); particle::Particle drawn_particle(specie, CylinderCoordinate( - blitz::TinyVector(particle_specs[0], - particle_specs[1], particle_specs[2])), - CartesianVector(v_normal*v_abs), injection_time, weight); + blitz::TinyVector(particle_specs[0], + particle_specs[1], particle_specs[2])), + CartesianVector(v_normal*v_abs), injection_time, weight); - queue.push_back(drawn_particle); + queue.push_back(drawn_particle); } double Injector::timePassed() const { diff --git a/src/simulation/simulation.cpp b/src/simulation/simulation.cpp index 5503e63..ce99555 100755 --- a/src/simulation/simulation.cpp +++ b/src/simulation/simulation.cpp @@ -128,7 +128,10 @@ void Simulation::start() { const double delta_t = time_resolution_events*injector_->timePassed(); - if(delta_t != timer_->getDeltaT()) { + // if delta_t of the timer after creation and potential restart is 0, then + // it's indicative of no restart having occured (a simulation with a delta_t + // of 0 would not be sensible) + if(timer_->getDeltaT() != 0 && delta_t != timer_->getDeltaT()) { std::stringstream out; out << "Old delta t: " << timer_->getDeltaT(); out << "; new delta t: " << delta_t; @@ -448,7 +451,6 @@ std::vector Simulation::determineInjectedDensityParticles( } if (current == 0) { DEBUG_LEVEL_0("particle number " + pair.second->name() + ": " + std::to_string(particleNumberDensity)); - // DEBUG_LEVEL_0("weight: " + std::to_string(particleNumberDensity/numberParticlesToDraw)); } injector_->rollNewParticleFromDensityField(random->engine(), pair.second,