-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_1d_tensor.cc
141 lines (120 loc) · 3.51 KB
/
test_1d_tensor.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
#include <cmath>
#include <cstdio>
#include <iostream>
#include <chrono>
#include <functional>
#include "lanczos.h"
#include "wstTensor.h"
#include "wstKernel.h"
#include "wstUtils.h"
#define PI 3.141592653589793238
inline
double v1(double alpha, double L, double x)
{
return -alpha*(cos(2.0*PI*x/L) + 1.0);
}
inline
double v2(double L, double x)
{
return cos(2.0*PI*x/L);
}
inline
double v2r(double L, double x)
{
return -(4.0*PI*PI/L/L)*cos(2.0*PI*x/L);
}
bool test_7_pts_lap_1d()
{
const double L = 5.0;
const int NPTS = 150;
vector<double> x = wstUtils::linspace(-L/2, L/2, NPTS);
double hx = std::abs(x[1]-x[0]);
wstTensorT<double> V;
V.create(std::bind(v2, L, std::placeholders::_1), x, NPTS, true);
wstTensorT<double> rho;
rho.create(std::bind(v2r, L, std::placeholders::_1), x, NPTS, true);
wstKernel1D<double> kernel = create_laplacian_7p_1d(hx);
wstTensorT<double> rho2 = kernel.apply(V);
wstTensorT<double> errorT = rho-rho2;
double error = norm2(rho-rho2)*L/NPTS;
return (error < 1.e-10);
}
bool test_5_pts_lap_1d()
{
const double L = 5.0;
const int NPTS = 150;
vector<double> x = wstUtils::linspace(-L/2, L/2, NPTS);
double hx = std::abs(x[1]-x[0]);
wstTensorT<double> V;
V.create(std::bind(v2, L, std::placeholders::_1), x, NPTS, true);
wstTensorT<double> rho;
rho.create(std::bind(v2r, L, std::placeholders::_1), x, NPTS, true);
wstKernel1D<double> kernel = create_laplacian_5p_1d(hx);
wstTensorT<double> rho2 = kernel.apply(V);
wstTensorT<double> errorT = rho-rho2;
double error = norm2(rho-rho2)*L/NPTS;
return (error < 1.e-7);
}
bool test_3_pts_lap_1d()
{
const double L = 5.0;
const int NPTS = 150;
vector<double> x = wstUtils::linspace(-L/2, L/2, NPTS);
double hx = std::abs(x[1]-x[0]);
wstTensorT<double> V;
V.create(std::bind(v2, L, std::placeholders::_1), x, NPTS, true);
wstTensorT<double> rho;
rho.create(std::bind(v2r, L, std::placeholders::_1), x, NPTS, true);
wstKernel1D<double> kernel = create_laplacian_3p_1d(hx);
wstTensorT<double> rho2 = kernel.apply(V);
wstTensorT<double> errorT = rho-rho2;
double error = norm2(rho-rho2)*L/NPTS;
return (error < 1.e-4);
}
//bool lanczos_test1_1d()
//{
// const double L = 5.0;
// const NPTS = 50;
// const alpha = 2.5;
//
// vector<double> x = wstUtils::linspace(-L/2, L/2, NPTS);
// double hx = std::abs(x[1]-x[0]);
//
// wstTensorT<double> 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<wstTensorT<double>,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;
//}
int main(int argc, char** argv)
{
bool testResult = false;
testResult = test_3_pts_lap_1d();
if (testResult)
printf("test_3_pts_lap_1d -- PASSED\n");
else
printf("test_3_pts_lap_1d -- FAILED\n");
testResult = test_5_pts_lap_1d();
if (testResult)
printf("test_5_pts_lap_1d -- PASSED\n");
else
printf("test_5_pts_lap_1d -- FAILED\n");
testResult = test_7_pts_lap_1d();
if (testResult)
printf("test_7_pts_lap_1d -- PASSED\n");
else
printf("test_7_pts_lap_1d -- FAILED\n");
return 0;
}