Restructuring Code. Removed some small classes which provided only 1 function, like parameter. Now each class calls it own parameters and stores them as static const.
Merged function containers. Added HDF5 output and reworked Logger class into Base class and 2 derived classes ASCI_Logger and H5_Logger
This commit is contained in:
+241
-68
@@ -8,48 +8,30 @@
|
||||
|
||||
#include "logger.hpp"
|
||||
|
||||
#include "myexeption.h"
|
||||
#include "hdf5.h"
|
||||
#include "hdf5_hl.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <sys/stat.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
Logger::Logger() {
|
||||
|
||||
const int Base_Logger::numberZBins = getParameter("numberZBins");
|
||||
const double Base_Logger::wgtsLength_m = getParameter("wgtsLength_m");
|
||||
|
||||
// Base_Logger
|
||||
Base_Logger::Base_Logger() {
|
||||
if (numberZBins == 0) {
|
||||
cout<<"Logging turned off."<<endl;
|
||||
logging = false;
|
||||
return;
|
||||
}
|
||||
create_outdirName();
|
||||
set_zLogValues();
|
||||
set_outStream();
|
||||
|
||||
}
|
||||
|
||||
void Logger::log_Electron(Electron *logElectron, blitz::TinyVector<double,3> oldLoc_m) {
|
||||
if (!logging) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Assumption: no change of sign in v_z
|
||||
if (numberZBins == -1) {
|
||||
outStream[0]<<*logElectron<<endl;
|
||||
} else {
|
||||
double zold = oldLoc_m[2];
|
||||
double znew = logElectron->get_Location_m()[2];
|
||||
double zmin = min(zold,znew);
|
||||
double zmax = max(zold,znew);
|
||||
|
||||
for (int i=0;i<numberZBins;i++) {
|
||||
if (zLogValues[i]>=zmin && zLogValues[i]<=zmax) {
|
||||
outStream[i]<<*logElectron<<endl; // Location wrong, because it s the new Location.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::set_zLogValues() {
|
||||
void Base_Logger::set_zLogValues() {
|
||||
zLogValues.clear();
|
||||
logging = true;
|
||||
if (numberZBins == -1) {
|
||||
@@ -66,7 +48,237 @@ void Logger::set_zLogValues() {
|
||||
return;
|
||||
}
|
||||
|
||||
void Logger::set_outStream() {
|
||||
string Base_Logger::getTimeString() {
|
||||
time_t rawtime;
|
||||
struct tm * timeinfo;
|
||||
char buffer[80];
|
||||
time (&rawtime);
|
||||
timeinfo = localtime(&rawtime);
|
||||
strftime(buffer,sizeof(buffer),"%Y%m%d",timeinfo);
|
||||
string str(buffer);
|
||||
return(str);
|
||||
}
|
||||
|
||||
std::string Base_Logger::getparName() {
|
||||
string parpath = getenv("parpath");
|
||||
string parfile = remove_catEnding(getenv("parfile"));
|
||||
return (parpath + "/" + parfile);
|
||||
}
|
||||
|
||||
std::string Base_Logger::remove_catEnding (std::string inString) {
|
||||
if ( inString.substr(inString.size()-4, 4) == ".cat" ){
|
||||
return (inString.substr(0, inString.size()-4) );
|
||||
} else {
|
||||
return (inString);
|
||||
}
|
||||
}
|
||||
|
||||
bool Base_Logger::isFile(std::string fileName) {
|
||||
struct stat buffer;
|
||||
return (stat (fileName.c_str(), &buffer) == 0);
|
||||
}
|
||||
|
||||
bool Base_Logger::isDir(std::string dirPath) {
|
||||
struct stat info;
|
||||
if( stat( dirPath.c_str(), &info ) != 0 )
|
||||
return (false);
|
||||
else if( info.st_mode & S_IFDIR ) // S_ISDIR() doesn't exist on my windows
|
||||
return (true);
|
||||
else
|
||||
return (false);
|
||||
}
|
||||
|
||||
// H5_Logger
|
||||
H5_Logger::H5_Logger() : Base_Logger() {
|
||||
set_outH5FName();
|
||||
create_outH5F();
|
||||
create_H5Gs();
|
||||
create_particle_electronH5TBs();
|
||||
//set_outStream();
|
||||
}
|
||||
|
||||
// File
|
||||
void H5_Logger::set_outH5FName() {
|
||||
string parname = getparName();
|
||||
stringstream H5name;
|
||||
int i = 1;
|
||||
do {
|
||||
H5name.str(std::string());
|
||||
H5name << parname << "_" << getTimeString() << "_" << i++<<".h5";
|
||||
} while ( isFile(H5name.str()) );
|
||||
outH5FName = H5name.str();
|
||||
return;
|
||||
}
|
||||
|
||||
void H5_Logger::create_outH5F() {
|
||||
const string consth5Name = outH5FName;
|
||||
outH5F = H5Fcreate(consth5Name.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
|
||||
}
|
||||
|
||||
// Groups
|
||||
void H5_Logger::create_H5Gs() {
|
||||
particleH5G = H5Gcreate2(outH5F, "/Particles", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
|
||||
if (numberZBins == -1) {
|
||||
particle_zH5G = new hid_t[1];
|
||||
particle_zH5G[0] = H5Gcreate2(outH5F, "/Particles/z=all", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
|
||||
} else {
|
||||
particle_zH5G = new hid_t [numberZBins];
|
||||
for (int i = 0; i < numberZBins; i++)
|
||||
{
|
||||
stringstream ss;
|
||||
ss << "/Particles/z=" << zLogValues[i];
|
||||
particle_zH5G[i] = H5Gcreate2(outH5F, ss.str().c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Electrons
|
||||
const size_t H5_Logger::particle_electronH5TB_size = sizeof(logElectron);
|
||||
const char *H5_Logger::particle_electronH5TB_fieldNames[8] = { "Id","ParentId", "x_m", "y_m", "z_m", "vx_ms", "vy_ms", "vz_ms" };
|
||||
const size_t H5_Logger::particle_electronH5TB_offset[8] = {
|
||||
HOFFSET(logElectron, Id),
|
||||
HOFFSET(logElectron, parentId),
|
||||
HOFFSET(logElectron, x_m),
|
||||
HOFFSET(logElectron, y_m),
|
||||
HOFFSET(logElectron, z_m),
|
||||
HOFFSET(logElectron, vx_ms),
|
||||
HOFFSET(logElectron, vy_ms),
|
||||
HOFFSET(logElectron, vz_ms)};
|
||||
const hid_t H5_Logger::particle_electronH5TB_fieldTypes[8] = {
|
||||
H5T_NATIVE_INT,
|
||||
H5T_NATIVE_INT,
|
||||
H5T_NATIVE_DOUBLE,
|
||||
H5T_NATIVE_DOUBLE,
|
||||
H5T_NATIVE_DOUBLE,
|
||||
H5T_NATIVE_DOUBLE,
|
||||
H5T_NATIVE_DOUBLE,
|
||||
H5T_NATIVE_DOUBLE};
|
||||
const hsize_t H5_Logger::particle_electronH5TB_chunkSize = 4096;
|
||||
|
||||
logElectron sampleLogElectron = {0,0,0,0,0,0,0,0};
|
||||
const size_t H5_Logger::particle_electronH5TB_sizes[8] = {
|
||||
sizeof( sampleLogElectron.Id),
|
||||
sizeof( sampleLogElectron.parentId),
|
||||
sizeof( sampleLogElectron.x_m),
|
||||
sizeof( sampleLogElectron.y_m),
|
||||
sizeof( sampleLogElectron.z_m),
|
||||
sizeof( sampleLogElectron.vx_ms),
|
||||
sizeof( sampleLogElectron.vy_ms),
|
||||
sizeof( sampleLogElectron.vz_ms)};
|
||||
|
||||
void H5_Logger::test_log() {
|
||||
Electron testElectron = Electron(vec3(1,2,3), vec3(4,5,6), 7, 8);
|
||||
log_Electron(&testElectron, vec3(9,10,7));
|
||||
return;
|
||||
}
|
||||
|
||||
void H5_Logger::log_Electron(Electron *inElectron,blitz::TinyVector<double,3> oldLoc_m) {
|
||||
if (!logging) {
|
||||
return;
|
||||
}
|
||||
logElectron logElectron_buffer = Electron_to_logElectron(inElectron);
|
||||
// Assumption: no change of sign in v_z
|
||||
if (numberZBins == -1) {
|
||||
H5TBappend_records(particle_zH5G[0], "electrons", 1, particle_electronH5TB_size, particle_electronH5TB_offset, particle_electronH5TB_sizes, &logElectron_buffer );
|
||||
} else {
|
||||
double zold = oldLoc_m[2];
|
||||
double znew = inElectron->get_Location_m()[2];
|
||||
double zmin = min(zold,znew);
|
||||
double zmax = max(zold,znew);
|
||||
|
||||
for (int i=0;i<numberZBins;i++) {
|
||||
if (zLogValues[i]>=zmin && zLogValues[i]<=zmax) {
|
||||
H5TBappend_records(particle_zH5G[i], "electrons", 1, particle_electronH5TB_size, particle_electronH5TB_offset, particle_electronH5TB_sizes, &logElectron_buffer ); // Location wrong, because it s the new Location.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void H5_Logger::create_particle_electronH5TBs() {
|
||||
if (numberZBins == -1) {
|
||||
particle_electronH5T = new hid_t[1];
|
||||
particle_electronH5T[0] = H5TBmake_table( "Electron Table", particle_zH5G[0], "electrons", 8, 0,
|
||||
particle_electronH5TB_size, particle_electronH5TB_fieldNames, particle_electronH5TB_offset, particle_electronH5TB_fieldTypes,
|
||||
particle_electronH5TB_chunkSize, NULL, 0, NULL);
|
||||
} else {
|
||||
particle_electronH5T = new hid_t[numberZBins];
|
||||
for (int i = 0; i < numberZBins; i++)
|
||||
{
|
||||
particle_electronH5T[i] = H5TBmake_table( "Electron Table", particle_zH5G[i], "electrons",8,0,
|
||||
particle_electronH5TB_size, particle_electronH5TB_fieldNames, particle_electronH5TB_offset, particle_electronH5TB_fieldTypes,
|
||||
particle_electronH5TB_chunkSize, NULL, 0, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logElectron H5_Logger::Electron_to_logElectron(Electron *inElectron) {
|
||||
logElectron res;
|
||||
res.Id = inElectron->get_Id();
|
||||
res.parentId = inElectron->get_ParentId();
|
||||
res.x_m = inElectron->get_Location_m()[0];
|
||||
res.y_m = inElectron->get_Location_m()[1];
|
||||
res.z_m = inElectron->get_Location_m()[2];
|
||||
res.vx_ms = inElectron->get_Speed_ms()[0];
|
||||
res.vy_ms = inElectron->get_Speed_ms()[1];
|
||||
res.vz_ms = inElectron->get_Speed_ms()[2];
|
||||
return (res);
|
||||
}
|
||||
|
||||
// ASCI_Logger
|
||||
ASCI_Logger::ASCI_Logger() : Base_Logger() {
|
||||
set_outdirName();
|
||||
create_outdir();
|
||||
set_outStream();
|
||||
}
|
||||
|
||||
void ASCI_Logger::log_Electron(Electron *inElectron, blitz::TinyVector<double,3> oldLoc_m) {
|
||||
if (!logging) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Assumption: no change of sign in v_z
|
||||
if (numberZBins == -1) {
|
||||
outStream[0]<<*inElectron<<endl;
|
||||
} else {
|
||||
double zold = oldLoc_m[2];
|
||||
double znew = inElectron->get_Location_m()[2];
|
||||
double zmin = min(zold,znew);
|
||||
double zmax = max(zold,znew);
|
||||
|
||||
for (int i=0;i<numberZBins;i++) {
|
||||
if (zLogValues[i]>=zmin && zLogValues[i]<=zmax) {
|
||||
outStream[i]<<*inElectron<<endl; // Location wrong, because it s the new Location.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ASCI_Logger::set_outdirName() {
|
||||
string parname = getparName();
|
||||
stringstream dirname;
|
||||
int i = 1;
|
||||
do {
|
||||
dirname.str(std::string());
|
||||
dirname << parname << "_" << getTimeString() << "_" << i++;
|
||||
} while ( isDir(dirname.str()) );
|
||||
outdirName = dirname.str();
|
||||
return;
|
||||
}
|
||||
|
||||
void ASCI_Logger::create_outdir() {
|
||||
if (mkdir(outdirName.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
|
||||
stringstream ss;
|
||||
ss<<"ASCI_Logger::create_outdir: Could not create output directiory.\n outdirName = "<<outdirName<<endl;
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void ASCI_Logger::set_outStream() {
|
||||
if (numberZBins == -1) {
|
||||
outStream = new ofstream [1];
|
||||
stringstream ss;
|
||||
@@ -82,42 +294,3 @@ void Logger::set_outStream() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::create_outdirName() {
|
||||
string poub = getenv("parpath");
|
||||
string pname = getenv("parfile");
|
||||
int i = 1;
|
||||
stringstream dirname;
|
||||
struct stat statbuf;
|
||||
int isDir;
|
||||
do {
|
||||
dirname.str(std::string());
|
||||
dirname << poub << "/" << pname << "_" << getTimeString() << "_" << i++;
|
||||
isDir = 0;
|
||||
if (stat(dirname.str().c_str(), &statbuf) != -1) {
|
||||
if (S_ISDIR(statbuf.st_mode)) {
|
||||
isDir = 1;
|
||||
}
|
||||
}
|
||||
} while ( isDir == 1 );
|
||||
if (mkdir(dirname.str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
|
||||
stringstream ss;
|
||||
ss<<"Simulation::initLogger: Could not create folder "<<dirname.str()<<" for Logging.";
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
outdirName = dirname.str();
|
||||
return;
|
||||
}
|
||||
|
||||
string Logger::getTimeString() {
|
||||
time_t rawtime;
|
||||
struct tm * timeinfo;
|
||||
char buffer[80];
|
||||
time (&rawtime);
|
||||
timeinfo = localtime(&rawtime);
|
||||
strftime(buffer,sizeof(buffer),"%Y%m%d",timeinfo);
|
||||
string str(buffer);
|
||||
return(str);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user