| 1 |
// Copyright (C) 2017 August Johansson |
| 2 |
// |
| 3 |
// This file is part of DOLFIN. |
| 4 |
// |
| 5 |
// DOLFIN is free software: you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU Lesser General Public License as published by |
| 7 |
// the Free Software Foundation, either version 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// DOLFIN is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU Lesser General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// First added: 2013-08-24 |
| 19 |
// Last changed: 2017-08-30 |
| 20 |
// |
| 21 |
// This demo program solves Poisson's equation on a domain defined by |
| 22 |
// three overlapping and non-matching meshes. The solution is computed |
| 23 |
// on a sequence of rotating meshes to test the multimesh |
| 24 |
// functionality. |
| 25 |
|
| 26 |
#include <cmath> |
| 27 |
#include <fstream> |
| 28 |
#include <dolfin.h> |
| 29 |
#include "MultiMeshPoisson.h" |
| 30 |
#include "MultiMeshL2Norm.h" |
| 31 |
#include "MultiMeshH10Norm.h" |
| 32 |
|
| 33 |
using namespace dolfin; |
| 34 |
|
| 35 |
class Arguments |
| 36 |
{ |
| 37 |
public: |
| 38 |
std::size_t N = 1; |
| 39 |
std::size_t Nx = 10; |
| 40 |
|
| 41 |
std::string print() const |
| 42 |
{ |
| 43 |
std::stringstream ss; |
| 44 |
ss << "N" << N<<"_" |
| 45 |
<< "Nx" << Nx; |
| 46 |
return ss.str(); |
| 47 |
} |
| 48 |
|
| 49 |
void parse(int argc, char** argv) |
| 50 |
{ |
| 51 |
std::size_t c = 1; |
| 52 |
|
| 53 |
if (argc > c) |
| 54 |
{ |
| 55 |
this->N = atoi(argv[c]); |
| 56 |
c++; |
| 57 |
if (argc > c) |
| 58 |
this->Nx = atoi(argv[c]); |
| 59 |
} |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
// Sub domain for Dirichlet boundary condition |
| 65 |
class DirichletBoundary : public SubDomain |
| 66 |
{ |
| 67 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 68 |
{ |
| 69 |
return on_boundary; |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
class ExactSolution : public Expression |
| 74 |
{ |
| 75 |
void eval(Array<double>& values, const Array<double>& x) const |
| 76 |
{ |
| 77 |
values[0] = sin(DOLFIN_PI*x[0])*sin(DOLFIN_PI*x[1])*sin(DOLFIN_PI*x[2]); |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
class Source : public Expression |
| 82 |
{ |
| 83 |
void eval(Array<double>& values, const Array<double>& x) const |
| 84 |
{ |
| 85 |
values[0] = 3.0*DOLFIN_PI*DOLFIN_PI*sin(DOLFIN_PI*x[0])*sin(DOLFIN_PI*x[1])*sin(DOLFIN_PI*x[2]); |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
void build_multimesh(std::size_t N, |
| 90 |
std::size_t Nx, |
| 91 |
std::shared_ptr<MultiMesh> multimesh, |
| 92 |
double& exact_volume) |
| 93 |
{ |
| 94 |
// Background mesh |
| 95 |
auto mesh_0 = std::make_shared<UnitCubeMesh>(Nx, Nx, Nx); |
| 96 |
multimesh->add(mesh_0); |
| 97 |
exact_volume = 1.; |
| 98 |
|
| 99 |
// Initialize random generator (dolfin built-in) |
| 100 |
dolfin::seed(1); |
| 101 |
|
| 102 |
for (std::size_t i = 1; i < N; ++i) |
| 103 |
{ |
| 104 |
// Create domain range |
| 105 |
double x_a = dolfin::rand(); |
| 106 |
double x_b = dolfin::rand(); |
| 107 |
|
| 108 |
if (x_a > x_b) |
| 109 |
std::swap(x_a, x_b); |
| 110 |
double y_a = dolfin::rand(); |
| 111 |
double y_b = dolfin::rand(); |
| 112 |
if (y_a > y_b) |
| 113 |
std::swap(y_a, y_b); |
| 114 |
|
| 115 |
double z_a = dolfin::rand(); |
| 116 |
double z_b = dolfin::rand(); |
| 117 |
if (z_a > z_b) |
| 118 |
std::swap(z_a, z_b); |
| 119 |
|
| 120 |
// std::cout << i << ": " << x_a<<' '<<y_a<<' ' << z_a << ", " << x_b<<' '<<y_b << ' ' << z_b <<std::endl; |
| 121 |
|
| 122 |
// Find number of elements |
| 123 |
const std::size_t Nx_part = (std::size_t)std::max(std::abs(x_a - x_b)*Nx, 1.); |
| 124 |
const std::size_t Ny_part = (std::size_t)std::max(std::abs(y_a - y_b)*Nx, 1.); |
| 125 |
const std::size_t Nz_part = (std::size_t)std::max(std::abs(z_a - z_b)*Nx, 1.); |
| 126 |
|
| 127 |
// Create mesh |
| 128 |
auto mesh_i = std::make_shared<BoxMesh>(Point(x_a, y_a, z_a), |
| 129 |
Point(x_b, y_b, z_b), |
| 130 |
Nx_part, Ny_part, Nz_part); |
| 131 |
multimesh->add(mesh_i); |
| 132 |
} |
| 133 |
|
| 134 |
// Build |
| 135 |
multimesh->build(); |
| 136 |
} |
| 137 |
|
| 138 |
template<class TFunctional> |
| 139 |
double compute_error(const std::shared_ptr<MultiMeshFunctionSpace> V, |
| 140 |
const std::shared_ptr<MultiMeshFunction> u_h, |
| 141 |
const std::shared_ptr<Expression> u) |
| 142 |
{ |
| 143 |
std::cout << "Compute error" << std::endl; |
| 144 |
|
| 145 |
auto M = std::make_shared<MultiMeshForm>(V); |
| 146 |
|
| 147 |
for (std::size_t i = 0; i < V->multimesh()->num_parts(); ++i) |
| 148 |
{ |
| 149 |
auto M_i = std::make_shared<TFunctional>(V->multimesh()->part(i)); |
| 150 |
const bool deepcopy = true; |
| 151 |
M_i->uh = std::make_shared<const Function>(*u_h->part(i, deepcopy)); |
| 152 |
M_i->u = u; |
| 153 |
M->add(M_i); |
| 154 |
} |
| 155 |
|
| 156 |
M->build(); |
| 157 |
|
| 158 |
auto assembler = std::make_shared<MultiMeshAssembler>(); |
| 159 |
auto m = std::make_shared<Scalar>(); |
| 160 |
assembler->assemble(*m, *M); |
| 161 |
|
| 162 |
dolfin_assert(m->get_scalar_value() > 0.); |
| 163 |
return std::sqrt(m->get_scalar_value()); |
| 164 |
} |
| 165 |
|
| 166 |
void solve(const std::shared_ptr<MultiMesh> multimesh) |
| 167 |
{ |
| 168 |
// Create function space |
| 169 |
auto V = std::make_shared<MultiMeshPoisson::MultiMeshFunctionSpace>(multimesh); |
| 170 |
|
| 171 |
// Create forms |
| 172 |
auto a = std::make_shared<MultiMeshPoisson::MultiMeshBilinearForm>(V, V); |
| 173 |
auto L = std::make_shared<MultiMeshPoisson::MultiMeshLinearForm>(V); |
| 174 |
|
| 175 |
// Attach source |
| 176 |
auto f = std::make_shared<Source>(); |
| 177 |
L->f = f; |
| 178 |
|
| 179 |
// Assemble linear system |
| 180 |
auto A = std::make_shared<Matrix>(); |
| 181 |
auto b = std::make_shared<Vector>(); |
| 182 |
assemble_multimesh(*A, *a); |
| 183 |
assemble_multimesh(*b, *L); |
| 184 |
|
| 185 |
// Apply boundary condition |
| 186 |
auto zero = std::make_shared<Constant>(0.0); |
| 187 |
auto boundary = std::make_shared<DirichletBoundary>(); |
| 188 |
auto bc = std::make_shared<MultiMeshDirichletBC>(V, zero, boundary); |
| 189 |
bc->apply(*A, *b); |
| 190 |
|
| 191 |
// Compute solution |
| 192 |
auto uh = std::make_shared<MultiMeshFunction>(V); |
| 193 |
std::cout << "Solve" << std::endl; |
| 194 |
solve(*A, *uh->vector(), *b, "cg"); |
| 195 |
|
| 196 |
// Compute errors |
| 197 |
auto exact_solution = std::make_shared<ExactSolution>(); |
| 198 |
const double L2error = compute_error<MultiMeshL2Norm::Functional>(V, uh, exact_solution); |
| 199 |
const double H10error = compute_error<MultiMeshH10Norm::Functional>(V, uh, exact_solution); |
| 200 |
std::cout << L2error << ' ' << H10error << std::endl; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
int main(int argc, char* argv[]) |
| 205 |
{ |
| 206 |
Arguments args; |
| 207 |
args.parse(argc, argv); |
| 208 |
|
| 209 |
auto multimesh = std::make_shared<MultiMesh>(); |
| 210 |
double exact_volume; |
| 211 |
build_multimesh(args.N, args.Nx, multimesh, exact_volume); |
| 212 |
|
| 213 |
const double volume = multimesh->compute_volume(); |
| 214 |
const double volume_error = std::abs(volume - exact_volume); |
| 215 |
dolfin::cout << "volume error " << volume_error << dolfin::endl; |
| 216 |
|
| 217 |
if (volume_error > DOLFIN_EPS_LARGE) |
| 218 |
{ |
| 219 |
dolfin::cout << " large error" << dolfin::endl; |
| 220 |
} |
| 221 |
|
| 222 |
solve(multimesh); |
| 223 |
|
| 224 |
} |