Hermes2D  3.0
vector_base_view.cpp
1 // This file is part of Hermes2D.
2 //
3 // Hermes2D is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // Hermes2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Hermes2D. If not, see <http://www.gnu.org/licenses/>.
15 // $Id: view4.cpp 1086 2008-10-21 09:05:44Z jakub $
16 
17 #include "vector_base_view.h"
18 
19 #ifndef NOGLUT
20 
21 #include <GL/freeglut.h>
22 #include "global.h"
23 #include "space.h"
24 #include "precalc.h"
25 #include "filter.h"
26 
27 namespace Hermes
28 {
29  namespace Hermes2D
30  {
31  namespace Views
32  {
33  template<typename Scalar>
34  void VectorBaseView<Scalar>::show(SpaceSharedPtr<Scalar> space)
35  {
36  free();
37  pss = new PrecalcShapeset(space->shapeset);
38  sln = new Solution<Scalar>();
39  this->space = space;
40  ndof = space->get_num_dofs();
41  base_index = 0;
42  update_solution();
43  }
44 
45  template<typename Scalar>
46  VectorBaseView<Scalar>::VectorBaseView(const char* title, WinGeom* wg)
47  : VectorView(title, wg) {
48  pss = nullptr; sln = nullptr; this->lines = false; basic_title.assign(title);
49  }
50 
51  template<typename Scalar>
52  VectorBaseView<Scalar>::VectorBaseView(char* title, WinGeom* wg)
53  : VectorView(title, wg) {
54  pss = nullptr; sln = nullptr; this->lines = false; basic_title.assign(title);
55  }
56 
57  template<typename Scalar>
58  VectorBaseView<Scalar>::~VectorBaseView()
59  {
60  free();
61  }
62 
63  template<typename Scalar>
64  void VectorBaseView<Scalar>::free()
65  {
66  if (pss != nullptr) { delete pss; pss = nullptr; }
67  }
68 
69  template<>
70  void VectorBaseView<double>::update_solution()
71  {
72  double* coeffs = malloc_with_check<double>(ndof + 1);
73  memset(coeffs, 0, sizeof(double)* (ndof + 1));
74  if (base_index >= -1 && base_index < ndof)
75  coeffs[base_index + 1] = 1.0;
76 
77  Solution<double>::vector_to_solution(coeffs, space, sln, pss);
78 
79  VectorView::show(sln, sln, H2D_FN_VAL_0, H2D_FN_VAL_1);
80  update_title();
81 
82  free_with_check(coeffs);
83  }
84  template<>
85  void VectorBaseView<std::complex<double> >::update_solution()
86  {
87  std::complex<double>* coeffs = calloc_with_check<std::complex<double> >(ndof + 1);
88  if (base_index >= -1 && base_index < ndof)
89  coeffs[base_index + 1] = 1.0;
90  Solution<std::complex<double> >::vector_to_solution(coeffs, space, sln, pss);
91 
92  Hermes::Hermes2D::RealFilter filter(sln);
93 
94  this->VectorView::show(&filter, &filter, H2D_FN_VAL_0, H2D_FN_VAL_1);
95  update_title();
96 
97  free_with_check(coeffs);
98  }
99 
100  template<typename Scalar>
101  void VectorBaseView<Scalar>::update_title()
102  {
103  std::stringstream str;
104  str << basic_title << " - dof = " << base_index;
105  if (base_index < 0)
106  str << " (Dirichlet lift)";
107  View::set_title(str.str().c_str());
108  }
109 
110  template<typename Scalar>
111  void VectorBaseView<Scalar>::on_special_key(int key, int x, int y)
112  {
113  switch (key)
114  {
115  case GLUT_KEY_LEFT:
116  if (base_index > -1) base_index--;
117  update_solution();
118  break;
119 
120  case GLUT_KEY_RIGHT:
121  if (base_index < ndof - 1) base_index++;
122  update_solution();
123  break;
124 
125  default:
126  VectorView::on_special_key(key, x, y);
127  }
128  }
129 
130  template<typename Scalar>
131  const char* VectorBaseView<Scalar>::get_help_text() const
132  {
133  return
134  "VectorBaseView\n\n"
135  "Controls:\n"
136  " Left mouse - pan\n"
137  " Right mouse - zoom\n"
138  " Left arrow - previous basis function\n"
139  " Right arrow - next basis function\n"
140  " C - center image\n"
141  " F - toggle smooth palette\n"
142  " X - toggle hexagonal grid\n"
143  " H - render high-quality frame\n"
144  " M - toggle mesh\n"
145  " P - cycle palettes\n"
146  " S - save screenshot\n"
147  " F1 - this help\n"
148  " Esc, Q - quit";
149  }
150  }
151  }
152 }
153 #endif
154 
155 namespace Hermes
156 {
157  namespace Hermes2D
158  {
159  namespace Views
160  {
161  template class HERMES_API VectorBaseView < double > ;
162  template class HERMES_API VectorBaseView < std::complex<double> > ;
163  }
164  }
165 }
Definition: adapt.h:24
Common definitions for Hermes2D.
Removes the imaginary part from a function.
Definition: filter.h:289