Skip to content

Commit f344c5f

Browse files
committed
Merge branch 'stuart-knock-wave_to_rennie'
This pull request solves #80 and #45.
2 parents 1f602bb + 541e0ab commit f344c5f

26 files changed

+645
-49
lines changed

src/harmonic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ using std::vector;
2525
/**
2626
@brief Computes the derivatives of the Harmonic Propagator function.
2727
28-
The Harmonic propgator equation is given by:
28+
The Harmonic propagator equation is given by:
2929
\f{eqnarray*}{
3030
\frac{d^2\phi}{dt^2}&=& \gamma^2 \left(Q - \frac{2}{\gamma} \frac{d\phi}{dt} - \phi \right)
3131
\f}

src/solver.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/** @file solver.cpp
2-
@brief A brief, one sentence description.
2+
@brief Solver member function definitions.
33
4-
A more detailed multiline description...
4+
The Solver class pulls together Population, Propagator, and Coupling classes.
5+
6+
If you define your own Propagators or Couplings, then, it is in the init
7+
member-function of this class that you must create appropriate entries in
8+
order to make those Propagators and Couplings specifiable via neurofield's
9+
configuration (.conf) files.
510
611
@author Peter Drysdale, Felix Fung,
712
*/
@@ -23,6 +28,7 @@
2328
#include "output.h" // Output; Outlet;
2429
#include "population.h" // Population;
2530
#include "wave.h" // Wave;
31+
#include "wave_legacy.h" // WaveLegacy;
2632

2733
// C++ standard library headers
2834
#include <cmath> // std::remainder; std::sqrt;
@@ -162,6 +168,14 @@ void Solver::init( Configf& configf ) {
162168
} else if(ptype=="HarmonicIntegral") {
163169
propagators.add( new
164170
HarmonicIntegral(nodes,deltat,i, *pops[cnt.pre[i]], *pops[cnt.post[i]], longside, topology));
171+
} else if(ptype=="WaveLegacy") {
172+
if( nodes==1 ) {
173+
propagators.add( new
174+
Harmonic(nodes,deltat,i, *pops[cnt.pre[i]], *pops[cnt.post[i]], longside, topology));
175+
} else {
176+
propagators.add( new
177+
WaveLegacy(nodes,deltat,i, *pops[cnt.pre[i]], *pops[cnt.post[i]], longside, topology));
178+
}
165179
} else {
166180
cerr<<"Invalid propagator type '"<<ptype<<"'."<<endl;
167181
exit(EXIT_FAILURE);

src/solver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @file solver.h
2-
@brief Defines the Solver class, a high level container for managing a simulation.
2+
@brief Declares the Solver class, a high level container for managing a simulation.
33
44
The Solver class pulls together Population, Propagator, and Coupling classes.
55

src/stencil.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ const vector<double>& Stencil::operator= ( const vector<double>& field ) {
148148
return field;
149149
}
150150

151-
152151
void Stencil::set( int node ) const {
153152
if( node>=0 && node<nodes ) {
154153
int x = node%longside;

src/stencil.h

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
/** @file stencil.h
2-
@brief A brief, one sentence description.
2+
@brief Stencil declaration -- spatial neighbourhood use to evaluate \f[{\Delta}x\f].
33
4-
A more detailed multiline description...
4+
This file contains the Stencil declaration, including a definition of Stencil's
5+
grid increment operator (ie, ++), and the parentheses operator (ie, ()). The
6+
default neighbourhood is the orthogonal or von Neumann neighbourhood of a
7+
two-dimensional five-point stencil, that is:
8+
\f{matrix}{
9+
& n & \\
10+
w & c & e \\
11+
& s
12+
\f}
13+
14+
#TODO: a reference to a good review paper or textbook would be better here.
15+
https://en.wikipedia.org/wiki/Five-point_stencil
16+
https://en.wikipedia.org/wiki/Stencil_code
517
618
@author Peter Drysdale, Felix Fung,
719
*/
@@ -13,12 +25,6 @@
1325
#include <string> // std::string;
1426
#include <vector> // std::vector;
1527

16-
enum Moore {
17-
nw=-4, n, ne,
18-
w, c, e,
19-
sw, s, se
20-
};
21-
2228
class Stencil {
2329
Stencil();
2430
Stencil& operator=(const Stencil&);
@@ -32,27 +38,32 @@ class Stencil {
3238

3339
mutable int ptr;
3440
public:
41+
enum Neighbours {
42+
n=-2,
43+
w, c, e,
44+
s
45+
};
46+
3547
Stencil( int nodes, int longside, const std::string& boundary );
3648
virtual ~Stencil(void);
3749

3850
const std::vector<double>& operator= ( const std::vector<double>& field );
39-
inline void operator++ (int) const; // increment Moore grid
51+
inline void operator++ (int) const; // increment Neighbours grid
4052
void set( int node ) const; // point to node
4153
int get(void) const; // get ptr
4254

43-
inline double operator() ( Moore moore=c ) const {
44-
int arr[9] = {ptr-longside-2-1, ptr-longside-2, ptr-longside-2+1,
45-
ptr-1, ptr, ptr+1,
46-
ptr+longside+2-1, ptr+longside+2, ptr+longside+2+1};
47-
return m[arr[moore+4]];
55+
inline double operator() ( Neighbours neighbour=c ) const {
56+
int arr[5] = { ptr-longside-2,
57+
ptr-1, ptr, ptr+1,
58+
ptr+longside+2};
59+
return m[arr[neighbour+2]];
4860
}
4961

5062
inline operator double (void) const {
5163
return (*this)(c);
5264
}
5365
};
5466

55-
5667
void Stencil::operator++ (int) const {
5768
ptr++;
5869
if( ( ptr%(longside+2)==longside+1 ) ) {

src/stencil_legacy.cpp

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/** @file stencil_legacy.cpp
2+
@brief StencilLegacy, square nine-point stencil definition.
3+
4+
As well as a constructor and destructor, this file defines the set() and
5+
get() member functions and a custom equals (=) operator.
6+
7+
@author Peter Drysdale, Felix Fung.
8+
*/
9+
10+
// Main module header
11+
#include "stencil_legacy.h" // StencilLegacy
12+
13+
// C++ standard library headers
14+
#include <iostream> // std::cerr; std::endl;
15+
#include <string> // std::string;
16+
#include <vector> // std::vector;
17+
using std::cerr;
18+
using std::endl;
19+
using std::string;
20+
using std::vector;
21+
22+
StencilLegacy::StencilLegacy( int nodes, int longside, const string& boundary )
23+
: nodes(nodes), longside(longside), shortside(nodes/longside),
24+
boundary(boundary), ptr(0) {
25+
if( boundary != "Torus" && boundary != "Sphere" ) {
26+
cerr<<"StencilLegacy boundary must be Torus or Sphere."<<endl;
27+
exit(EXIT_FAILURE);
28+
}
29+
m = new double[ (shortside+2)*(longside+2) ];
30+
}
31+
32+
StencilLegacy::~StencilLegacy() {
33+
delete[] m;
34+
}
35+
36+
const vector<double>& StencilLegacy::operator= ( const vector<double>& field ) {
37+
if( boundary == "Torus" ) {
38+
// copy centre square
39+
const double* __restrict__ p1 = &field[0];
40+
double* __restrict__ p2 = &m[3+longside];
41+
for( int i=0; i<shortside; i++ ) {
42+
for( int j=0; j<longside; j++ ) {
43+
*p2++ = *p1++;
44+
}
45+
p2 += 2;
46+
}
47+
48+
// copy top row from bottom row
49+
p1 = &field[longside*(shortside-1)];
50+
p2 = &m[1];
51+
for( int i=0; i<longside; i++ ) {
52+
*p2++ = *p1++;
53+
}
54+
55+
// copy bottom row from top row
56+
p1 = &field[0];
57+
p2 = &m[(longside+2)*(shortside+1) +1];
58+
for( int i=0; i<longside; i++ ) {
59+
*p2++ = *p1++;
60+
}
61+
62+
// copy left edge from right edge
63+
p1 = &field[longside -1];
64+
p2 = &m[longside +2];
65+
for( int i=0; i<shortside; i++, p1+=longside, p2+=longside+2 ) {
66+
*p2 = *p1;
67+
}
68+
69+
// copy right edge from left edge
70+
p1 = &field[0];
71+
p2 = &m[2*longside +3];
72+
for( int i=0; i<shortside; i++, p1+=longside, p2+=longside+2 ) {
73+
*p2 = *p1;
74+
}
75+
76+
/*// copy centre square
77+
for( int j=0; j< nodes/longside; j++ )
78+
for( int i=0; i<longside; i++ )
79+
m[(j+1)*(longside+2)+i+1] = field[j*longside+i];
80+
81+
// copy right edge into left boundary
82+
for( int i=0; i<=nodes/longside; i++ )
83+
m[i*(longside+2)] = field[i*longside-1];
84+
85+
// copy left edge into right boundary
86+
for( int i=0; i<=nodes/longside; i++ )
87+
m[(i+2)*(longside+2)-1] = field[i*longside];
88+
89+
// copy bottom edge into top boundary
90+
for( int i=0; i<longside; i++ )
91+
m[i+1] = field[nodes-longside+i];
92+
93+
// copy top edge into bottom boundary
94+
for( int i=0; i<longside; i++ )
95+
m[(nodes/longside+1)*(longside+2)+1+i] = field[i];*/
96+
97+
// copy 4 corners
98+
m[0] = field[nodes-1];
99+
m[longside+1] = field[nodes-longside];
100+
m[(longside+2)*(nodes/longside+1)] = field[longside-1];
101+
m[(longside+2)*(nodes/longside+2)-1] = field[0];
102+
} else if( boundary == "Sphere" ) {
103+
// copy centre square
104+
const double* __restrict__ p1 = &field[0];
105+
double* __restrict__ p2 = &m[3+longside];
106+
for( int i=0; i<shortside; i++ ) {
107+
for( int j=0; j<longside; j++ ) {
108+
*p2++ = *p1++;
109+
}
110+
p2 += 2;
111+
}
112+
113+
// copy top row from bottom row
114+
p1 = &field[longside*(shortside-1)];
115+
p2 = &m[longside+1];
116+
for( int i=0; i<longside; i++ ) {
117+
*p2-- = *p1++;
118+
}
119+
120+
// copy bottom row from top row
121+
p1 = &field[longside-1];
122+
p2 = &m[(longside+2)*(shortside+1) +1];
123+
for( int i=0; i<longside; i++ ) {
124+
*p2++ = *p1--;
125+
}
126+
127+
// copy left edge from right edge
128+
p1 = &field[longside -1];
129+
p2 = &m[longside +2];
130+
for( int i=0; i<shortside; i++, p1+=longside, p2+=longside+2 ) {
131+
*p2 = *p1;
132+
}
133+
134+
// copy right edge from left edge
135+
p1 = &field[0];
136+
p2 = &m[2*longside +3];
137+
for( int i=0; i<shortside; i++, p1+=longside, p2+=longside+2 ) {
138+
*p2 = *p1;
139+
}
140+
141+
// copy 4 corners NEEDS TO MODIFY SOMEWHERE!!
142+
m[0] = field[nodes-1];
143+
m[longside+1] = field[nodes-longside];
144+
m[(longside+2)*(nodes/longside+1)] = field[longside-1];
145+
m[(longside+2)*(nodes/longside+2)-1] = field[0];
146+
}
147+
148+
set(0);
149+
return field;
150+
}
151+
152+
void StencilLegacy::set( int node ) const {
153+
if( node>=0 && node<nodes ) {
154+
int x = node%longside;
155+
int y = node/longside;
156+
ptr = (y+1)*(longside+2) +x+1;
157+
} else {
158+
cerr<<"StencilLegacy node setting out of bound: "<<node<<endl;
159+
exit(EXIT_FAILURE);
160+
}
161+
}
162+
163+
int StencilLegacy::get() const {
164+
int x = ptr%(longside+2)-1;
165+
int y = ptr/(longside+2)-1;
166+
return x+y*longside;
167+
}
168+
169+
/*double StencilLegacy::nw(void) const { return m[ ptr-longside-2-1]; }
170+
double StencilLegacy::n (void) const { return m[ ptr-longside-2 ]; }
171+
double StencilLegacy::ne(void) const { return m[ ptr-longside-2+1]; }
172+
double StencilLegacy:: w(void) const { return m[ ptr -1]; }
173+
double StencilLegacy:: c(void) const { return m[ ptr ]; }
174+
double StencilLegacy:: e(void) const { return m[ ptr +1]; }
175+
double StencilLegacy::sw(void) const { return m[ ptr+longside+2-1]; }
176+
double StencilLegacy::s (void) const { return m[ ptr+longside+2 ]; }
177+
double StencilLegacy::se(void) const { return m[ ptr+longside+2+1]; }*/

src/stencil_legacy.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/** @file stencil_legacy.h
2+
@brief StencilLegacy square nine-point stencil declaration.
3+
4+
The StencilLegacy stencil declaration, including definition of the Moore
5+
neighbourhood and the stencil's Moore-grid increment operator (ie, ++).
6+
The specific Moore neighbourhood used here implements a "square" nine-point
7+
stencil.
8+
9+
#TODO: a reference to a good review paper or textbook would be better here.
10+
https://en.wikipedia.org/wiki/Stencil_code
11+
12+
@author Peter Drysdale, Felix Fung.
13+
*/
14+
15+
#ifndef NEUROFIELD_SRC_STENCIL_LEGACY_H
16+
#define NEUROFIELD_SRC_STENCIL_LEGACY_H
17+
18+
// C++ standard library headers
19+
#include <string> // std::string;
20+
#include <vector> // std::vector;
21+
22+
class StencilLegacy {
23+
StencilLegacy();
24+
StencilLegacy& operator=(const StencilLegacy&);
25+
StencilLegacy(const StencilLegacy&);
26+
protected:
27+
int nodes;
28+
int longside;
29+
int shortside;
30+
std::string boundary;
31+
double* m;
32+
33+
mutable int ptr;
34+
public:
35+
enum Moore {
36+
nw=-4, n, ne,
37+
w, c, e,
38+
sw, s, se
39+
};
40+
41+
StencilLegacy( int nodes, int longside, const std::string& boundary );
42+
virtual ~StencilLegacy(void);
43+
44+
const std::vector<double>& operator= ( const std::vector<double>& field );
45+
inline void operator++ (int) const; // increment Moore grid
46+
void set( int node ) const; // point to node
47+
int get(void) const; // get ptr
48+
49+
inline double operator() ( Moore moore=c ) const {
50+
int arr[9] = {ptr-longside-2-1, ptr-longside-2, ptr-longside-2+1,
51+
ptr-1, ptr, ptr+1,
52+
ptr+longside+2-1, ptr+longside+2, ptr+longside+2+1};
53+
return m[arr[moore+4]];
54+
}
55+
56+
inline operator double (void) const {
57+
return (*this)(c);
58+
}
59+
};
60+
61+
62+
void StencilLegacy::operator++ (int) const {
63+
ptr++;
64+
if( ( ptr%(longside+2)==longside+1 ) ) {
65+
// Move stencil from far side to near side
66+
ptr += 2;
67+
}
68+
if( ptr == (longside+1)*(shortside+2)+1 ) {
69+
// Move stencil from very end of the array to the
70+
set(0);
71+
}
72+
}
73+
74+
#endif //NEUROFIELD_SRC_STENCIL_LEGACY_H

0 commit comments

Comments
 (0)