1
0
Fork 0
mirror of https://github.com/cosmo-sims/MUSIC.git synced 2024-09-19 17:03:46 +02:00

Added some logging infra structure.

This commit is contained in:
Oliver Hahn 2010-08-14 18:19:48 -07:00
parent b5d5cd0d90
commit 28701c3d8e
7 changed files with 318 additions and 12 deletions

View file

@ -253,7 +253,6 @@ namespace convolution{
}else{ }else{
rr2 = rr[0]*rr[0]+rr[1]*rr[1]+rr[2]*rr[2]; rr2 = rr[0]*rr[0]+rr[1]*rr[1]+rr[2]*rr[2];
val += tfr->compute_real(rr2); val += tfr->compute_real(rr2);
kdata_[idx] += (fftw_real)tfr->compute_real(rr2);
} }
} }
} }

View file

@ -516,13 +516,13 @@ void GenerateDensityHierarchy( config_file& cf, transfer_function *ptf, tf_type
top = new DensityGrid<real_t>( nbase, nbase, nbase ); top = new DensityGrid<real_t>( nbase, nbase, nbase );
random_numbers<real_t> *rc; random_numbers<real_t> *rc;
int x0[3] = { refh.offset_abs(levelmin,0)-lfac*shift[0], /*int x0[3] = { refh.offset_abs(levelmin,0)-lfac*shift[0],
refh.offset_abs(levelmin,1)-lfac*shift[1], refh.offset_abs(levelmin,1)-lfac*shift[1],
refh.offset_abs(levelmin,2)-lfac*shift[2] }; refh.offset_abs(levelmin,2)-lfac*shift[2] };
int lx[3] = { refh.size(levelmin,0), int lx[3] = { refh.size(levelmin,0),
refh.size(levelmin,1), refh.size(levelmin,1),
refh.size(levelmin,2) }; refh.size(levelmin,2) };*/
if( randc[levelmin] == NULL ) if( randc[levelmin] == NULL )
{ {
@ -647,7 +647,7 @@ void GenerateDensityHierarchy( config_file& cf, transfer_function *ptf, tf_type
//... restrict these contributions to the next level //... restrict these contributions to the next level
for( int j=1; j<=levelmax-levelmin; ++j ) for( int j=1; j<=(int)levelmax-(int)levelmin; ++j )
{ {
int R = j; int R = j;
delta.add_patch( refh.offset(levelmin+j,0), refh.offset(levelmin+j,1), refh.offset(levelmin+j,2), delta.add_patch( refh.offset(levelmin+j,0), refh.offset(levelmin+j,1), refh.offset(levelmin+j,2),
@ -723,7 +723,7 @@ void GenerateDensityHierarchy( config_file& cf, transfer_function *ptf, tf_type
meshvar_bnd delta_longrange( *delta.get_grid(levelmin+i) ); meshvar_bnd delta_longrange( *delta.get_grid(levelmin+i) );
coarse->copy_unpad( delta_longrange ); coarse->copy_unpad( delta_longrange );
for( int j=1; j<=levelmax-levelmin-i; ++j ) for( int j=1; j<=(int)levelmax-(int)levelmin-i; ++j )
{ {
int R = j; int R = j;
//delta.add_patch( refh.offset(levelmin+i+j,0), refh.offset(levelmin+i+j,1), refh.offset(levelmin+i+j,2), //delta.add_patch( refh.offset(levelmin+i+j,0), refh.offset(levelmin+i+j,1), refh.offset(levelmin+i+j,2),

View file

@ -24,6 +24,7 @@
#ifndef __GENERAL_HH #ifndef __GENERAL_HH
#define __GENERAL_HH #define __GENERAL_HH
#include "log.hh"
#include <omp.h> #include <omp.h>

136
log.cc Normal file
View file

@ -0,0 +1,136 @@
/*
log.cc - This file is part of MUSIC -
a code to generate multi-scale initial conditions
for cosmological simulations
Copyright (C) 2010 Oliver Hahn
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "log.hh"
#include <iostream>
#include <algorithm>
std::string MUSIC::log::outputFile_;
std::ofstream MUSIC::log::outputStream_;
std::list<MUSIC::log::message> MUSIC::log::messages_;
void (*MUSIC::log::receiver)(const message&) = NULL;
MUSIC::log::messageType MUSIC::log::logLevel_;
std::string RemoveMultipleWhiteSpaces( std::string s )
{
std::string search = " "; // this is 2 spaces
size_t index;
while( (index = s.find(search)) != std::string::npos )
{ // remove 1 character from the string at index
s.erase(index,1);
}
return s;
}
//void MUSIC::log::send(messageType type, const std::string& text)
void MUSIC::log::send(messageType type, std::stringstream& textstr)
{
std::string text = textstr.str();
// Skip logging if minimum level is higher
if (logLevel_)
if (type < logLevel_) return;
// log message
MUSIC::log::message m;
m.type = type;
m.text = text;
time_t t = time(NULL);
m.when = localtime(&t);
messages_.push_back(m);
if( type==Info||type==Warning||type==Error||type==FatalError )
{
std::cout << " - ";
if(type==Warning)
std::cout << "WARNING: ";
if(type==Error)
std::cout << "ERROR: ";
if(type==FatalError)
std::cout << "FATAL: ";
std::cout << text << std::endl;
}
std::replace(text.begin(),text.end(),'\n',' ');
RemoveMultipleWhiteSpaces(text);
// if enabled logging to file
if(outputStream_.is_open())
{
// print time
char buffer[9];
strftime(buffer, 9, "%X", m.when);
outputStream_ << buffer;
// print type
switch(type)
{
case Info: outputStream_ << " | info | "; break;
case DebugInfo: outputStream_ << " | debug | "; break;
case Warning: outputStream_ << " | warning | "; break;
case Error: outputStream_ << " | ERROR | "; break;
case FatalError:outputStream_ << " | FATAL | "; break;
case User: outputStream_ << " | info | "; break;
default: outputStream_ << " | ";
}
// print description
outputStream_ << text << std::endl;
}
// if user wants to catch messages, send it to him
if(receiver)
receiver(m);
}
void MUSIC::log::setOutput(const std::string& filename)
{
//logDebug("Setting output log file: " + filename);
outputFile_ = filename;
// close old one
if(outputStream_.is_open())
outputStream_.close();
// create file
outputStream_.open(filename.c_str());
if(!outputStream_.is_open())
LOGERR("Cannot create/open '" + filename + "' for logging");
//send(Error, "Cannot create/open '" + filename + "' for logging");
}
void MUSIC::log::setLevel(const MUSIC::log::messageType level)
{
logLevel_ = level;
}
MUSIC::log::~log()
{
if(outputStream_.is_open())
outputStream_.close();
}

153
log.hh Normal file
View file

@ -0,0 +1,153 @@
/*
log.hh - This file is part of MUSIC -
a code to generate multi-scale initial conditions
for cosmological simulations
Copyright (C) 2010 Oliver Hahn
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LOG_HH
#define __LOG_HH
#include <string>
#include <list>
#include <fstream>
#include <ctime>
#include <sstream>
/*!
* \brief System for logging runtime library errors, warnings, etc.
*
* This is the class that catches every (debug) info, warning, error, or user message and
* processes it. Messages can be written to files and/or forwarded to user function for
* processing messages.
*/
namespace MUSIC
{
class log
{
public:
log(){}
~log();
/*!
* \brief Types of logged messages.
*/
enum messageType
{
Info,
DebugInfo,
Warning,
Error,
FatalError,
User
};
/*!
* \brief Logged message of type MessageType with some info.
*/
struct message
{
messageType type;
std::string text;
tm* when;
};
/*!
* \brief Open file where to log the messages.
*/
static void setOutput(const std::string& filename);
/*!
* \brief Get the filename of log.
*/
static const std::string& output() { return outputFile_; }
/*!
* \brief Add a new message to log.
* \param type Type of the new message.
* \param text Message.
* \remarks Message is directly passes to user reciever if one is set.
*/
//static void send(messageType type, const std::string& text);
static void send(messageType type, std::stringstream& text);
/*!
* \brief Get the list of all of the logged messages.
*/
static const std::list<message>& messages() { return messages_; }
/*!
* \brief Get the last logged message.
*/
static const message& lastMessage() { return messages_.back(); }
/*!
* \brief Set user function to receive newly sent messages to logger.
*/
static void setUserReceiver(void (*userFunc)(const message&)) { receiver = userFunc; }
/*!
* \brief Set minimum level of message to be logged.
*/
static void setLevel(const log::messageType level);
private:
static std::string outputFile_;
static std::ofstream outputStream_;
static std::list<message> messages_;
static messageType logLevel_;
static void (*receiver)(const message&);
};
}
#ifndef LOGERR
#define LOGERR(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::Error, ss); }
#endif
#ifndef LOGINFO
#define LOGINFO(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::Info, ss); }
#endif
#ifndef LOGWARN
#define LOGWARN(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::Warning, ss); }
#endif
#ifndef LOGFATAL
#define LOGFATAL(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::FatalError, ss); }
#endif
#ifndef LOGDEBUG
#define LOGDEBUG(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::DebugInfo, ss); }
#endif
#ifndef LOGUSER
#define LOGUSER(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::User, ss); }
#endif
/*#define LOGINFO(x) MUSIC::log::send(MUSIC::log::Info, std::stringstream()<<(x));
#define LOGWARN(x) MUSIC::log::send(MUSIC::log::Warning, std::stringstream()<<(x));
#define LOGFATAL(x) MUSIC::log::send(MUSIC::log::FatalError, std::stringstream()<<(x));
*/
#endif //__LOG_HH

29
main.cc
View file

@ -48,7 +48,7 @@
#include "transfer_function.hh" #include "transfer_function.hh"
#define THE_CODE_NAME "music!" #define THE_CODE_NAME "music!"
#define THE_CODE_VERSION "0.4.0a" #define THE_CODE_VERSION "0.5.0a"
namespace music namespace music
@ -272,6 +272,11 @@ int main (int argc, const char * argv[])
exit(0); exit(0);
} }
//... open log file
char logfname[128];
sprintf(logfname,"%s_log.txt",argv[1]);
MUSIC::log::setOutput(logfname);
LOGINFO(std::string("Opening log file '")+logfname+"'.")
/******************************************************************************************************/ /******************************************************************************************************/
/* read and interpret config file *********************************************************************/ /* read and interpret config file *********************************************************************/
@ -285,7 +290,7 @@ int main (int argc, const char * argv[])
boxlength = cf.getValue<double>( "setup", "boxlength" ); boxlength = cf.getValue<double>( "setup", "boxlength" );
lbase = cf.getValue<unsigned>( "setup", "levelmin" ); lbase = cf.getValue<unsigned>( "setup", "levelmin" );
lmax = cf.getValue<unsigned>( "setup", "levelmax" ); lmax = cf.getValue<unsigned>( "setup", "levelmax" );
lbaseTF = cf.getValueSafe<unsigned>( "setup", "levelminTF", lbase ); lbaseTF = cf.getValueSafe<unsigned>( "setup", "levelmin_TF", lbase );
force_shift = cf.getValueSafe<bool>("setup", "force_shift", force_shift ); force_shift = cf.getValueSafe<bool>("setup", "force_shift", force_shift );
@ -296,8 +301,11 @@ int main (int argc, const char * argv[])
{ {
std::cout << " - WARNING: levelminTF < levelmin. This is not good!\n" std::cout << " - WARNING: levelminTF < levelmin. This is not good!\n"
<< " I will set levelminTF = levelmin.\n"; << " I will set levelminTF = levelmin.\n";
LOGUSER("levelminTF < levelmin. set levelminTF = levelmin.");
lbaseTF = lbase; lbaseTF = lbase;
cf.insertValue("setup","levelminTF",cf.getValue<std::string>("setup","levelmin")); cf.insertValue("setup","levelmin_TF",cf.getValue<std::string>("setup","levelmin"));
} }
temp = cf.getValue<std::string>( "setup", "ref_offset" ); temp = cf.getValue<std::string>( "setup", "ref_offset" );
@ -409,11 +417,13 @@ int main (int argc, const char * argv[])
try{ try{
if( ! do_2LPT ) if( ! do_2LPT )
{ {
LOGUSER("Entering 1LPT branch")
//... cdm density and displacements //... cdm density and displacements
std::cout << "=============================================================\n"; std::cout << "=============================================================\n";
std::cout << " COMPUTING DARK MATTER DISPLACEMENTS\n"; std::cout << " COMPUTING DARK MATTER DISPLACEMENTS\n";
std::cout << "-------------------------------------------------------------\n"; std::cout << "-------------------------------------------------------------\n";
LOGUSER("Computing dark matter displacements...")
grid_hierarchy f( nbnd ), u(nbnd); grid_hierarchy f( nbnd ), u(nbnd);
@ -449,6 +459,7 @@ int main (int argc, const char * argv[])
std::cout << "=============================================================\n"; std::cout << "=============================================================\n";
std::cout << " COMPUTING BARYON DENSITY\n"; std::cout << " COMPUTING BARYON DENSITY\n";
std::cout << "-------------------------------------------------------------\n"; std::cout << "-------------------------------------------------------------\n";
LOGUSER("Computing baryon density...")
GenerateDensityHierarchy( cf, the_transfer_function_plugin, baryon , rh_TF, f, false, true ); GenerateDensityHierarchy( cf, the_transfer_function_plugin, baryon , rh_TF, f, false, true );
coarsen_density(rh_Poisson, f); coarsen_density(rh_Poisson, f);
@ -468,6 +479,7 @@ int main (int argc, const char * argv[])
std::cout << "=============================================================\n"; std::cout << "=============================================================\n";
std::cout << " COMPUTING VELOCITIES\n"; std::cout << " COMPUTING VELOCITIES\n";
std::cout << "-------------------------------------------------------------\n"; std::cout << "-------------------------------------------------------------\n";
LOGUSER("Computing velocitites...")
//... velocities //... velocities
if( do_baryons ) if( do_baryons )
@ -496,11 +508,14 @@ int main (int argc, const char * argv[])
}else { }else {
//.. use 2LPT ... //.. use 2LPT ...
LOGUSER("Entering 2LPT branch")
grid_hierarchy f( nbnd ), u1(nbnd), u2(nbnd); grid_hierarchy f( nbnd ), u1(nbnd), u2(nbnd);
std::cout << "=============================================================\n"; std::cout << "=============================================================\n";
std::cout << " COMPUTING VELOCITIES\n"; std::cout << " COMPUTING VELOCITIES\n";
std::cout << "-------------------------------------------------------------\n"; std::cout << "-------------------------------------------------------------\n";
LOGUSER("Computing velocities...")
GenerateDensityHierarchy( cf, the_transfer_function_plugin, total , rh_TF, f, true, false ); GenerateDensityHierarchy( cf, the_transfer_function_plugin, total , rh_TF, f, true, false );
coarsen_density(rh_Poisson, f); coarsen_density(rh_Poisson, f);
@ -548,7 +563,8 @@ int main (int argc, const char * argv[])
std::cout << "=============================================================\n"; std::cout << "=============================================================\n";
std::cout << " COMPUTING DARK MATTER DISPLACEMENTS\n"; std::cout << " COMPUTING DARK MATTER DISPLACEMENTS\n";
std::cout << "-------------------------------------------------------------\n"; std::cout << "-------------------------------------------------------------\n";
LOGUSER("Computing dark matter displacements...")
//... //...
//u1 += u2; //u1 += u2;
@ -592,6 +608,7 @@ int main (int argc, const char * argv[])
std::cout << "=============================================================\n"; std::cout << "=============================================================\n";
std::cout << " COMPUTING BARYON DENSITY\n"; std::cout << " COMPUTING BARYON DENSITY\n";
std::cout << "-------------------------------------------------------------\n"; std::cout << "-------------------------------------------------------------\n";
LOGUSER("Computing baryon density...")
GenerateDensityHierarchy( cf, the_transfer_function_plugin, cdm , rh_TF, f, false, true ); GenerateDensityHierarchy( cf, the_transfer_function_plugin, cdm , rh_TF, f, false, true );
coarsen_density(rh_Poisson, f); coarsen_density(rh_Poisson, f);
@ -650,7 +667,7 @@ int main (int argc, const char * argv[])
/** we are done ! **/ /** we are done ! **/
std::cout << " - Done!" << std::endl << std::endl; std::cout << " - Done!" << std::endl << std::endl;
LOGUSER("Done");
///*****************************************/// ///*****************************************///

View file

@ -1191,8 +1191,8 @@ public:
<< " size = (" << std::setw(5) << nx_[ilevel] << ", " << std::setw(5) << ny_[ilevel] << ", " << std::setw(5) << nz_[ilevel] << ")\n"; << " size = (" << std::setw(5) << nx_[ilevel] << ", " << std::setw(5) << ny_[ilevel] << ", " << std::setw(5) << nz_[ilevel] << ")\n";
} }
std::cout << "-------------------------------------------------------------\n"; std::cout << "-------------------------------------------------------------\n";
} }
}; };
#endif #endif