-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallen-cahn.cpp
149 lines (116 loc) · 3.13 KB
/
allen-cahn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// allen-cahn.cpp
// Algorithms for 2D and 3D Allen-Cahn model
// Questions/comments to [email protected] (Jason Gruber)
#ifndef ALLENCAHN_UPDATE
#define ALLENCAHN_UPDATE
#include <cmath>
#include <mpi.h>
#include "MMSP.hpp"
#include "allen-cahn.hpp"
#include "manufactured.h"
const double runtime = 8.0;
const double A1 = 0.0075;
const double B1 = 8.0 * M_PI;
const double A2 = 0.03;
const double B2 = 22.0 * M_PI;
using std::cos;
using std::sin;
using std::tanh;
namespace MMSP
{
template<int dim, typename T>
double analyze(grid<dim, T>& Grid, const double elapsed, const double kappa, const double C2)
{
int rank = 0;
#ifdef MPI_VERSION
rank = MPI::COMM_WORLD.Get_rank();
#endif
double l2 = 0.0;
const double dV = dx(Grid, 0) * dx(Grid, 1);
for (int n = 0; n < nodes(Grid); n++) {
vector<int> r = position(Grid, n);
double x = dx(Grid, 0) * r[0];
double y = dx(Grid, 1) * r[1];
const double dEta = eta(A1, A2, B1, B2, C2, kappa, elapsed, x, y) - Grid(n);
l2 += dEta * dEta * dV;
}
#ifdef MPI_VERSION
double myL2(l2);
MPI::COMM_WORLD.Allreduce(&l2, &myL2, 1, MPI_DOUBLE, MPI_SUM);
#endif
return std::sqrt(l2);
}
template<int dim, typename T>
void bc(grid<dim, T>& Grid)
{
vector<int> r(dim, 0);
if (x0(Grid, 1) == g0(Grid, 1)) {
// lower boundary (y = 0)
r[1] = x0(Grid, 1) - 1;
for (r[0] = x0(Grid, 0); r[0] < x1(Grid, 0); r[0]++)
Grid(r) = 1.0;
}
if (x1(Grid, 1) == g1(Grid, 1)) {
// upper boundary (y = ½)
r[1] = x1(Grid, 1);
for (r[0] = x0(Grid, 0); r[0] < x1(Grid, 0); r[0]++)
Grid(r) = 0.0;
}
}
void generate(int dim, const char* filename, const int L, const double kappa, const double C2)
{
int rank = 0;
#ifdef MPI_VERSION
rank = MPI::COMM_WORLD.Get_rank();
#endif
if (dim == 2) {
GRID2D initGrid(0, 0, 2 * L, 0, L);
for (int d = 0; d < dim; d++)
dx(initGrid, d) = 1.0 / (2 * L);
vector<int> r(2, 0);
for (int n = 0; n < nodes(initGrid); n++) {
r = position(initGrid, n);
double x = dx(initGrid, 0) * r[0];
double y = dx(initGrid, 1) * r[1];
double t = 0.0;
initGrid(n) = eta0(A2, B2, kappa, x, y);
}
bc(initGrid);
output(initGrid, filename);
} else {
std::cerr << "Error: PFHub Benchmark 7 is 2-D, only." << std::endl;
}
}
template <int dim, typename T>
double update(grid<dim, T>& Grid, const unsigned steps, const double dt, const double kappa, const double C2)
{
int rank = 0;
#ifdef MPI_VERSION
rank = MPI::COMM_WORLD.Get_rank();
#endif
ghostswap(Grid);
const double LinStab = dx(Grid, 0) * dx(Grid, 1) / kappa;
assert(LinStab < 0.2);
grid<dim, T> newGrid(Grid);
double elapsed = 0.0;
for (int step = 0; step < steps; step++) {
if (rank == 0)
print_progress(step, steps);
for (int n = 0; n < nodes(Grid); n++) {
vector<int> r = position(Grid, n);
double x = dx(Grid, 0) * r[0];
double y = dx(Grid, 1) * r[1];
T phi = Grid(n);
newGrid(n) = phi + dt * (- 4 * phi * (phi - 1) * (phi - 0.5) + kappa * laplacian(Grid, n)
+ S(A1, A2, B1, B2, C2, kappa, elapsed, x, y));
}
swap(Grid, newGrid);
ghostswap(Grid);
bc(Grid);
elapsed += dt;
}
return elapsed;
}
} // namespace MMSP
#endif
#include"main.cpp"