-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperiodic.cc
204 lines (166 loc) · 4.81 KB
/
periodic.cc
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <cmath>
#include <cstdio>
#include <iostream>
#include <chrono>
#include "lanczos.h"
#include "wstTensor.h"
#include "wstKernel.h"
#include "wstModel.h"
#include "wstUtils.h"
#define L 5.0
#define NPTS 50
#define alpha 2.5
#define PI 3.141592653589793238
inline
double v1(double x)
{
return -alpha*(cos(2.0*PI*x/L) + 1.0);
}
inline
double v1(double x, double y)
{
return -alpha*(cos(2.0*PI*x/L)*cos(2.0*PI*y/L) + 1.0);
}
inline
double v1(double x, double y, double z)
{
return -alpha*(cos(2.0*PI*x/L)*cos(2.0*PI*y/L)*cos(2.0*PI*z/L) + 1.0);
}
inline
double v2(double x)
{
return cos(2.0*PI*x/L);
}
inline
double v2(double x, double y)
{
return cos(2.0*PI*x/L)*cos(2.0*PI*y/L);
}
inline
double v2(double x, double y, double z)
{
return cos(2.0*PI*x/L)*cos(2.0*PI*y/L)*cos(2.0*PI*z/L);
}
inline
double v2r(double x)
{
return -(4.0*PI*PI/L/L)*cos(2.0*PI*x/L);
}
inline
double v2r(double x, double y)
{
return -(8.0*PI*PI/L/L)*cos(2.0*PI*x/L)*cos(2.0*PI*y/L);
}
inline
double v2r(double x, double y, double z)
{
return -(12.0*PI*PI/L/L)*cos(2.0*PI*x/L)*cos(2.0*PI*y/L)*cos(2.0*PI*z/L);
}
void test1_3d()
{
vector<double> x = wstUtils::linspace(-L/2, L/2, NPTS);
vector<double> y = wstUtils::linspace(-L/2, L/2, NPTS);
vector<double> z = wstUtils::linspace(-L/2, L/2, NPTS);
double hx = std::abs(x[1]-x[0]);
double hy = std::abs(y[1]-y[0]);
double hz = std::abs(z[1]-z[0]);
wstTensor V;
V.create(v2, x, y, z, NPTS, NPTS, NPTS, true, true, true);
wstTensor rho;
rho.create(v2r, x, y, z, NPTS, NPTS, NPTS, true, true, true);
wstKernel3D kernel = create_laplacian_7p_3d(hx, hy, hz);
const auto tstart = std::chrono::system_clock::now();
wstTensor rho2 = kernel.apply(V);
const auto tstop = std::chrono::system_clock::now();
const std::chrono::duration<double> time_elapsed = tstop - tstart;
wstTensor errorT = rho-rho2;
double error = (rho-rho2).norm2();
printf("Error is: %15.8e\n", error);
std::cout << "Elapsed time: " << time_elapsed.count() << std::endl;
}
void test1_2d()
{
vector<double> x = wstUtils::linspace(-L/2, L/2, NPTS);
vector<double> y = wstUtils::linspace(-L/2, L/2, NPTS);
double hx = std::abs(x[1]-x[0]);
double hy = std::abs(y[1]-y[0]);
wstTensor V;
V.create(v2, x, y, NPTS, NPTS, true, true);
wstTensor rho;
rho.create(v2r, x, y, NPTS, NPTS, true, true);
wstKernel2D kernel = create_laplacian_7p_2d(hx, hy);
wstTensor rho2 = kernel.apply(V);
wstTensor errorT = rho-rho2;
double error = (rho-rho2).norm2();
print(rho, rho2, errorT);
printf("Error is: %15.8e\n", error);
}
void test1_1d()
{
vector<double> x = wstUtils::linspace(-L/2, L/2, NPTS);
double hx = std::abs(x[1]-x[0]);
for (unsigned int i = 0; i < x.size(); i++) {
printf("%15.5f\n", x[i]);
}
printf("\n\n");
wstTensor V;
V.create(v2, x, NPTS, true);
wstTensor rho;
rho.create(v2r, x, NPTS, true);
wstKernel1D kernel = create_laplacian_7p_1d(hx, false);
wstTensor rho2 = kernel.apply(V);
wstTensor errorT = rho-rho2;
double error = (rho-rho2).norm2();
print(rho, rho2, errorT);
printf("Error is: %15.8e\n", error);
}
void lanczos_test1_1d()
{
vector<double> x = wstUtils::linspace(-L/2, L/2, NPTS);
double hx = std::abs(x[1]-x[0]);
wstTensor V;
V.create(v1, x, NPTS, true);
wstKernel1D kernel = create_laplacian_7p_1d(hx);
const auto tstart = std::chrono::system_clock::now();
//wstModel model(kernel, V);
//Lanczos<wstTensor,wstModel> lanczos(&model, 100);
//lanczos.run();
printf("creating lanzcos\n");
wstLanczos1D lanczos(V, hx, 40);
printf("created lanzcos\n");
lanczos.run();
printf("finished lanzcos\n");
const auto tstop = std::chrono::system_clock::now();
const std::chrono::duration<double> time_elapsed = tstop - tstart;
std::cout << "Elapsed time: " << time_elapsed.count() << std::endl;
}
void lanczos_test1_3d()
{
vector<double> x = wstUtils::linspace(-L/2, L/2, NPTS);
vector<double> y = wstUtils::linspace(-L/2, L/2, NPTS);
vector<double> z = wstUtils::linspace(-L/2, L/2, NPTS);
double hx = std::abs(x[1]-x[0]);
double hy = std::abs(y[1]-y[0]);
double hz = std::abs(z[1]-z[0]);
wstTensor V;
V.create(v1, x, y, z, NPTS, NPTS, NPTS, true, true, true);
wstKernel3D kernel = create_laplacian_7p_3d(hx, hy, hz);
const auto tstart = std::chrono::system_clock::now();
//wstModel model(kernel, V);
//Lanczos<wstTensor,wstModel> lanczos(&model, 100);
//lanczos.run();
printf("creating lanzcos\n");
wstLanczos3D lanczos(V, hx, hy, hz);
printf("created lanzcos\n");
lanczos.run();
printf("finished lanzcos\n");
const auto tstop = std::chrono::system_clock::now();
const std::chrono::duration<double> time_elapsed = tstop - tstart;
std::cout << "Elapsed time: " << time_elapsed.count() << std::endl;
}
int main(int argc, char** argv)
{
lanczos_test1_1d();
//test1_1d();
return 0;
}