Commit d945d8e6 authored by Thomas Witkowski's avatar Thomas Witkowski
Browse files

Added init and finalize functions in AMDiS namespace. Removed Parameters class.

parent e1523f76
......@@ -64,6 +64,7 @@ SET(AMDIS_SRC ${SOURCE_DIR}/AdaptBase.cc
${SOURCE_DIR}/AdaptInfo.cc
${SOURCE_DIR}/AdaptInstationary.cc
${SOURCE_DIR}/AdaptStationary.cc
${SOURCE_DIR}/AMDiS.cc
${SOURCE_DIR}/Assembler.cc
${SOURCE_DIR}/BasisFunction.cc
${SOURCE_DIR}/Boundary.cc
......
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.
#include "AMDiS.h"
#include <string>
#ifdef HAVE_ZOLTAN
#include <zoltan_cpp.h>
#endif
namespace AMDiS {
using namespace std;
void init(int argc, char **argv)
{
FUNCNAME("AMDiS::init()");
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
PetscInitialize(&argc, &argv, NULL, NULL);
mpi::startRand();
#ifdef HAVE_ZOLTAN
float zoltanVersion = 0.0;
Zoltan_Initialize(argc, argv, &zoltanVersion);
#endif
#endif
TEST_EXIT(argc >= 2)("No init file!\n");
if (string(argv[0]) != "ddt")
Parameters::init(string(argv[1]));
else
Parameters::init(string(argv[2]));
Parameters::readArgv(argc, argv);
}
void finalize()
{
FUNCNAME("AMDiS::finalize()");
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
MeshDistributor::globalMeshDistributor->exitParallelization();
PetscFinalize();
#endif
}
}
......@@ -144,4 +144,11 @@
#endif
namespace AMDiS {
void init(int argc, char **argv);
void finalize();
}
#endif
......@@ -61,7 +61,7 @@ namespace AMDiS {
/// Called at the end of each timestep.
virtual void closeTimestep(AdaptInfo *adaptInfo)
{
int i, size = static_cast<int>(interfaces_.size());
int size = static_cast<int>(interfaces_.size());
for (int i = 0; i < size; i++)
interfaces_[i]->closeTimestep(adaptInfo);
}
......
......@@ -53,12 +53,14 @@ namespace AMDiS {
}
/// Creates a object of the type corresponding to key.
static CreatorInterface<BaseClass>* getCreator(std::string key)
static CreatorInterface<BaseClass>* getCreator(std::string key,
std::string initFileStr)
{
FUNCNAME("CreatorMap::getCreator()");
init();
CreatorInterface<BaseClass> *creator = creatorMap[key];
TEST_EXIT(creator)("no creator for key %s\n", key.c_str());
TEST_EXIT(creator)("No creator for key \"%s\" defined in init file for parameter \"%s\"\n",
key.c_str(), initFileStr.c_str());
return creator;
}
......
......@@ -561,7 +561,8 @@ namespace AMDiS {
in.get();
if (typeName != "NULL") {
elementData = CreatorMap<ElementData>::getCreator(typeName)->create();
elementData =
CreatorMap<ElementData>::getCreator(typeName, "deserializetion from file")->create();
if (elementData)
elementData->deserialize(in);
......
......@@ -74,7 +74,8 @@ namespace AMDiS {
in >> decoratedType;
in.get();
if (decoratedType != "NULL") {
decorated = CreatorMap<ElementData>::getCreator(decoratedType)->create();
decorated =
CreatorMap<ElementData>::getCreator(decoratedType, "deserialization from file")->create();
decorated->deserialize(in);
} else {
decorated = NULL;
......
......@@ -26,21 +26,24 @@ namespace AMDiS {
void setPrecon(PreconPair< Vector >& pair, const Matrix& matrix)
{
/// Creator for the left preconditioner
CreatorInterface< ITL_BasePreconditioner< Vector > >* leftCreator(NULL);
CreatorInterface< ITL_BasePreconditioner<Vector> >* leftCreator(NULL);
/// Creator for the right preconditioner
CreatorInterface< ITL_BasePreconditioner< Vector > >* rightCreator(NULL);
CreatorInterface< ITL_BasePreconditioner<Vector> >* rightCreator(NULL);
std::string preconType("no");
Parameters::get(oem.getName() + "->left precon", preconType);
leftCreator = CreatorMap<ITL_BasePreconditioner< Vector > >::getCreator(preconType);
std::string initFileStr = oem.getName() + "->left precon";
Parameters::get(initFileStr, preconType);
leftCreator =
CreatorMap<ITL_BasePreconditioner<Vector> >::getCreator(preconType, initFileStr);
TEST_EXIT(leftCreator != NULL)
("there is no creator for the given left preconditioner");
preconType = "no";
Parameters::get(oem.getName() + "->right precon", preconType);
rightCreator = CreatorMap<ITL_BasePreconditioner< Vector > >::getCreator(preconType);
initFileStr = oem.getName() + "->right precon";
Parameters::get(initFileStr, preconType);
rightCreator =
CreatorMap<ITL_BasePreconditioner<Vector> >::getCreator(preconType, initFileStr);
TEST_EXIT(rightCreator != NULL)
("there is no creator for the given right preconditioner");
......
......@@ -409,9 +409,8 @@ namespace AMDiS {
static void init(int print, string filename, const char *flags = NULL)
{
WARNING("Parameters::init(int,std::string,const char*) is depreciated. "
"Use Parameters::init(std::string) instead!\n");
init(filename);
ERROR_EXIT("Parameters::init(int,std::string,const char*) is depreciated. "
"Use Parameters::init(std::string) instead!\n");
}
......
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.
#ifndef MTL4TYPES_H
#define MTL4TYPES_H
#include <boost/numeric/mtl/mtl.hpp>
namespace AMDiS {
namespace MTLTypes {
typedef double value_type;
typedef mtl::matrix::compressed2D< value_type > MTLMatrix;
typedef mtl::vector::dense_vector< value_type > MTLVector;
typedef double value_type;
typedef mtl::matrix::compressed2D< value_type > MTLMatrix;
typedef mtl::vector::dense_vector< value_type > MTLVector;
}
}
#endif
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.
#include <fstream>
#include <sstream>
#include <algorithm>
#include <stdarg.h>
#include <time.h>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include "Parameters.h"
#include "Serializer.h"
namespace AMDiS {
using namespace std;
const char Parameters::comment='%';
const string Parameters::separators=",; ";
Parameters* Parameters::singlett = NULL;
int Parameters::getGlobalParameter(int info, const string& key,
const char *format, ...)
{
FUNCNAME("Parameters::getGlobalParameter()");
vector<struct Parameters::param>::iterator i;
param tParam(key);
va_list arg;
Parameters::initIntern();
if (Parameters::singlett->paramInfo) {
if (Parameters::singlett->paramInfo > 1)
info = max(info, Parameters::singlett->paramInfo - 1);
} else {
info = 0;
}
if (0 == Parameters::singlett->allParam.size()) {
if (0 == Parameters::singlett->filename.size()) {
INFO(info, 1)("no parameters defined\n");
return 0;
} else {
Parameters::singlett->read(Parameters::singlett->filename,key);
}
}
i = find(Parameters::singlett->allParam.begin(),
Parameters::singlett->allParam.end(), tParam);
if (i == Parameters::singlett->allParam.end()) {
INFO(info, 1)("initialization of parameter `%s' fails\n", key.data());
return 0;
}
va_start(arg, format);
int count = 0;
INFO(info, 2)("parameter `%s' initialized with: ", key.data());
string s = (*i).parameters;
for (const char *p = format; *p; p++) {
if (*p != '%')
continue;
const char *word = Parameters::singlett->getNextWord(&s);
if (!*word) {
PRINT_INFO(info, 2)("\n");
va_end(arg);
return count;
}
count++;
switch(*++p) {
case 'S':
{
char *Sval = va_arg(arg, char *);
*Sval = static_cast<char>(atoi(word));
INFO(info, 2)("%d ", *Sval);
}
break;
case 'U':
{
unsigned char *Uval = va_arg(arg, unsigned char *);
*Uval = static_cast<unsigned char>(atoi(word));
INFO(info, 2)("%d ", *Uval);
}
break;
case 'c':
{
char *cval = va_arg(arg, char *);
*cval = *word;
INFO(info, 2)("%c ", *cval);
}
break;
case 's':
{
char *sval = va_arg(arg, char *);
strcpy(sval, word);
INFO(info, 2)("%s ", sval);
}
break;
case 'd':
{
int *ival = va_arg(arg, int *);
*ival = atoi(word);
INFO(info, 2)("%d ", *ival);
}
break;
case 'e':
case 'f':
case 'g':
{
double *rval = va_arg(arg, double *);
*rval = atof(word);
INFO(info, 2)("%g ", *rval);
}
break;
case 'v':
{
WorldVector<double> *vecVal = va_arg(arg, WorldVector<double> *);
string sstr = string(word);
int ind = 0;
int found = sstr.find_first_of(",; ");
string seperator;
seperator = sstr[found];
while (found != static_cast<int>(string::npos) &&
ind < static_cast<int>(vecVal->size())) {
if (found > 0) {
(*vecVal)[ind] = atof(sstr.substr(0, found).c_str());
ind++;
}
sstr = sstr.substr(found + 1);
found = sstr.find_first_of(seperator);
}
if (sstr.length() > 0 && ind < static_cast<int>(vecVal->size()))
(*vecVal)[ind] = atof(sstr.c_str());
}
break;
case 'E': // list of double values, example parameter: {a,b,c,d,e}
case 'F':
case 'G':
{
vector<double> *vecVal = va_arg(arg, vector<double> *);
string sstr = string(word);
string brackets1 = "[{(", brackets2 = "]})";
int bracketIdx1 = sstr.find_first_of(brackets1);
int bracket = brackets1.find_first_of(sstr[bracketIdx1]);
int bracketIdx2 = sstr.find_first_of(brackets2[bracket]);
TEST_EXIT(bracket>=0 && bracketIdx1>=0 && bracketIdx2>=0 && bracketIdx1<bracketIdx2)
("no enclosing brackets found in '%s' \n",key.data());
sstr = sstr.substr(bracketIdx1+1, bracketIdx2-bracketIdx1);
int found = sstr.find_first_of(separators);
string seperator;
seperator = sstr[found];
while (found != static_cast<int>(string::npos)) {
if (found > 0) {
vecVal->push_back(atof(sstr.substr(0, found).c_str()));
}
sstr = sstr.substr(found + 1);
found = sstr.find_first_of(seperator);
}
if (sstr.length() > 0)
vecVal->push_back(atof(sstr.c_str()));
if (vecVal->size() == 0)
WARNING("no values in parameter vector!\n");
}
break;
case 'D': // list of int values, example parameter: {a,b,c,d,e}
{
vector<int> *vecVal = va_arg(arg, vector<int> *);
string sstr = string(word);
string brackets1 = "[{(", brackets2 = "]})";
int bracketIdx1 = sstr.find_first_of(brackets1);
int bracket = brackets1.find_first_of(sstr[bracketIdx1]);
int bracketIdx2 = sstr.find_first_of(brackets2[bracket]);
TEST_EXIT(bracket>=0 && bracketIdx1>=0 && bracketIdx2>=0 && bracketIdx1<bracketIdx2)
("no enclosing brackets found in '%s' \n",key.data());
sstr = sstr.substr(bracketIdx1+1, bracketIdx2-bracketIdx1);
int found = sstr.find_first_of(separators);
string seperator;
seperator = sstr[found];
while (found != static_cast<int>(string::npos)) {
if (found > 0) {
vecVal->push_back(atoi(sstr.substr(0, found).c_str()));
}
sstr = sstr.substr(found + 1);
found = sstr.find_first_of(seperator);
}
if (sstr.length() > 0)
vecVal->push_back(atoi(sstr.c_str()));
if (vecVal->size() == 0)
WARNING("no values in parameter vector!\n");
}
break;
case '*':
break;
default:
INFO(info, 2)("\n");
INFO(info, 2)
("unknow format specifier `%%%c', skipping initialization of %s\n",
*p, key.data());
}
}
INFO(info, 2)("\n");
va_end(arg);
return count;
}
void Parameters::read(const string& aFilename,
const string& maxKey)
{
FUNCNAME("Parameters::read()");
char line[256];
int nLine = 0;
string key, parameter;
string actfile;
if (aFilename.size() == 0)
return;
singlett->inputFile.open(aFilename.c_str(), ios::in);
if (!inputFile.is_open()) {
ERROR("init-file can't be opened\n");
return;
}
INFO(paramInfo, 2)("reading from file %s\n", aFilename.data());
actfile = getActFile(aFilename);
while (!inputFile.eof()) {
inputFile.getline(line, 255);
nLine++;
key = getKey(line, nLine, aFilename);
if (key == "")
continue;
parameter = getPar(key, line, &nLine, aFilename);
if (parameter == "")
continue;
addParam(key, parameter);
}
singlett->inputFile.close();
}
const string& Parameters::getActFile(const string& aFilename)
{
FUNCNAME("Parameters::getActFile()");
list< string >::iterator i;
for (i = filenames.begin(); i != filenames.end(); i++)
if (aFilename==*i)
break;
if (i != filenames.end()) {
return(const_cast<const string&>(*i));
} else {
filenames.push_back(aFilename);
return(const_cast<const string&>(aFilename));
}
}
void Parameters::swap(int i, int j)
{
param tmp(allParam[i]);
allParam[i] = allParam[j];
allParam[j] = tmp;
}
void Parameters::qsort(int left, int right)
{
if (left >= right)
return;
swap(left, (left + right) / 2);
int last = left;
for (int i = left + 1; i <= right; i++)
if (allParam[i].key.compare(allParam[left].key) < 0)
swap(++last, i);
swap(left, last);
qsort(left, last - 1);
qsort(last + 1, right);
}
const string Parameters::getKey(const string& s, int nLine,
const string& aFilename)
{
FUNCNAME("Parameters::getKey()");
string fn, key = "";
char c;
int i, epos;
string h = " \t\r\f";
int pos = s.find_first_not_of(" \t\f\r"); //skip Blank
if (pos < 0)
return key;
if (s[pos] == comment || s[pos] == '\0' || s[pos] == '\n')
return(key);
if (s[pos] == '#') {
// if (static_cast<int>(s.find("#include")) == pos) {
// /****************************************************************************/
// /* another init file has to be included */
// /****************************************************************************/
//
// pos += strlen("#include");
// pos = s.find_first_not_of(" \t\f\r");
//
// i = 0;
// switch (c = s[pos++]) {
// case '<':
// c = '>';
// case '\"':
// h += c;
// epos = s.find_first_not_of(h, pos);
// fn = s.substr(pos,epos-1);
//
// if (s[epos] != c) {
// ERROR("aFilename of include not terminated by %c\n", c);
// ERROR("skipping line %d of file %s\n", nLine, aFilename.c_str());
// return("");
// }
// break;
// default:
// ERROR("no aFilename of include file found\n");
// ERROR("skipping line %d of file %s\n", nLine, aFilename.c_str());
// return("");
// }
//
// read(fn);
// return("");
// } else {
// ERROR("# must not be the first character on a line; except #include\n");
// return("");
// }
}
/****************************************************************************/
/* now get the key */
/****************************************************************************/
i = 0;
epos=s.find_first_of(":#",pos+1);
if (s[epos] == '#') {
ERROR("key must not contain '%c'.\n", '#');
ERROR("Skipping line %d of file %s\n", nLine, aFilename.c_str());
return("");
}
key = s.substr(pos,epos);
if (s[epos] != ':') {
ERROR("key was not terminated by ':'.\n");
ERROR("Skipping line %d of file %s\n", nLine, aFilename.c_str());
return("");
}
if (key.size() == 0) {
ERROR("use of ':' without key.\n");
ERROR("Skipping line %d of file %s\n", nLine, aFilename.c_str());