NiHu  2.0
laplace_2d_transparent_standalone.cpp
1 #include <boost/math/constants/constants.hpp>
2 
4 #include "library/laplace_2d.hpp"
5 #include "../library/lib_element.hpp"
6 
7 #include <iostream>
8 
9 typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> dMatrix;
10 typedef Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic> uMatrix;
11 
14 {
15  using namespace boost::math::double_constants;
16 
17  dMatrix nodes(N,2);
18  uMatrix elements(N, 3);
19  for (int i = 0; i < N; ++i)
20  {
21  double phi = i*two_pi/N;
22  nodes(i,0) = r * cos(phi);
23  nodes(i,1) = r * sin(phi);
24  elements(i,0) = NiHu::line_1_elem::id;
25  elements(i,1) = i % N;
26  elements(i,2) = (i+1) % N;
27  }
28  return NiHu::create_mesh(nodes, elements, NiHu::line_1_tag());
29 }
30 
31 template <class TestSpace, class TrialSpace>
32 static void tester(TestSpace const &test_space, TrialSpace const &trial_space)
33 {
34  using namespace boost::math::double_constants;
35 
36  // instantiate integral operators
42 
43  size_t N = test_space.get_num_dofs();
44 
45  // excitation and response
46  dMatrix q0(N, 1), p0(N,1);
47  p0.setZero();
48  q0.setZero();
49 
50  dMatrix x0(2, 2);
51  x0 <<
52  .2, .3,
53  .3, .2;
54  dMatrix A(1, 2);
55  A << 1.0, -1.0;
56 
57  auto const &mesh = trial_space.get_mesh();
58  for (unsigned k = 0; k < N; ++k)
59  {
60  auto const &elem = mesh.template get_elem<NiHu::line_1_elem>(k);
61  auto y = elem.get_center();
62  auto ny = elem.get_normal().normalized();
63  for (int s = 0; s < x0.cols(); ++s)
64  {
65  auto rvec = y - x0.col(s);
66  double r = rvec.norm();
67  double rdn = rvec.dot(ny) / r;
68  p0(k) += A(s) * -std::log(r) / two_pi;
69  q0(k) += A(s) * -(1.0/r) * rdn / two_pi;
70  }
71  }
72 
73  std::cout << "Matrix generation" << std::endl;
74 
75  dMatrix G(N, N), H(N, N), Ht(N, N), D(N, N), I(N,N);
76  G.setZero();
77  H.setZero();
78  Ht.setZero();
79  D.setZero();
80  I.setZero();
81 
82  I << test_space * I_op[trial_space];
83  G << test_space * G_op[trial_space];
84  H << test_space * H_op[trial_space];
85  Ht << test_space * Ht_op[trial_space];
86  D << test_space * D_op[trial_space];
87 
88  // solve with direct Neumann
89  std::cout << " Solution with direct Neumann:" << std::endl;
90  dMatrix p_direct_neumann = (H - .5 * I).colPivHouseholderQr().solve(G * q0);
91  double error_direct_neumann = (p_direct_neumann-p0).norm() / p0.norm();
92  std::cout << "Direct Neumann Error: " << error_direct_neumann << std::endl;
93 
94  // solve with direct Dirichlet
95  std::cout << " Solution with direct Dirichlet:" << std::endl;
96  dMatrix q_direct_dirichlet = G.colPivHouseholderQr().solve((H - .5 * I) * p0);
97  double error_direct_dirichlet = (q_direct_dirichlet-q0).norm() / q0.norm();
98  std::cout << "Direct Dirichlet Error: " << error_direct_dirichlet << std::endl;
99 }
100 
101 int main()
102 {
103  int N = 40;
104  double r = 1.2;
105  auto mesh = create_mesh(r, N);
106  auto const &trial_space = NiHu::constant_view(mesh);
107 
108  std::cout << "Collocation" << std::endl;
109  tester(NiHu::dirac(trial_space), trial_space);
110 
111  std::cout << "Galerkin" << std::endl;
112  tester(trial_space, trial_space);
113 
114  return 0;
115 }
116 
NiHu::create_integral_operator
integral_operator< Kernel > create_integral_operator(Kernel &&kernel)
factory function of an integral operator
Definition: integral_operator.hpp:421
NiHu::create_mesh
mesh< tmp::vector< typename tag2type< Args >::type... > > create_mesh(nodes_t const &nodes, elements_t const &elements, Args...)
factory function to create a mesh from nodes and elements matrices
Definition: mesh.hpp:298
NiHu::bessel::H
make_complex< T >::type H(T const &z)
H^(K)_nu(z) Bessel function.
Definition: math_functions.hpp:365
NiHu::type2tag
Metafunction assigning a tag to a type.
Definition: type2tag.hpp:17
NiHu::mesh
container class for a mesh
Definition: mesh.hpp:110
NiHu::constant_view
const function_space_view< Mesh, field_option::constant, Dimension > & constant_view(mesh_base< Mesh > const &msh, Dimension dim=Dimension())
factory function to transform a mesh into a constant function space
Definition: function_space.hpp:304
weighted_residual.hpp
declaration of class NiHu::weighted_residual
NiHu::mex::real_matrix
Definition: mex_matrix_separate.hpp:133
NiHu::normal_derivative_kernel
Normal derivative of a distance dependent kernel.
Definition: normal_derivative_kernel.hpp:26
NiHu::identity_integral_operator
The identity integral operator .
Definition: integral_operator.hpp:310