-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cgpoisson_problem.cpp
235 lines (197 loc) · 7.95 KB
/
cgpoisson_problem.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
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
// Copyright (C) 2017-2019 Chris N. Richardson and Garth N. Wells
//
// This file is part of FEniCS-miniapp (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: MIT
#include "cgpoisson_problem.h"
#include "Poisson.h"
#include "cg.h"
#include <cfloat>
#include <cmath>
#include <dolfinx/common/Scatterer.h>
#include <dolfinx/common/Timer.h>
#include <dolfinx/fem/DirichletBC.h>
#include <dolfinx/fem/Function.h>
#include <dolfinx/fem/FunctionSpace.h>
#include <dolfinx/fem/assembler.h>
#include <dolfinx/fem/petsc.h>
#include <dolfinx/fem/utils.h>
#include <dolfinx/la/MatrixCSR.h>
#include <dolfinx/mesh/Mesh.h>
#include <dolfinx/mesh/utils.h>
#include <memory>
#include <petscsys.h>
#include <utility>
using namespace dolfinx;
using T = PetscScalar;
namespace
{
void pack_fn(std::span<const T> in, std::span<const std::int32_t> idx,
std::span<T> out)
{
for (std::size_t i = 0; i < idx.size(); ++i)
out[i] = in[idx[i]];
}
void unpack_fn(std::span<const T> in, std::span<const std::int32_t> idx,
std::span<T> out, std::function<T(T, T)> op)
{
for (std::size_t i = 0; i < idx.size(); ++i)
out[idx[i]] = op(out[idx[i]], in[i]);
}
} // namespace
std::tuple<std::shared_ptr<la::Vector<T>>, std::shared_ptr<fem::Function<T>>,
std::function<int(fem::Function<T>&, const la::Vector<T>&)>>
cgpoisson::problem(std::shared_ptr<mesh::Mesh<double>> mesh, int order,
std::string scatterer)
{
common::Timer t0("ZZZ FunctionSpace");
auto element = basix::create_element<double>(
basix::element::family::P, basix::cell::type::tetrahedron, order,
basix::element::lagrange_variant::gll_warped,
basix::element::dpc_variant::unset, false);
auto V = std::make_shared<fem::FunctionSpace<double>>(
fem::create_functionspace(mesh, element, {}));
t0.stop();
common::Timer t1("ZZZ Assemble");
common::Timer t2("ZZZ Create boundary conditions");
// Define boundary condition
auto u0 = std::make_shared<fem::Function<T>>(V);
u0->x()->set(0);
// Find facets with bc applied
const int tdim = mesh->topology()->dim();
const std::vector<std::int32_t> bc_facets = mesh::locate_entities(
*mesh, tdim - 1,
[](auto x)
{
constexpr double eps = 1.0e-8;
std::vector<std::int8_t> marker(x.extent(1), false);
for (std::size_t p = 0; p < x.extent(1); ++p)
{
double x0 = x(0, p);
if (std::abs(x0) < eps or std::abs(x0 - 1) < eps)
marker[p] = true;
}
return marker;
});
// Find constrained dofs
const std::vector<std::int32_t> bdofs = fem::locate_dofs_topological(
*V->mesh()->topology_mutable(), *V->dofmap(), tdim - 1, bc_facets);
auto bc = std::make_shared<fem::DirichletBC<T>>(u0, bdofs);
t2.stop();
// Define coefficients
common::Timer t3("ZZZ Create RHS function");
auto f = std::make_shared<fem::Function<T>>(V);
auto g = std::make_shared<fem::Function<T>>(V);
f->interpolate(
[](auto x) -> std::pair<std::vector<T>, std::vector<std::size_t>>
{
std::vector<T> v(x.extent(1));
for (std::size_t p = 0; p < x.extent(1); ++p)
{
double dx = x(0, p) - 0.5;
double dy = x(1, p) - 0.5;
double dr = dx * dx + dy * dy;
v[p] = 10 * std::exp(-dr / 0.02);
}
return {std::move(v), {v.size()}};
});
g->interpolate(
[](auto x) -> std::pair<std::vector<T>, std::vector<std::size_t>>
{
std::vector<T> f(x.extent(1));
for (std::size_t p = 0; p < x.extent(1); ++p)
f[p] = std::sin(5 * x(0, p));
return {f, {f.size()}};
});
t3.stop();
std::vector form_poisson_L
= {form_Poisson_L1, form_Poisson_L2, form_Poisson_L3};
std::vector form_poisson_a
= {form_Poisson_a1, form_Poisson_a2, form_Poisson_a3};
std::vector form_poisson_M
= {form_Poisson_M1, form_Poisson_M2, form_Poisson_M3};
// Define variational forms
auto L = std::make_shared<fem::Form<T>>(fem::create_form<T>(
*form_poisson_L.at(order - 1), {V}, {{"w0", f}, {"w1", g}}, {}, {}));
// auto a = std::make_shared<fem::Form<T>>(fem::create_form<T>(
// *form_poisson_a.at(order - 1), {V, V},
// std::vector<std::shared_ptr<const fem::Function<T>>>{}, {}, {}));
auto un = std::make_shared<fem::Function<T>>(V);
auto M = std::make_shared<fem::Form<T>>(fem::create_form<T>(
*form_poisson_M.at(order - 1), {V}, {{"w0", un}}, {{}}, {}));
// Create la::Vector
la::Vector<T> b(L->function_spaces()[0]->dofmap()->index_map,
L->function_spaces()[0]->dofmap()->index_map_bs());
b.set(0);
common::Timer t5("ZZZ Assemble vector");
const std::vector constants_L = fem::pack_constants(*L);
auto coeffs_L = fem::allocate_coefficient_storage(*L);
fem::pack_coefficients(*L, coeffs_L);
fem::assemble_vector<T>(b.mutable_array(), *L, constants_L,
fem::make_coefficients_span(coeffs_L));
// Apply lifting to account for Dirichlet boundary condition
// b <- b - A * x_bc
fem::set_bc<T, double>(un->x()->mutable_array(), {bc}, -1.0);
fem::assemble_vector(b.mutable_array(), *M);
// Communicate ghost values
b.scatter_rev(std::plus<T>());
// Set BC dofs to zero (effectively zeroes columns of A)
fem::set_bc<T, double>(b.mutable_array(), {bc}, 0.0);
b.scatter_fwd();
// Pack coefficients and constants
if (un->x()->array().size() != b.array().size())
throw std::runtime_error("error");
// Create Function to hold solution
auto u = std::make_shared<fem::Function<T>>(V);
std::function<int(fem::Function<T>&, const la::Vector<T>&)> solver_function
= [M, un, bc, scatterer](fem::Function<T>& u, const la::Vector<T>& b)
{
const std::vector<T> constants;
auto coeff = fem::allocate_coefficient_storage(*M);
auto V = M->function_spaces()[0];
auto idx_map = V->dofmap()->index_map;
int bs = V->dofmap()->bs();
common::Scatterer sct(*idx_map, bs);
std::vector<T> local_buffer(sct.local_buffer_size(), 0);
std::vector<T> remote_buffer(sct.remote_buffer_size(), 0);
common::Scatterer<>::type type;
if (scatterer == "neighbor")
type = common::Scatterer<>::type::neighbor;
if (scatterer == "p2p")
type = common::Scatterer<>::type::p2p;
std::vector<MPI_Request> request = sct.create_request_vector(type);
// Create function for computing the action of A on x (y = Ax)
auto action = [&](la::Vector<T>& x, la::Vector<T>& y)
{
// Zero y
y.set(0.0);
// Update coefficient un (just copy data from x to un)
std::copy(x.array().begin(), x.array().end(),
un->x()->mutable_array().begin());
// Compute action of A on x
fem::pack_coefficients(*M, coeff);
fem::assemble_vector(y.mutable_array(), *M, std::span<const T>(constants),
fem::make_coefficients_span(coeff));
// Set BC dofs to zero (effectively zeroes rows of A)
fem::set_bc<T, double>(y.mutable_array(), {bc}, 0.0);
// Accumuate ghost values
// y.scatter_rev(std::plus<T>());
const std::int32_t local_size = bs * idx_map->size_local();
const std::int32_t num_ghosts = bs * idx_map->num_ghosts();
std::span<T> remote_data(y.mutable_array().data() + local_size,
num_ghosts);
std::span<T> local_data(y.mutable_array().data(), local_size);
sct.scatter_rev_begin<T>(remote_data, remote_buffer, local_buffer,
pack_fn, request, type);
sct.scatter_rev_end<T>(local_buffer, local_data, unpack_fn,
std::plus<T>(), request);
// Update ghost values
sct.scatter_fwd_begin<T>(local_data, local_buffer, remote_buffer, pack_fn,
request, type);
sct.scatter_fwd_end<T>(remote_buffer, remote_data, unpack_fn, request);
};
int num_it = linalg::cg(*u.x(), b, action, 100, 1e-6);
return num_it;
};
return {std::make_shared<la::Vector<T>>(std::move(b)), u, solver_function};
}