Commit 5a33a58d authored by fabianw's avatar fabianw

Merge branch 'nonuniform'

parents dd0c5b3c 9b81d2a6
......@@ -27,18 +27,19 @@
#include <fstream>
#include <ostream>
#include <limits>
#include <regex> // C++11
class Value
{
private:
std::string content;
std::string content;
public:
Value() : content("") {}
Value() : content("") {}
Value(std::string content_) : content(content_) { /*printf("%s\n",content.c_str());*/ }
Value(std::string content_) : content(content_) { /*printf("%s\n",content.c_str());*/ }
Value(const Value& c) : content(c.content) {}
......@@ -55,47 +56,47 @@ public:
}
Value operator+(const Value& rhs) { return Value(content + " " + rhs.content); }
double asDouble(double def=0)
{
if (content == "")
double asDouble(double def=0)
{
if (content == "")
{
std::ostringstream sbuf;
sbuf << def;
content = sbuf.str();
}
return (double) atof(content.c_str());
}
return (double) atof(content.c_str());
}
int asInt(int def=0)
{
if (content == "")
int asInt(int def=0)
{
if (content == "")
{
std::ostringstream sbuf;
sbuf << def;
content = sbuf.str();
}
return atoi(content.c_str());
}
return atoi(content.c_str());
}
bool asBool(bool def=false)
{
if (content == "")
bool asBool(bool def=false)
{
if (content == "")
{
if (def) content = "true";
else content = "false";
}
if (content == "0") return false;
if (content == "false") return false;
if (content == "0") return false;
if (content == "false") return false;
return true;
}
return true;
}
std::string asString(std::string def="")
{
if (content == "") content = def;
std::string asString(std::string def="")
{
if (content == "") content = def;
return content;
}
return content;
}
friend std::ostream& operator<<(std::ostream& lhs, const Value& rhs)
{
......@@ -108,12 +109,12 @@ public:
class CommandlineParser
{
private:
const int iArgC;
char** vArgV;
bool bStrictMode, bVerbose;
const int iArgC;
char** vArgV;
bool bStrictMode, bVerbose;
protected:
std::map<std::string,Value> mapArguments;
std::map<std::string,Value> mapArguments;
inline void _normalizeKey(std::string& key) const
{
......@@ -129,59 +130,65 @@ protected:
public:
Value& operator()(std::string key)
{
Value& operator()(std::string key)
{
_normalizeKey(key);
if (bStrictMode)
{
if (!_existKey(key,mapArguments))
{
printf("Runtime option NOT SPECIFIED! ABORTING! name: %s\n",key.data());
abort();
}
}
if (bVerbose) printf("%s is %s\n", key.data(), mapArguments[key].asString().data());
return mapArguments[key];
}
inline bool check(std::string key) const
{
if (bStrictMode)
{
if (!_existKey(key,mapArguments))
{
printf("Runtime option NOT SPECIFIED! ABORTING! name: %s\n",key.data());
abort();
}
}
if (bVerbose) printf("%s is %s\n", key.data(), mapArguments[key].asString().data());
return mapArguments[key];
}
inline bool check(std::string key) const
{
_normalizeKey(key);
return _existKey(key,mapArguments);
}
CommandlineParser(const int argc, char ** argv) : iArgC(argc), vArgV(argv), bStrictMode(false), bVerbose(true), mapArguments()
{
for (int i=1; i<argc; i++)
if (argv[i][0] == '-')
{
std::string values = "";
int itemCount = 0;
for (int j=i+1; j<argc; j++)
return _existKey(key,mapArguments);
}
CommandlineParser(const int argc, char ** argv) : iArgC(argc), vArgV(argv), bStrictMode(false), bVerbose(true), mapArguments()
{
// parse commandline <key> <value> pairs. Key passed on the command
// line must start with a leading dash (-). For example:
// -mykey myvalue0 [myvalue1 ...]
for (int i=1; i<argc; i++)
if (argv[i][0] == '-')
{
std::string values = "";
int itemCount = 0;
// check if the current key i is a list of values. If yes,
// concatenate them into a string
for (int j=i+1; j<argc; j++)
{
// if the current value is numeric and (possibly) negative,
// do not interpret it as a key
const bool leadingDash = (argv[j][0] == '-');
const char c = argv[j][1];
const bool firstNumeric = ((c >= '0' && c <= '9') || c == 0) ? true : false;
if (leadingDash && !firstNumeric)
break;
else
{
if (strcmp(values.c_str(), ""))
values += ' ';
values += argv[j];
itemCount++;
}
const bool isNumeric = std::regex_match(argv[j], std::regex("(\\+|-)?[0-9]*(\\.[0-9]*)?((e|E)(\\+|-)?[0-9]*)?"));
if (leadingDash && !isNumeric)
break;
else
{
if (strcmp(values.c_str(), ""))
values += ' ';
values += argv[j];
itemCount++;
}
}
if (itemCount == 0)
values = "true";
if (itemCount == 0)
values = "true";
std::string key(argv[i]);
key.erase(0,1); // remove leading '-'
if (key[0] == '+')
if (key[0] == '+') // for key concatenation
{
key.erase(0,1);
if (!_existKey(key,mapArguments))
......@@ -189,70 +196,70 @@ public:
else
mapArguments[key] += Value(values);
}
else
else // regular key
{
if (!_existKey(key,mapArguments))
mapArguments[key] = Value(values);
}
i += itemCount;
}
mute();
//printf("found %ld arguments of %d\n",mapArguments.size(),argc);
}
int getargc() const { return iArgC; }
char** getargv() const { return vArgV; }
void set_strict_mode()
{
bStrictMode = true;
}
void unset_strict_mode()
{
bStrictMode = false;
}
void mute()
{
bVerbose = false;
}
void loud()
{
bVerbose = true;
}
void save_options(std::string path=".")
{
std::string options;
for(std::map<std::string,Value>::iterator it=mapArguments.begin(); it!=mapArguments.end(); it++)
{
options+= it->first + " " + it->second.asString() + " ";
}
std::string filepath = (path + "/" + std::string("argumentparser.log"));
FILE * f = fopen(filepath.data(), "a");
if (f == NULL)
{
printf("impossible to write %s.\n", filepath.data());
return;
}
fprintf(f, "%s\n", options.data());
fclose(f);
}
void print_args()
{
for(std::map<std::string,Value>::iterator it=mapArguments.begin(); it!=mapArguments.end(); it++)
{
i += itemCount;
}
mute();
//printf("found %ld arguments of %d\n",mapArguments.size(),argc);
}
int getargc() const { return iArgC; }
char** getargv() const { return vArgV; }
void set_strict_mode()
{
bStrictMode = true;
}
void unset_strict_mode()
{
bStrictMode = false;
}
void mute()
{
bVerbose = false;
}
void loud()
{
bVerbose = true;
}
void save_options(std::string path=".")
{
std::string options;
for(std::map<std::string,Value>::iterator it=mapArguments.begin(); it!=mapArguments.end(); it++)
{
options+= it->first + " " + it->second.asString() + " ";
}
std::string filepath = (path + "/" + std::string("argumentparser.log"));
FILE * f = fopen(filepath.data(), "a");
if (f == NULL)
{
printf("impossible to write %s.\n", filepath.data());
return;
}
fprintf(f, "%s\n", options.data());
fclose(f);
}
void print_args()
{
for(std::map<std::string,Value>::iterator it=mapArguments.begin(); it!=mapArguments.end(); it++)
{
std::cout.width(50);
std::cout.fill('.');
std::cout << std::left << it->first;
std::cout << ": " << it->second.asString() << std::endl;
}
}
}
};
......
......@@ -9,46 +9,118 @@
#pragma once
#include <cstdlib>
#include "MeshMap.h"
struct BlockInfo
{
long long blockID;
void * ptrBlock;
long long blockID;
void * ptrBlock;
bool special;
int index[3];
double origin[3];
double h, h_gridpoint;
template <typename T>
inline void pos(T p[2], int ix, int iy) const
{
p[0] = origin[0] + h_gridpoint*(ix+0.5);
p[1] = origin[1] + h_gridpoint*(iy+0.5);
}
template <typename T>
inline void pos(T p[3], int ix, int iy, int iz) const
{
p[0] = origin[0] + h_gridpoint*(ix+0.5);
p[1] = origin[1] + h_gridpoint*(iy+0.5);
p[2] = origin[2] + h_gridpoint*(iz+0.5);
}
BlockInfo(long long ID, const int idx[3], const double _pos[3], const double spacing, double h_gridpoint_, void * ptr=NULL, const bool _special=false):
blockID(ID), ptrBlock(ptr), special(_special)
{
index[0] = idx[0];
index[1] = idx[1];
index[2] = idx[2];
origin[0] = _pos[0];
origin[1] = _pos[1];
origin[2] = _pos[2];
h = spacing;
h_gridpoint = h_gridpoint_;
}
BlockInfo():blockID(-1), ptrBlock(NULL) {}
int index[3];
double origin[3];
double h, h_gridpoint;
double uniform_grid_spacing[3];
double block_extent[3];
double* ptr_grid_spacing[3];
bool bUniform[3];
///////////////////////////////////////////////////////////////////////////////
template <typename T>
inline void pos(T p[2], int ix, int iy) const
{
const int I[2] = {ix, iy};
for (int j = 0; j < 2; ++j)
{
T delta = 0.0;
if (bUniform[j])
delta = uniform_grid_spacing[j]*(I[j]+0.5);
else
{
const double* const dh = ptr_grid_spacing[j];
for (int i = 0; i < I[j]; ++i)
delta += dh[i];
delta += 0.5*dh[I[j]];
}
p[j] = origin[j] + delta;
}
}
template <typename T>
inline void pos(T p[3], int ix, int iy, int iz) const
{
const int I[3] = {ix, iy, iz};
for (int j = 0; j < 3; ++j)
{
T delta = 0.0;
if (bUniform[j])
delta = uniform_grid_spacing[j]*(I[j]+0.5);
else
{
const double* const dh = ptr_grid_spacing[j];
for (int i = 0; i < I[j]; ++i)
delta += dh[i];
delta += 0.5*dh[I[j]];
}
p[j] = origin[j] + delta;
}
}
///////////////////////////////////////////////////////////////////////////////
BlockInfo(long long ID, const int idx[3], const double _pos[3], const double spacing, double h_gridpoint_, void * ptr=NULL, const bool _special=false):
blockID(ID), ptrBlock(ptr), special(_special)
{
h = spacing;
h_gridpoint = h_gridpoint_;
for (int i = 0; i < 3; ++i)
{
ptr_grid_spacing[i] = NULL;
index[i] = idx[i];
origin[i] = _pos[i];
uniform_grid_spacing[i] = h_gridpoint_;
block_extent[i] = h;
bUniform[i] = true;
}
}
template <typename TBlock>
BlockInfo(long long ID, const int idx[3], MeshMap<TBlock>* const mapX, MeshMap<TBlock>* const mapY, MeshMap<TBlock>* const mapZ, void * ptr=NULL, const bool _special=false):
blockID(ID), ptrBlock(ptr), special(_special)
{
// TODO: [fabianw@mavt.ethz.ch; Wed May 03 2017 05:06:58 PM (-0700)]
// ugly but keep this for the moment to ensure that nothing breaks
// down as this has propagated into uniform mesh applications.
// WARNING: THIS CAN CAUSE UNEXPECTED RESULTS (if used with a
// nonuniform grid)!
h = -1.0;
h_gridpoint = -1.0;
MeshMap<TBlock>* const ptr_map[3] = {mapX, mapY, mapZ};
for (int i = 0; i < 3; ++i)
{
index[i] = idx[i];
origin[i] = ptr_map[i]->block_origin(idx[i]);
block_extent[i] = ptr_map[i]->block_width(idx[i]);
ptr_grid_spacing[i] = ptr_map[i]->get_grid_spacing(idx[i]);
bUniform[i] = ptr_map[i]->uniform();
const double* const dh = ptr_grid_spacing[i];
if (bUniform[i]) uniform_grid_spacing[i] = dh[0];
else uniform_grid_spacing[i] = -1.0;
}
}
BlockInfo():blockID(-1), ptrBlock(NULL) {}
};
This diff is collapsed.
......@@ -13,73 +13,73 @@
template<typename MyBlockLab>
class BlockLabMPI : public MyBlockLab
{
const SynchronizerMPI * refSynchronizerMPI;
typedef typename MyBlockLab::BlockType BlockType;
const SynchronizerMPI * refSynchronizerMPI;
typedef typename MyBlockLab::BlockType BlockType;
protected:
int mypeindex[3], pesize[3], mybpd[3];
int gLastX, gLastY, gLastZ;
int mypeindex[3], pesize[3], mybpd[3];
int gLastX, gLastY, gLastZ;
public:
template< typename TGrid >
void prepare(GridMPI<TGrid>& grid, const SynchronizerMPI& SynchronizerMPI)
{
refSynchronizerMPI = &SynchronizerMPI;
refSynchronizerMPI->getpedata(mypeindex, pesize, mybpd);
StencilInfo stencil = refSynchronizerMPI->getstencil();
assert(stencil.isvalid());
MyBlockLab::prepare(grid, stencil.sx, stencil.ex, stencil.sy, stencil.ey, stencil.sz, stencil.ez, stencil.tensorial);
gLastX = grid.getBlocksPerDimension(0)-1;
gLastY = grid.getBlocksPerDimension(1)-1;
gLastZ = grid.getBlocksPerDimension(2)-1;
}
void load(const BlockInfo& info, const Real t=0, const bool applybc=true)
{
MyBlockLab::load(info, t, false);
assert(refSynchronizerMPI != NULL);
const int xorigin = mypeindex[0]*mybpd[0];
const int yorigin = mypeindex[1]*mybpd[1];
const int zorigin = mypeindex[2]*mybpd[2];
const bool xskin = (info.index[0] == xorigin || info.index[0] == xorigin + mybpd[0]-1);
const bool yskin = (info.index[1] == yorigin || info.index[1] == yorigin + mybpd[1]-1);
const bool zskin = (info.index[2] == zorigin || info.index[2] == zorigin + mybpd[2]-1);
//const bool xboundary = info.index[0]==0 || info.index[0]==gLastX;
//const bool yboundary = info.index[1]==0 || info.index[1]==gLastY;
//const bool zboundary = info.index[2]==0 || info.index[2]==gLastZ;
//const bool any_periodic = this->is_xperiodic() || this->is_yperiodic() || this->is_zperiodic();
//const bool any_boundary = xboundary || yboundary || zboundary;
if ((xskin || yskin || zskin))// && (!any_boundary || any_boundary && any_periodic))
{
const bool xperiodic = this->is_xperiodic();
const bool yperiodic = this->is_yperiodic();
const bool zperiodic = this->is_zperiodic();
const int rsx = (!xperiodic && info.index[0]==0)? 0 : this->m_stencilStart[0];
const int rex = (!xperiodic && info.index[0]==gLastX) ? BlockType::sizeX : (BlockType::sizeX+this->m_stencilEnd[0]-1);
const int rsy = (!yperiodic && info.index[1]==0)? 0 : this->m_stencilStart[1];
const int rey = (!yperiodic && info.index[1]==gLastY) ? BlockType::sizeY : (BlockType::sizeY+this->m_stencilEnd[1]-1);
const int rsz = (!zperiodic && info.index[2]==0)? 0 : this->m_stencilStart[2];
const int rez = (!zperiodic && info.index[2]==gLastZ) ? BlockType::sizeZ : (BlockType::sizeZ+this->m_stencilEnd[2]-1);
Real * const dst = (Real *)&this->m_cacheBlock->LinAccess(0);
typedef typename MyBlockLab::ElementType ET;
refSynchronizerMPI->fetch((const Real*)info.ptrBlock, dst,
this->m_stencilStart[0], this->m_stencilStart[1], this->m_stencilStart[2],
this->m_cacheBlock->getSize()[0], this->m_cacheBlock->getSize()[1], this->m_cacheBlock->getSize()[2],
sizeof(ET)/sizeof(Real),
rsx, rex, rsy, rey, rsz, rez);
}
if (applybc) MyBlockLab::_apply_bc(info, t);
}
template< typename TGrid >
void prepare(GridMPI<TGrid>& grid, const SynchronizerMPI& SynchronizerMPI)
{
refSynchronizerMPI = &SynchronizerMPI;
refSynchronizerMPI->getpedata(mypeindex, pesize, mybpd);
StencilInfo stencil = refSynchronizerMPI->getstencil();
assert(stencil.isvalid());
MyBlockLab::prepare(grid, stencil.sx, stencil.ex, stencil.sy, stencil.ey, stencil.sz, stencil.ez, stencil.tensorial);
gLastX = grid.getBlocksPerDimension(0)-1;
gLastY = grid.getBlocksPerDimension(1)-1;
gLastZ = grid.getBlocksPerDimension(2)-1;
}
void load(const <