Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes and update of ResultWriter #255

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7ad4dd2
Fix to make mixing rules work
HomesGH May 5, 2023
5e46bfa
Fixes and logging/warnings regarding shifted parameter; Print boolean…
HomesGH May 5, 2023
a270300
Major update of ResultWriter to improve parsing of output, doc, error…
HomesGH May 5, 2023
1485f7c
Minor fixes
HomesGH May 5, 2023
0d151cb
Fixes according to comments
HomesGH May 19, 2023
be04e8b
Avoid second read of xml config
HomesGH May 24, 2023
a9b4f22
Add missing header files
HomesGH May 24, 2023
9bd8002
Add default values to SpatialProfile plugin as fix
HomesGH Jun 22, 2023
7610d51
Use global calc. values in ResultWriter plugin
HomesGH Jul 24, 2023
93ab85c
Use correct quaternion identity
HomesGH Sep 29, 2023
845b160
AutoPas writes full checkpoint for compatibility
HomesGH Sep 29, 2023
0e11435
Undo AutoPas checkpoint writing
HomesGH Sep 29, 2023
d729d6f
Change default version of MMPLD in MMPLDWriter
HomesGH Oct 20, 2023
d4e5c8f
Tentative fix of MMPLDWriter
HomesGH Oct 20, 2023
5629359
Delete finish() in MMPLDWriter as it does wrong stuff
HomesGH Oct 24, 2023
753d6c1
Fix set of initial simulation time
HomesGH Oct 24, 2023
922b0cb
Minor fixes regarding setSimulationTime()
HomesGH Oct 24, 2023
aa64e64
Merge branch 'master' into variousChanges
HomesGH Oct 24, 2023
f81d919
Add std:: and Log::
HomesGH Oct 24, 2023
5d4f89d
Fix in init of simulation steps
HomesGH Oct 24, 2023
6351286
delete unnecessary header
HomesGH Oct 24, 2023
981cddf
Parse boolean and check input type
HomesGH Oct 25, 2023
78db2ee
Add info to error message
HomesGH Oct 25, 2023
43c1a2f
Realize suggestions
HomesGH Oct 25, 2023
0f8092e
Add some basic checks for xml parsing
HomesGH Oct 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/DropletCoalescence/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<mass>39.948</mass>
<sigma>3.3916</sigma>
<epsilon>137.90</epsilon>
<shifted>1</shifted>
<shifted>true</shifted>
</site>
<momentsofinertia rotaxes="xyz" >
<Ixx>0.0</Ixx>
Expand All @@ -23,7 +23,7 @@
<mass>39.948</mass>
<sigma>3.3916</sigma>
<epsilon>137.90</epsilon>
<shifted>1</shifted>
<shifted>true</shifted>
</site>
<momentsofinertia rotaxes="xyz" >
<Ixx>0.0</Ixx>
Expand Down
2 changes: 1 addition & 1 deletion examples/DropletCoalescence/liq/config_1_generateLiq.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<components>
<include query="/components/moleculetype" >../components.xml</include>
<include query="/mixing/rule">../mixing_2c.xml</include>
<include query="/mixing">../mixing_2c.xml</include>
</components>

<phasespacepoint>
Expand Down
2 changes: 1 addition & 1 deletion examples/DropletCoalescence/liq/config_2_replicateLiq.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<components>
<include query="/components/moleculetype" >../components.xml</include>
<include query="/mixing/rule">../mixing_2c.xml</include>
<include query="/mixing">../mixing_2c.xml</include>
</components>

<phasespacepoint>
Expand Down
2 changes: 1 addition & 1 deletion examples/DropletCoalescence/vap/config_3_generateVap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<components>
<include query="/components/moleculetype" >../components.xml</include>
<include query="/mixing/rule">../mixing_2c.xml</include>
<include query="/mixing">../mixing_2c.xml</include>
</components>

<phasespacepoint>
Expand Down
2 changes: 1 addition & 1 deletion examples/DropletCoalescence/vap/config_4_replicateVap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<components>
<include query="/components/moleculetype" >../components.xml</include>
<include query="/mixing/rule">../mixing_2c.xml</include>
<include query="/mixing">../mixing_2c.xml</include>
</components>

<phasespacepoint>
Expand Down
2 changes: 1 addition & 1 deletion examples/DropletCoalescence/vle/config_5_droplet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<components>
<include query="/components/moleculetype" >../components.xml</include>
<include query="/mixing/rule">../mixing_2c.xml</include>
<include query="/mixing">../mixing_2c.xml</include>
</components>

<phasespacepoint>
Expand Down
2 changes: 1 addition & 1 deletion examples/SpinodalDecomposition/components_1c.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<mass>1.0</mass>
<sigma>1.0</sigma>
<epsilon>1.0</epsilon>
<shifted>1</shifted>
<shifted>true</shifted>
</site>
<momentsofinertia rotaxes="xyz" >
<Ixx>0.0</Ixx>
Expand Down
118 changes: 70 additions & 48 deletions src/io/ResultWriter.cpp
Original file line number Diff line number Diff line change
@@ -1,89 +1,111 @@
#include "io/ResultWriter.h"

#include "Domain.h"
#include "parallel/DomainDecompBase.h"
#include "Simulation.h"
#include "parallel/DomainDecompBase.h"
#include "utils/Logger.h"
#include <chrono>

#include <fstream>

using Log::global_log;

void ResultWriter::readXML(XMLfileUnits& xmlconfig) {
_writeFrequency = 1;
xmlconfig.getNodeValue("writefrequency", _writeFrequency);
global_log->info() << "[ResultWriter] Write frequency: " << _writeFrequency << endl;

_outputPrefix = "mardyn";
xmlconfig.getNodeValue("outputprefix", _outputPrefix);
global_log->info() << "[ResultWriter] Output prefix: " << _outputPrefix << endl;

size_t acc_steps = 1000;
xmlconfig.getNodeValue("accumulation_steps", acc_steps);
_U_pot_acc = new Accumulator<double>(acc_steps);
_p_acc = new Accumulator<double>(acc_steps);
global_log->info() << "[ResultWriter] Accumulation steps: " << acc_steps << endl;

_writePrecision = 5;
xmlconfig.getNodeValue("writeprecision", _writePrecision);
global_log->info() << "[ResultWriter] Write precision: " << _writePrecision << endl;
}

void ResultWriter::init(ParticleContainer * /*particleContainer*/,
DomainDecompBase *domainDecomp, Domain * /*domain*/) {

// Only main rank writes data to file
if(domainDecomp->getRank() == 0) {
const auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
tm unused{};
const auto nowStr = std::put_time(localtime_r(&now, &unused), "%c");
string resultfile(_outputPrefix+".res");
_resultStream.open(resultfile.c_str(), std::ios::out);
_resultStream << "# ls1 MarDyn simulation started at " << nowStr << endl;
_resultStream << "# Averages are the accumulated values over " << _U_pot_acc->getWindowLength() << " time steps."<< endl;
_resultStream << std::setw(10) << "# step" << std::setw(_writePrecision+15) << "time"
<< std::setw(_writePrecision+15) << "U_pot"
const string resultfile(_outputPrefix+".res");
std::ofstream resultStream;
resultStream.open(resultfile.c_str(), std::ios::out);
resultStream << std::setw(10) << "#step"
<< std::setw(_writePrecision+15) << "time"
<< std::setw(_writePrecision+15) << "U_pot_avg"
<< std::setw(_writePrecision+15) << "p"
<< std::setw(_writePrecision+15) << "U_kin_avg"
<< std::setw(_writePrecision+15) << "U_kinTrans_avg"
<< std::setw(_writePrecision+15) << "U_kinRot_avg"
<< std::setw(_writePrecision+15) << "p_avg"
<< std::setw(_writePrecision+15) << "beta_trans"
<< std::setw(_writePrecision+15) << "beta_rot"
<< std::setw(_writePrecision+15) << "c_v"
<< std::setw(_writePrecision+15) << "N"
HomesGH marked this conversation as resolved.
Show resolved Hide resolved
<< endl;
FG-TUM marked this conversation as resolved.
Show resolved Hide resolved
resultStream.close();
}
}

void ResultWriter::endStep(ParticleContainer *particleContainer, DomainDecompBase *domainDecomp, Domain *domain,
unsigned long simstep) {

// Writing of cavities now handled by CavityWriter
const uint64_t globalNumMolecules = domain->getglobalNumMolecules(true, particleContainer, domainDecomp);

unsigned long globalNumMolecules = domain->getglobalNumMolecules(true, particleContainer, domainDecomp);
double cv = domain->cv();
_uPot_acc += domain->getGlobalUpot();
_p_acc += domain->getGlobalPressure();

_U_pot_acc->addEntry(domain->getGlobalUpot());
_p_acc->addEntry(domain->getGlobalPressure());
if((domainDecomp->getRank() == 0) && (simstep % _writeFrequency == 0)){
_resultStream << std::setw(10) << simstep << std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << _simulation.getSimulationTime()
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << domain->getGlobalUpot()
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << _U_pot_acc->getAverage()
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << domain->getGlobalPressure()
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << _p_acc->getAverage()
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << domain->getGlobalBetaTrans()
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << domain->getGlobalBetaRot()
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << cv
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << globalNumMolecules
<< endl;
double uKinLocal = 0.0F;
double uKinGlobal = 0.0F;
double uKinTransLocal = 0.0F;
double uKinTransGlobal = 0.0F;
double uKinRotLocal = 0.0F;
double uKinRotGlobal = 0.0F;
#if defined(_OPENMP)
#pragma omp parallel reduction(+:uKinLocal,uKinTransLocal,uKinRotLocal)
#endif
{
for (auto moleculeIter = particleContainer->iterator(ParticleIterator::ONLY_INNER_AND_BOUNDARY);
moleculeIter.isValid(); ++moleculeIter) {
uKinLocal += moleculeIter->U_kin();
uKinTransLocal += moleculeIter->U_trans();
uKinRotLocal += moleculeIter->U_rot();
}
}
}
#ifdef ENABLE_MPI
MPI_Allreduce(&uKinLocal, &uKinGlobal, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(&uKinTransLocal, &uKinTransGlobal, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(&uKinRotLocal, &uKinRotGlobal, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
amartyads marked this conversation as resolved.
Show resolved Hide resolved
#else
uKinGlobal = uKinLocal;
uKinTransGlobal = uKinTransLocal;
uKinRotGlobal = uKinRotLocal;
#endif
_uKin_acc += uKinGlobal;
_uKinTrans_acc += uKinTransGlobal;
_uKinRot_acc += uKinRotGlobal;

void ResultWriter::finish(ParticleContainer * /*particleContainer*/,
DomainDecompBase *domainDecomp, Domain * /*domain*/){
_numSamples++;

if(domainDecomp->getRank() == 0) {
const auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
tm unused{};
const auto nowStr = std::put_time(localtime_r(&now, &unused), "%c");
_resultStream << "# ls1 mardyn simulation finished at " << nowStr << endl;
_resultStream.close();
if ((simstep % _writeFrequency == 0) and (simstep > 0UL)) {
amartyads marked this conversation as resolved.
Show resolved Hide resolved
// Only main rank writes data to file
if (domainDecomp->getRank() == 0) {
const string resultfile(_outputPrefix+".res");
std::ofstream resultStream;
resultStream.open(resultfile.c_str(), std::ios::app);
resultStream << std::setw(10) << simstep << std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << _simulation.getSimulationTime()
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << _uPot_acc/_numSamples
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << _uKin_acc/_numSamples
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << _uKinTrans_acc/_numSamples
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << _uKinRot_acc/_numSamples
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << _p_acc/_numSamples
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << domain->cv()
<< std::setw(_writePrecision+15) << std::scientific << std::setprecision(_writePrecision) << globalNumMolecules
<< endl;
FG-TUM marked this conversation as resolved.
Show resolved Hide resolved
resultStream.close();
}

// Reset values
_numSamples = 0UL;
_uPot_acc = 0.0F;
_uKin_acc = 0.0F;
_uKinTrans_acc = 0.0F;
_uKinRot_acc = 0.0F;
_p_acc = 0.0F;
}
}
63 changes: 30 additions & 33 deletions src/io/ResultWriter.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#ifndef SRC_IO_RESULTWRITER_H_
#define SRC_IO_RESULTWRITER_H_

#include <fstream>
#include <string>

#include "plugins/PluginBase.h"
#include "utils/Accumulator.h"

#include <string>


/** @brief Writes thermodynamic properties to a file.
*
Expand All @@ -15,57 +14,55 @@
* The following values are written to the file:
* - Simulation time step
* - time since the simulation started (dimensionless)
* - Average potential Energy
* - Pressure
* - BetaTrans
* - BetaRot
* - Average potential energy
* - Average kinetic energy
* - Average kinetic translational energy
* - Average kinetic rotational energy
* - Average Pressure
* - Isochoric heat capacity
* - Global number of particles
*/
class ResultWriter : public PluginBase {
public:
ResultWriter() : _U_pot_acc(nullptr), _p_acc(nullptr) {}
~ResultWriter() {
delete _U_pot_acc;
delete _p_acc;
}
ResultWriter() = default;
~ResultWriter() override = default;


/** @brief Read in XML configuration for ResultWriter and all its included objects.
*
* The following xml object structure is handled by this method:
* \code{.xml}
<outputplugin name="ResultWriter">
<writefrequency>INTEGER</writefrequency> <!-- Frequency in which the output is written; Default: 1 -->
<outputprefix>STRING</outputprefix> <!-- Prefix of the output file; Default: "results" -->
<accumulation_steps>INTEGER</accumulation_steps> <!-- Result is accumulated over the specified steps; Default: 1000 -->
<writefrequency>UINTEGER</writefrequency> <!-- Frequency in which the output is written; Default: 1000 -->
<outputprefix>STRING</outputprefix> <!-- Prefix of the output file; Default: "mardyn" -->
<writeprecision>UINTEGER</writeprecision> <!-- Precision of output can be set here; Default: 5 -->
</outputplugin>
\endcode
*/
virtual void readXML(XMLfileUnits& xmlconfig);
virtual void readXML(XMLfileUnits& xmlconfig) override;

void init(ParticleContainer *particleContainer,
DomainDecompBase *domainDecomp, Domain *domain);
DomainDecompBase *domainDecomp, Domain *domain) override;

void endStep(
ParticleContainer *particleContainer,
DomainDecompBase *domainDecomp, Domain *domain,
unsigned long simstep
);
void endStep(ParticleContainer *particleContainer, DomainDecompBase *domainDecomp,
Domain *domain, unsigned long simstep) override;

void finish(ParticleContainer *particleContainer,
DomainDecompBase *domainDecomp, Domain *domain);
void finish(ParticleContainer* /* particleContainer */,
DomainDecompBase* /* domainDecomp */, Domain* /* domain */) override {}

std::string getPluginName() {
return std::string("ResultWriter");
}
std::string getPluginName() override {return std::string("ResultWriter");}
static PluginBase* createInstance() { return new ResultWriter(); }

private:
std::ofstream _resultStream;
long _writeFrequency;
int _writePrecision;
std::string _outputPrefix;
Accumulator<double> *_U_pot_acc;
Accumulator<double> *_p_acc;
uint64_t _writeFrequency{1000UL};
unsigned int _writePrecision{5};
std::string _outputPrefix{"mardyn"};
uint64_t _numSamples{0UL};
double _uPot_acc{0.0F};
double _uKin_acc{0.0F};
double _uKinTrans_acc{0.0F};
double _uKinRot_acc{0.0F};
double _p_acc{0.0F};
};

#endif // SRC_IO_RESULTWRITER_H_
5 changes: 5 additions & 0 deletions src/molecules/Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ void Component::readXML(XMLfileUnits& xmlconfig) {
LJcenter ljSite;
ljSite.readXML(xmlconfig);
addLJcenter(ljSite);
global_log->info() << "sigma = " << ljSite.sigma() << " ; epsilon = " << ljSite.eps()
<< " ; mass = " << ljSite.m() << " ; shifted = " << ljSite.shiftRequested() << endl;
} else if (siteType == "Charge") {
Charge chargeSite;
chargeSite.readXML(xmlconfig);
addCharge(chargeSite);
global_log->info() << "charge = " << chargeSite.q() << endl;
} else if (siteType == "Dipole") {
Dipole dipoleSite;
dipoleSite.readXML(xmlconfig);
addDipole(dipoleSite);
global_log->info() << "dipoleMoment = " << dipoleSite.absMy() << endl;
} else if (siteType == "Stockmayer") {
_isStockmayer = true;
_rot_dof = 2;
Expand All @@ -77,6 +81,7 @@ void Component::readXML(XMLfileUnits& xmlconfig) {
Quadrupole quadrupoleSite;
quadrupoleSite.readXML(xmlconfig);
addQuadrupole(quadrupoleSite);
global_log->info() << "quadrupoleMoment = " << quadrupoleSite.absQ() << endl;
} else if (siteType == "Tersoff") {
global_log->error() << "Tersoff no longer supported:" << siteType << endl;
Simulation::exit(-1);
Expand Down
11 changes: 10 additions & 1 deletion src/molecules/Site.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <array>
#include <cmath>
#include <cstdint>
#include <string>

using Log::global_log;

Expand Down Expand Up @@ -126,7 +127,15 @@ class LJcenter : public Site {
Site::readXML(xmlconfig);
xmlconfig.getNodeValueReduced("epsilon", _epsilon);
xmlconfig.getNodeValueReduced("sigma", _sigma);
xmlconfig.getNodeValue("shifted", _shiftRequested);
// Read shifted value and check if its boolean (true/false) to avoid legacy-errors
std::string strShifted = "false";
xmlconfig.getNodeValue("shifted", strShifted);
if (strShifted == "true" || strShifted == "false") {
HomesGH marked this conversation as resolved.
Show resolved Hide resolved
xmlconfig.getNodeValue("shifted", _shiftRequested);
HomesGH marked this conversation as resolved.
Show resolved Hide resolved
} else {
global_log->error() << "Parameter <shifted> of components has to be either set to 'true' or 'false'" << std::endl;
mardyn_exit(1);
}
}

/// write to stream
Expand Down
Loading