-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptive.cpp
More file actions
428 lines (382 loc) · 17 KB
/
adaptive.cpp
File metadata and controls
428 lines (382 loc) · 17 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <stdlib.h>
#include <boost/program_options.hpp>
#include <chrono>
#include <limits>
#include "../space/initial_triangulation.hpp"
#include "../time/basis.hpp"
#include "../tools/util.hpp"
#include "adaptive_heat_equation.hpp"
#include "problems.hpp"
using applications::AdaptiveHeatEquation;
using datastructures::DoubleTreeView;
using space::HierarchicalBasisFn;
using Time::OrthonormalWaveletFn;
using Time::ThreePointWaveletFn;
using namespace applications;
namespace po = boost::program_options;
namespace applications {
std::istream& operator>>(std::istream& in,
HeatEquationOptions::SpaceInverse& inverse_type) {
std::string token;
in >> token;
if (token == "DirectInverse" || token == "di")
inverse_type = HeatEquationOptions::SpaceInverse::DirectInverse;
else if (token == "Multigrid" || token == "mg")
inverse_type = HeatEquationOptions::SpaceInverse::Multigrid;
else
in.setstate(std::ios_base::failbit);
return in;
}
void PrintTimeSliceSS(double t, AdaptiveHeatEquation::TypeXVector* solution) {
auto time_slice = spacetime::Trace(t, *solution);
// Calculate the triangulation corresponding to this space mesh.
space::TriangulationView triang(time_slice.Bfs());
std::cerr << "triang{";
for (auto [elem, vertices] : triang.element_leaves())
std::cerr << "(" << vertices[0] << ", " << vertices[1] << ", "
<< vertices[2] << ");";
std::cerr << "}\t";
// Calculate the single scale representation
space::MassOperator op(triang);
Eigen::VectorXd u_SS = time_slice.ToVector();
assert(op.FeasibleVector(u_SS));
op.ApplyHierarchToSingle(u_SS);
assert(op.FeasibleVector(u_SS));
// Print the data in single scale.
time_slice.FromVector(u_SS);
std::cerr << "vertices{";
for (auto nv : time_slice.Bfs())
std::cerr << "(" << nv->node()->center().first << ","
<< nv->node()->center().second << ") : " << nv->value() << ";";
std::cerr << "}";
}
// Compile time constants.
constexpr size_t N_t = 20;
constexpr size_t N_x = 197;
constexpr size_t N_y = 199;
std::vector<std::tuple<float, float, float, double>> PrintSampling(
AdaptiveHeatEquation::TypeXVector* solution) {
int cnt[N_t + 1][N_x + 1][N_y + 1] = {0};
double h_t = 1.0 / N_t;
double h_x = 1.0 / N_x;
double h_y = 1.0 / N_y;
for (auto dblnode : solution->Bfs())
for (int t = 0; t <= N_t; t++) {
if (dblnode->node_0()->Eval(t * h_t) == 0) continue;
for (int x = 0; x <= N_x; x++) {
if (!dblnode->node_1()->Contains(x * h_x,
dblnode->node_1()->center().second))
continue;
for (int y = 0; y <= N_y; y++) {
if (dblnode->node_1()->Eval(x * h_x, y * h_y) == 0) continue;
cnt[t][x][y]++;
}
}
}
std::vector<std::tuple<float, float, float, double>> result;
for (int t = 0; t <= N_t; t++)
for (int x = 0; x <= N_x; x++)
for (int y = 0; y <= N_y; y++) {
result.emplace_back(t * h_t, x * h_x, y * h_y, cnt[t][x][y]);
}
return result;
}
space::InitialTriangulation InitialTriangulation(std::string domain,
size_t initial_refines) {
if (domain == "square" || domain == "unit-square")
return space::InitialTriangulation::UnitSquare(initial_refines);
else if (domain == "lshape" || domain == "l-shape")
return space::InitialTriangulation::LShape(initial_refines);
else if (domain == "pacman")
return space::InitialTriangulation::Pacman(initial_refines);
else {
std::cout << "domain not recognized :-(" << std::endl;
exit(1);
}
}
} // namespace applications
int main(int argc, char* argv[]) {
std::string problem, domain;
size_t initial_refines = 0;
size_t max_dofs = 0;
size_t num_threads = 1;
bool calculate_condition_numbers = false;
bool print_centers = false;
bool print_sampling = false;
bool print_time_apply = true;
bool print_bilforms = false;
std::vector<double> print_time_slices;
boost::program_options::options_description problem_optdesc(
"Problem options");
problem_optdesc.add_options()(
"problem", po::value<std::string>(&problem)->default_value("singular"))(
"domain", po::value<std::string>(&domain)->default_value("square"))(
"initial_refines", po::value<size_t>(&initial_refines))(
"max_dofs", po::value<size_t>(&max_dofs)->default_value(
std::numeric_limits<std::size_t>::max()))(
"calculate_condition_numbers",
po::value<bool>(&calculate_condition_numbers))(
"print_centers", po::value<bool>(&print_centers))(
"print_sampling", po::value<bool>(&print_sampling))(
"print_time_slices",
po::value<std::vector<double>>(&print_time_slices)->multitoken())(
"print_time_apply", po::value<bool>(&print_time_apply))(
"print_bilforms", po::value<bool>(&print_bilforms))(
"num_threads", po::value<size_t>(&num_threads));
std::sort(print_time_slices.begin(), print_time_slices.end());
AdaptiveHeatEquationOptions adapt_opts;
boost::program_options::options_description adapt_optdesc(
"AdaptiveHeatEquation options");
adapt_optdesc.add_options()("use_cache",
po::value<bool>(&adapt_opts.use_cache))(
"build_space_mats", po::value<bool>(&adapt_opts.build_space_mats))(
"solve_factor", po::value<double>(&adapt_opts.solve_factor))(
"solve_xi", po::value<double>(&adapt_opts.solve_xi))(
"solve_maxit", po::value<size_t>(&adapt_opts.solve_maxit))(
"estimate_saturation_layers",
po::value<size_t>(&adapt_opts.estimate_saturation_layers))(
"estimate_mean_zero", po::value<bool>(&adapt_opts.estimate_mean_zero))(
"mark_theta", po::value<double>(&adapt_opts.mark_theta))(
"PX_alpha", po::value<double>(&adapt_opts.PX_alpha))(
"PX_inv",
po::value<HeatEquationOptions::SpaceInverse>(&adapt_opts.PX_inv))(
"PY_inv",
po::value<HeatEquationOptions::SpaceInverse>(&adapt_opts.PY_inv))(
"PXY_mg_build", po::value<bool>(&adapt_opts.PXY_mg_build))(
"PX_mg_cycles", po::value<size_t>(&adapt_opts.PX_mg_cycles))(
"PY_mg_cycles", po::value<size_t>(&adapt_opts.PY_mg_cycles));
boost::program_options::options_description cmdline_options;
cmdline_options.add(problem_optdesc).add(adapt_optdesc);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(cmdline_options).run(),
vm);
po::notify(vm);
assert(num_threads > 0);
if (num_threads > 1 && adapt_opts.use_cache) {
std::cout << "Multithreading is only enabled for no-cache." << std::endl;
return 1;
}
assert(num_threads <= omp_get_max_threads());
assert(num_threads <= MAX_NUMBER_THREADS);
omp_set_num_threads(num_threads);
std::cout << "Problem options:" << std::endl;
std::cout << "\tProblem: " << problem << std::endl;
std::cout << "\tDomain: " << domain
<< "; initial-refines: " << initial_refines << std::endl;
std::cout << "\tNumber-threads: " << num_threads << std::endl;
std::cout << std::endl;
std::cout << adapt_opts << std::endl;
auto T = InitialTriangulation(domain, initial_refines);
auto B = Time::Bases();
T.hierarch_basis_tree.UniformRefine(1);
B.ortho_tree.UniformRefine(1);
B.three_point_tree.UniformRefine(1);
auto vec_Xd = std::make_shared<
DoubleTreeVector<ThreePointWaveletFn, HierarchicalBasisFn>>(
B.three_point_tree.meta_root(), T.hierarch_basis_tree.meta_root());
vec_Xd->SparseRefine(1);
std::pair<std::unique_ptr<LinearFormBase<Time::OrthonormalWaveletFn>>,
std::unique_ptr<LinearFormBase<Time::ThreePointWaveletFn>>>
problem_data;
if (problem == "smooth")
problem_data = SmoothProblem();
else if (problem == "singular")
problem_data = SingularProblem();
else if (problem == "cylinder")
problem_data = CylinderProblem();
else if (problem == "moving-peak")
problem_data = MovingPeakProblem(vec_Xd);
else {
std::cout << "problem not recognized :-(" << std::endl;
return 1;
}
AdaptiveHeatEquation heat_eq(vec_Xd, std::move(problem_data.first),
std::move(problem_data.second), adapt_opts);
size_t ndof_Xd = 0;
Eigen::VectorXd x0 = Eigen::VectorXd::Zero(vec_Xd->container().size());
double t_delta = heat_eq.Estimate(x0).second.second.error;
std::cout << "t_init: " << t_delta << std::endl;
size_t iter = 0;
auto start_algorithm = std::chrono::steady_clock::now();
while (ndof_Xd < max_dofs) {
// Store a vector of all the nodes having maximum gradedness;
std::vector<typename HeatEquation::TypeXVector::DNType*> max_gradedness;
// A slight overestimate.
ndof_Xd = vec_Xd->Bfs().size();
size_t ndof_Xd_time = vec_Xd->Project_0()->Bfs().size();
size_t ndof_Xd_space = vec_Xd->Project_1()->Bfs().size();
size_t ndof_Xdd = heat_eq.vec_Xdd()->Bfs().size();
size_t ndof_Ydd = heat_eq.vec_Ydd()->Bfs().size();
std::cout << "iter: " << ++iter << "\n\tXDelta-size: " << ndof_Xd
<< "\n\tXDelta-space-size: " << ndof_Xd_space
<< "\n\tXDelta-time-size: " << ndof_Xd_time
<< "\n\tXDelta-Gradedness: "
<< vec_Xd->Gradedness(&max_gradedness)
<< "\n\tXDeltaDelta-size: " << ndof_Xdd
<< "\n\tYDeltaDelta-size: " << ndof_Ydd
<< "\n\ttotal-memory-kB: " << getmem() << std::flush;
if (print_sampling) {
auto sampling = PrintSampling(vec_Xd.get());
std::cout << "\n\tsampling: ";
for (auto [t, x, y, val] : sampling)
std::cout << "" << t << "," << x << "," << y << "," << val << ";";
std::cout << std::endl;
}
if (calculate_condition_numbers) {
auto start = std::chrono::steady_clock::now();
std::chrono::duration<double> duration_cond =
std::chrono::steady_clock::now() - start;
// Set the initial vector to something valid.
heat_eq.vec_Ydd()->Reset();
for (auto nv : heat_eq.vec_Ydd()->Bfs())
if (!nv->node_1()->on_domain_boundary()) nv->set_random();
auto lanczos_Y = tools::linalg::Lanczos(
*heat_eq.heat_d_dd()->A(), *heat_eq.heat_d_dd()->P_Y(),
heat_eq.vec_Ydd()->ToVectorContainer());
// Set the initial vector to something valid.
heat_eq.vec_Xd()->Reset();
for (auto nv : heat_eq.vec_Xd()->Bfs())
if (!nv->node_1()->on_domain_boundary()) nv->set_random();
auto lanczos_X = tools::linalg::Lanczos(
*heat_eq.heat_d_dd()->S(), *heat_eq.heat_d_dd()->P_X(),
heat_eq.vec_Xd()->ToVectorContainer());
std::cout << "\n\tlmin-PY-A: " << lanczos_Y.min()
<< "\n\tlmax-PY-A: " << lanczos_Y.max()
<< "\n\tlmin-PX-S: " << lanczos_X.min()
<< "\n\tlmax-PX-S: " << lanczos_X.max()
<< "\n\tcond-time: " << duration_cond.count() << std::flush;
}
Eigen::VectorXd solution = x0;
double total_error;
AdaptiveHeatEquation::TypeXVector* residual;
int cycle = 1;
auto start = std::chrono::steady_clock::now();
auto rhs = heat_eq.RHS();
std::chrono::duration<double> duration_rhs =
std::chrono::steady_clock::now() - start;
std::cout << "\n\trhs-time: " << duration_rhs.count();
std::cout << "\n\trhs-g-linform-time: "
<< heat_eq.g_lin_form()->TimeLastApply();
std::cout << "\n\trhs-u0-linform-time: "
<< heat_eq.u0_lin_form()->TimeLastApply();
auto start_solve_estimate = std::chrono::steady_clock::now();
do {
t_delta /= adapt_opts.solve_factor;
std::cout << "\n\tcycle: " << cycle << "\n\t\tt_delta: " << t_delta;
// Solve.
start = std::chrono::steady_clock::now();
auto [cur_solution, pcg_data] = heat_eq.Solve(solution, rhs, t_delta);
solution = cur_solution;
t_delta = pcg_data.algebraic_error;
std::chrono::duration<double> duration_solve =
std::chrono::steady_clock::now() - start;
std::cout << "\n\t\tsolve-PCG-steps: " << pcg_data.iterations
<< "\n\t\tsolve-PCG-initial-algebraic-error: "
<< pcg_data.initial_algebraic_error
<< "\n\t\tsolve-PCG-algebraic-error: "
<< pcg_data.algebraic_error
<< "\n\t\tsolve-time: " << duration_solve.count()
<< "\n\t\tsolve-memory: " << getmem() << std::flush;
// Estimate.
start = std::chrono::steady_clock::now();
auto [residual, global_errors] = heat_eq.Estimate(solution);
auto [residual_norm, global_error] = global_errors;
total_error = global_error.error;
std::chrono::duration<double> duration_estimate =
std::chrono::steady_clock::now() - start;
std::cout << "\n\t\tresidual-norm: " << residual_norm
<< "\n\t\testimate-time: " << duration_estimate.count()
<< "\n\t\testimate-memory: " << getmem() << std::flush;
std::cout << "\n\t\tglobal-error: " << total_error
<< "\n\t\tYnorm-error: " << global_error.error_Yprime
<< "\n\t\tT0-error: " << global_error.error_t0 << std::flush;
cycle++;
} while (t_delta > adapt_opts.solve_xi * total_error);
t_delta = total_error;
std::chrono::duration<double> duration_solve_estimate =
std::chrono::steady_clock::now() - start_solve_estimate;
std::cout << "\n\tsolve-estimate-time: " << duration_solve_estimate.count();
if (print_time_apply) {
auto heat_d_dd = heat_eq.heat_d_dd();
std::cout
<< "\n\tA-time-per-apply: " << heat_d_dd->A()->TimePerApply()
<< "\n\tB-time-per-apply: " << heat_d_dd->B()->TimePerApply()
<< "\n\tB-A-time-per-apply: " << heat_d_dd->B()->A()->TimePerApply()
<< "\n\tB-B-time-per-apply: " << heat_d_dd->B()->B()->TimePerApply()
<< "\n\tBT-time-per-apply: " << heat_d_dd->BT()->TimePerApply()
<< "\n\tG-time-per-apply: " << heat_d_dd->G()->TimePerApply()
<< "\n\tP_Y-time-per-apply: " << heat_d_dd->P_Y()->TimePerApply()
<< "\n\tP_X-time-per-apply: " << heat_d_dd->P_X()->TimePerApply()
<< "\n\tS-time-per-apply: " << heat_d_dd->S()->TimePerApply()
<< "\n\ttotal-time-apply: " << heat_d_dd->TotalTimeApply()
<< "\n\ttotal-time-construct: " << heat_d_dd->TotalTimeConstruct()
<< std::flush;
}
if (print_bilforms) {
auto heat_d_dd = heat_eq.heat_d_dd();
std::cout << "\n\tB-A-bilforms: " << heat_d_dd->B()->A()->Information()
<< "\n\tP_Y-bilforms: " << heat_d_dd->P_Y()->Information()
<< std::flush;
}
if (print_centers) {
vec_Xd->FromVectorContainer(solution);
auto print_dblnode = [](auto dblnode) {
std::cout << "((" << dblnode->node_0()->level() << ","
<< dblnode->node_0()->center() << "),"
<< "(" << dblnode->node_1()->level() << ",("
<< dblnode->node_1()->center().first << ","
<< dblnode->node_1()->center().second
<< ")) : " << dblnode->value() << ";";
};
std::cout << "\n\tcenters: ";
for (auto dblnode : vec_Xd->Bfs()) print_dblnode(dblnode);
std::cout << "\n\tcenters-max-gradedness: ";
for (auto dblnode : max_gradedness) print_dblnode(dblnode);
}
if (print_time_slices.size()) {
vec_Xd->FromVectorContainer(solution);
for (double t : print_time_slices) {
assert(t >= 0 && t <= 1);
std::cerr << "time_slice " << t << " = ";
PrintTimeSliceSS(t, vec_Xd.get());
std::cerr << std::endl;
}
std::cerr << std::endl;
}
#ifdef VERBOSE
std::cerr << std::endl << "Adaptive::Trees" << std::endl;
std::cerr << " T.vertex: #bfs = " << T.vertex_tree.Bfs().size()
<< std::endl;
std::cerr << " T.element: #bfs = " << T.elem_tree.Bfs().size()
<< std::endl;
std::cerr << " T.hierarch: #bfs = " << T.hierarch_basis_tree.Bfs().size()
<< std::endl;
std::cerr << std::endl;
std::cerr << " B.elem: #bfs = " << B.elem_tree.Bfs().size()
<< std::endl;
std::cerr << " B.three_pt: #bfs = " << B.three_point_tree.Bfs().size()
<< std::endl;
std::cerr << " B.ortho: #bfs = " << B.ortho_tree.Bfs().size()
<< std::endl;
#endif
// Mark - Refine.
auto marked_nodes = heat_eq.Mark(residual);
start = std::chrono::steady_clock::now();
vec_Xd->FromVectorContainer(solution);
auto r_info = heat_eq.Refine(marked_nodes);
x0 = vec_Xd->ToVectorContainer();
std::chrono::duration<double> duration_refine =
std::chrono::steady_clock::now() - start;
std::cout << "\n\tnodes-marked: " << r_info.nodes_marked
<< "\n\tnodes-conforming: " << r_info.nodes_conforming
<< "\n\tresidual-norm-marked: " << r_info.res_norm_marked
<< "\n\tresidual-norm-conforming: " << r_info.res_norm_conforming
<< "\n\trefine-time: " << duration_refine.count()
<< "\n\ttotal-time-algorithm: "
<< std::chrono::duration<double>(
std::chrono::steady_clock::now() - start_algorithm)
.count()
<< std::endl;
}
return 0;
}