Commit c7e5faaa authored by Sergey Kireev's avatar Sergey Kireev
Browse files

cpp fixed

parent d0ab8da8
......@@ -32,10 +32,10 @@ struct XY {
enum Neighborhood_type { hood_default, hood_vonNeumann, hood_Moore, hood_hex };
template<Neighborhood_type t> struct Neighborhood_value { static constexpr int ndirs = 0, nmovedirs = 0; };
template<> struct Neighborhood_value<hood_vonNeumann> { static constexpr int ndirs = 4, nmovedirs = 4; };
template<> struct Neighborhood_value<hood_Moore> { static constexpr int ndirs = 8, nmovedirs = 4; };
template<> struct Neighborhood_value<hood_hex> { static constexpr int ndirs = 6, nmovedirs = 6; };
template<Neighborhood_type t> struct Neighborhood_value { static constexpr int ndirs = 0; };
template<> struct Neighborhood_value<hood_vonNeumann> { static constexpr int ndirs = 4; };
template<> struct Neighborhood_value<hood_Moore> { static constexpr int ndirs = 8; };
template<> struct Neighborhood_value<hood_hex> { static constexpr int ndirs = 6; };
template<int ndirs> struct Neighborhood_dirs { static constexpr std::array<int,ndirs> dx(),dy(); };
template<> struct Neighborhood_dirs<4> { static constexpr std::array<int,4> dx = {0,1,0,-1},dy = {-1,0,1,0}; };
......@@ -88,31 +88,31 @@ class Neighborhood_base {
};
template<Neighborhood_type t>
class Neighborhood : public Neighborhood_base<Neighborhood_value<t>::nmovedirs> {
class Neighborhood : public Neighborhood_base<Neighborhood_value<t>::ndirs> {
public:
Neighborhood():Neighborhood_base<Neighborhood_value<hood_vonNeumann>::nmovedirs>() {}
Neighborhood():Neighborhood_base<Neighborhood_value<hood_vonNeumann>::ndirs>() {}
};
template<>
class Neighborhood<hood_vonNeumann> : public Neighborhood_base<Neighborhood_value<hood_vonNeumann>::nmovedirs> {
class Neighborhood<hood_vonNeumann> : public Neighborhood_base<Neighborhood_value<hood_vonNeumann>::ndirs> {
public:
Neighborhood():Neighborhood_base<Neighborhood_value<hood_vonNeumann>::nmovedirs>() {}
Neighborhood():Neighborhood_base<Neighborhood_value<hood_vonNeumann>::ndirs>() {}
bool isSquare() const { return true; }
std::string name() { return "hood_vonNeumann"; }
};
template<>
class Neighborhood<hood_Moore> : public Neighborhood_base<Neighborhood_value<hood_Moore>::nmovedirs> {
class Neighborhood<hood_Moore> : public Neighborhood_base<Neighborhood_value<hood_Moore>::ndirs> {
public:
Neighborhood():Neighborhood_base<Neighborhood_value<hood_Moore>::nmovedirs>() {}
Neighborhood():Neighborhood_base<Neighborhood_value<hood_Moore>::ndirs>() {}
bool isSquare() const { return true; }
std::string name() { return "hood_Moore"; }
};
template<>
class Neighborhood<hood_hex> : public Neighborhood_base<Neighborhood_value<hood_hex>::nmovedirs> {
class Neighborhood<hood_hex> : public Neighborhood_base<Neighborhood_value<hood_hex>::ndirs> {
public:
Neighborhood():Neighborhood_base<Neighborhood_value<hood_hex>::nmovedirs>() {}
Neighborhood():Neighborhood_base<Neighborhood_value<hood_hex>::ndirs>() {}
void fix(int &ix,int &iy,int nx,int ny) const {
while (iy < 0) { iy += ny; ix -= ny/2; }
while (iy >= ny) { iy -= ny; ix += ny/2; }
......
#include "ca2d.h"
// TODO: check what parameter values are set (use LOG_NLE_CA to print in constructors, setData(), etc.)
#define LOG_NLE_CA 0
#define logfn(C,X) if (LOG_NLE_CA) fprintf(stderr,"%s::%s(%s)\n",C,__FUNCTION__,X)
......@@ -8,17 +7,18 @@
using namespace ca2d;
template<Neighborhood_type hood_type>
class NLE_CA_base : public CA2D<int,hood_type> {
template<Neighborhood_type dirs_type,Neighborhood_type hood_type=dirs_type>
class NLE_CA_base : public CA2D<int,dirs_type,hood_type> {
int maxgroupsize;
protected:
using ca = CA2D<int,hood_type>;
using nle = NLE_CA_base<hood_type>;
using ca = CA2D<int,dirs_type,hood_type>;
using nle = NLE_CA_base<dirs_type,hood_type>;
static constexpr int ndirs = Neighborhood_value<hood_type>::nmovedirs;
static constexpr int ndirs = Neighborhood_value<dirs_type>::ndirs;
static constexpr int nneighbors = Neighborhood_value<hood_type>::ndirs;
const int nsize; // size of N side
int nnsize; // number of cells in N
......@@ -50,6 +50,8 @@ class NLE_CA_base : public CA2D<int,hood_type> {
evap = 0.0;
pgroup = 0.0;
test_move_NL_dH = false;
//printf("ndirs: %d nneighbors: %d\n",ndirs,nneighbors);
//printf("dirs.count %d hood.count: %d\n",ca::dirs.count,ca::hood.count);
}
//void setXYS() { logfn("NLE_CA_base",""); }
......@@ -146,6 +148,7 @@ class NLE_CA_base : public CA2D<int,hood_type> {
int count_e = 0;
bool wasn = false;
int pbx = 0, pby = 0;
//printf("neighborhood: %d\n", ca::hood.count); fflush(stdout);
for (int d=0;d<ca::hood.count;d++) {
const int state = ca::getn(x,y,d);
if (isN(state)) {
......@@ -169,7 +172,8 @@ class NLE_CA_base : public CA2D<int,hood_type> {
return { count_n, count_l, count_e };
}
virtual bool try_swap_LE(const int lx,const int ly,const int ex,const int ey) {
bool try_swap_LE(const int lx,const int ly,const int ex,const int ey) {
//printf("try_swap_LE(%d,%d, %d,%d) : %d\n",lx,ly,ex,ey,ca::hood.count); fflush(stdout);
const auto cl = neighborhood(lx,ly);
const auto ce = neighborhood(ex,ey);
const double dH = enl*(cl[0]-ce[0]) + el*(cl[1]-ce[1]+1);
......@@ -177,14 +181,18 @@ class NLE_CA_base : public CA2D<int,hood_type> {
return ca::rnd01() < exp(-dH/kT);
}
virtual bool try_evap_L(const int lx,const int ly) {
bool try_evap_L(const int lx,const int ly) {
const auto cl = neighborhood(lx,ly);
const double dH = enl*cl[0] + el*cl[1] + mu;
if (dH <= 0) return true;
return ca::rnd01() < exp(-dH/kT);
//const bool result = dH <= 0 || ca::rnd01() < exp(-dH/kT);
//printf("try_evap_L(%d,%d)(%d) : %s\n",lx,ly,ca::hood.count,result?"true":"false"); fflush(stdout);
//return result;
}
virtual bool try_cond_L(const int ex,const int ey) {
bool try_cond_L(const int ex,const int ey) {
//printf("try_cond_L(%d,%d)(%d)\n",ex,ey,ca::hood.count); fflush(stdout);
const auto ce = neighborhood(ex,ey);
const double dH = -enl*ce[0] - el*ce[1] - mu;
if (dH <= 0) return true;
......@@ -208,7 +216,11 @@ class NLE_CA_base : public CA2D<int,hood_type> {
return dH;
}
virtual std::array<int,3> test_count_nl(int bx,int by,int dir) { return { 0, 0, 0 }; }
virtual std::array<int,3> test_count_nl(int bx,int by,int dir) {
printf("Error: NLE_CA_base<%s,%s>::test_count_nl(%d,%d,%d) is called!\n",ca::dirs.name().c_str(),ca::hood.name().c_str(),bx,by,dir);
exit(-1);
return { 0, 0, 0 };
}
double count_dH_move_NL_test(int bx,int by,int dir) {
XY nb(bx,by);
......@@ -242,7 +254,10 @@ class NLE_CA_base : public CA2D<int,hood_type> {
if (dH <= 0) return true;
return ca::rnd01() < exp(-dH/kT);
}
virtual void do_move_NL(int bi,int bj,int dir) {}
virtual void do_move_NL(int bx,int by,int dir) {
printf("Error: NLE_CA_base<%s,%s>::do_move_nl(%d,%d,%d) is called!\n",ca::dirs.name().c_str(),ca::hood.name().c_str(),bx,by,dir);
exit(-1);
}
void find_groups() {
if (groups_found) return;
......@@ -298,7 +313,10 @@ class NLE_CA_base : public CA2D<int,hood_type> {
return true;
}
virtual void do_move_group_NL(int label,int dir) {}
virtual void do_move_group_NL(int label,int dir) {
printf("Error: NLE_CA_base<%s,%s>::do_move_group_NL(%d,%d) is called!\n",ca::dirs.name().c_str(),ca::hood.name().c_str(),label,dir);
exit(-1);
}
virtual void rule(int i,int j) {
ca::dirs.fix(i,j,ca::nx,ca::ny);
......@@ -398,11 +416,11 @@ class NLE_CA_base : public CA2D<int,hood_type> {
////////////////////////////////////////////////////////////////////////////////////////////////////
template<Neighborhood_type hood_type>
class NLE_sqr_CA : public NLE_CA_base<hood_type> {
class NLE_sqr_CA : public NLE_CA_base<hood_vonNeumann,hood_type> {
protected:
using ca = CA2D<int,hood_type>;
using ca = CA2D<int,hood_vonNeumann,hood_type>;
public:
......@@ -411,7 +429,7 @@ class NLE_sqr_CA : public NLE_CA_base<hood_type> {
protected:
using nle = NLE_CA_base<hood_type>;
using nle = NLE_CA_base<hood_vonNeumann,hood_type>;
using nle::nsize;
using nle::nnsize;
using nle::xN;
......@@ -422,10 +440,10 @@ class NLE_sqr_CA : public NLE_CA_base<hood_type> {
int td[4][6][7]; // test data: x,y,dx,dy,n,first_state,final_state
NLE_sqr_CA(const int nx,const int ny,const int ns = 1) : NLE_CA_base<hood_type>(nx,ny,ns) { logfn_scc(std::string("NLE_sqr_CA<")+ca::hood.name()+std::string(">"),"int,int,int"); }
NLE_sqr_CA(const int nx,const int ny,const int ns = 1) : NLE_CA_base<hood_vonNeumann,hood_type>(nx,ny,ns) { logfn_scc(std::string("NLE_sqr_CA<")+ca::hood.name()+std::string(">"),"int,int,int"); }
void setXYS() {
logfn_scc(std::string("NLE_sqr_CA<")+ca::dirs.name()+std::string(",")+ca::hood.name()+std::string(">"),"");
logfn_scc(std::string("NLE_sqr_CA<")+ca::hood.name()+std::string(">"),"");
const int ns = nsize;
nnsize = ns*ns;
sN.resize(nnsize);
......@@ -453,14 +471,14 @@ class NLE_sqr_CA : public NLE_CA_base<hood_type> {
}
}
virtual bool may_move_NL(int bi,int bj,int dir) const { //!!!
virtual bool may_move_NL(int bi,int bj,int dir) const {
for (int k=0;k<nsize;k++)
if (!nle::isL(ca::getfixd(bi+side[dir][k].x,bj+side[dir][k].y))) return false;
return true;
}
virtual void do_move_NL(int bi,int bj,int dir) {
ca::hood.move(bi,bj,dir);
ca::dirs.move(bi,bj,dir);
int opp = oppdir[dir];
for (int k=0;k<nsize;k++)
ca::getfixd(bi+side[opp][k].x,bj+side[opp][k].y) = nle::L;
......
......@@ -477,12 +477,14 @@ bool Parameters::read_from_cmdline(int argc,char *argv[]) {
fraction_e = default_fraction_e;
}
else { // --E
fraction_n = default_fraction_n;
const double x = 1.0 - fraction_e;
fraction_n = std::min(default_fraction_n,x);
fraction_l = 1.0 - fraction_n - fraction_e;
}
else
if (!set_ife) { // -L-
fraction_n = default_fraction_n;
const double x = 1.0 - fraction_l;
fraction_n = std::min(default_fraction_n,x);
fraction_e = 1.0 - fraction_n - fraction_l;
}
else { // -LE
......@@ -491,7 +493,8 @@ bool Parameters::read_from_cmdline(int argc,char *argv[]) {
else
if (!set_ifl)
if (!set_ife) { // N--
fraction_l = default_fraction_l;
const double x = 1.0 - fraction_n;
fraction_l = std::min(default_fraction_l,x);
fraction_e = 1.0 - fraction_n - fraction_l;
}
else { // N-E
......@@ -503,7 +506,9 @@ bool Parameters::read_from_cmdline(int argc,char *argv[]) {
}
else { // NLE
}
if (std::abs(fraction_n) < 1e-10) fraction_n = 0.0;
if (std::abs(fraction_l) < 1e-10) fraction_l = 0.0;
if (std::abs(fraction_e) < 1e-10) fraction_e = 0.0;
return true;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment