Hermes2D  2.0
exact_solution.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 
16 #include "solution_h2d_xml.h"
17 #include "exact_solution.h"
18 #include "api2d.h"
19 namespace Hermes
20 {
21  namespace Hermes2D
22  {
23  template<typename Scalar>
24  ExactSolution<Scalar>::ExactSolution(const Mesh* mesh) : Solution<Scalar>(mesh)
25  {
26  this->sln_type = HERMES_EXACT;
27  this->num_dofs = -1;
28  this->exact_multiplicator = 1.0;
29  }
30 
31  template<typename Scalar>
32  MeshFunction<Scalar>* ExactSolution<Scalar>::clone() const
33  {
34  throw Hermes::Exceptions::Exception("Solution<Scalar>::clone() must be overridden in the case of exact solutions.");
35  return NULL;
36  }
37 
38  template<typename Scalar>
39  ExactSolutionScalar<Scalar>::ExactSolutionScalar(const Mesh* mesh) : ExactSolution<Scalar>(mesh)
40  {
41  this->num_components = 1;
42  }
43 
44  template<typename Scalar>
46  {
47  return 1;
48  }
49 
50  template<typename Scalar>
52  {
53  this->num_components = 2;
54  }
55 
56  template<typename Scalar>
58  {
59  return 2;
60  }
61 
62  template<>
63  void ConstantSolution<double>::save(const char* filename) const
64  {
65  if(this->sln_type == HERMES_SLN)
66  {
67  Solution<double>::save(filename);
68  return;
69  }
70  try
71  {
72  XMLSolution::solution xmlsolution(1, 0, 0, 1, 0);
73 
74  xmlsolution.exactCXR() = this->constant;
75 
76  std::string solution_schema_location(Hermes2DApi.get_text_param_value(xmlSchemasDirPath));
77  solution_schema_location.append("/solution_h2d_xml.xsd");
78  ::xml_schema::namespace_info namespace_info_solution("XMLSolution", solution_schema_location);
79 
80  ::xml_schema::namespace_infomap namespace_info_map;
81  namespace_info_map.insert(std::pair<std::basic_string<char>, xml_schema::namespace_info>("solution", namespace_info_solution));
82 
83  std::ofstream out(filename);
84  ::xml_schema::flags parsing_flags = ::xml_schema::flags::dont_pretty_print;
85  XMLSolution::solution_(out, xmlsolution, namespace_info_map, "UTF-8", parsing_flags);
86 
87  out.close();
88  }
89  catch (const xml_schema::exception& e)
90  {
91  throw Hermes::Exceptions::SolutionSaveFailureException(e.what());
92  }
93  }
94 
95  template<>
96  void ConstantSolution<std::complex<double> >::save(const char* filename) const
97  {
98  if(this->sln_type == HERMES_SLN)
99  {
100  Solution<std::complex<double> >::save(filename);
101  return;
102  }
103  try
104  {
105  XMLSolution::solution xmlsolution(1, 0, 0, 1, 1);
106 
107  xmlsolution.exactCXR() = this->constant.real();
108  xmlsolution.exactCXC() = this->constant.imag();
109 
110  std::string solution_schema_location(Hermes2DApi.get_text_param_value(xmlSchemasDirPath));
111  solution_schema_location.append("/solution_h2d_xml.xsd");
112  ::xml_schema::namespace_info namespace_info_solution("XMLSolution", solution_schema_location);
113 
114  ::xml_schema::namespace_infomap namespace_info_map;
115  namespace_info_map.insert(std::pair<std::basic_string<char>, xml_schema::namespace_info>("solution", namespace_info_solution));
116 
117  std::ofstream out(filename);
118  ::xml_schema::flags parsing_flags = ::xml_schema::flags::dont_pretty_print;
119  XMLSolution::solution_(out, xmlsolution, namespace_info_map, "UTF-8", parsing_flags);
120  out.close();
121  }
122  catch (const xml_schema::exception& e)
123  {
124  throw Hermes::Exceptions::SolutionSaveFailureException(e.what());
125  }
126  }
127 
128  template<typename Scalar>
129  ConstantSolution<Scalar>::ConstantSolution(const Mesh* mesh, Scalar constant) : ExactSolutionScalar<Scalar>(mesh), constant(constant) {};
130 
131  template<typename Scalar>
132  Scalar ConstantSolution<Scalar>::value (double x, double y) const {
133  return constant;
134  };
135 
136  template<typename Scalar>
138  {
139  if(this->sln_type == HERMES_SLN)
140  return Solution<Scalar>::clone();
141  ConstantSolution<Scalar>* sln = new ConstantSolution<Scalar>(this->mesh, this->constant);
142  return sln;
143  }
144 
145  template<typename Scalar>
146  void ConstantSolution<Scalar>::derivatives (double x, double y, Scalar& dx, Scalar& dy) const {
147  dx = 0;
148  dy = 0;
149  };
150 
151  template<typename Scalar>
152  Ord ConstantSolution<Scalar>::ord(Ord x, Ord y) const {
153  return Ord(0);
154  }
155 
156  template<>
157  void ZeroSolution<double>::save(const char* filename) const
158  {
159  if(this->sln_type == HERMES_SLN)
160  {
161  Solution<double>::save(filename);
162  return;
163  }
164  try
165  {
166  XMLSolution::solution xmlsolution(1, 0, 0, 1, 0);
167 
168  xmlsolution.exactCXR() = 0;
169 
170  std::string solution_schema_location(Hermes2DApi.get_text_param_value(xmlSchemasDirPath));
171  solution_schema_location.append("/solution_h2d_xml.xsd");
172  ::xml_schema::namespace_info namespace_info_solution("XMLSolution", solution_schema_location);
173 
174  ::xml_schema::namespace_infomap namespace_info_map;
175  namespace_info_map.insert(std::pair<std::basic_string<char>, xml_schema::namespace_info>("solution", namespace_info_solution));
176 
177  std::ofstream out(filename);
178  ::xml_schema::flags parsing_flags = ::xml_schema::flags::dont_pretty_print;
179  XMLSolution::solution_(out, xmlsolution, namespace_info_map, "UTF-8", parsing_flags);
180  out.close();
181  }
182  catch (const xml_schema::exception& e)
183  {
184  throw Hermes::Exceptions::SolutionSaveFailureException(e.what());
185  }
186  }
187 
188  template<>
189  void ZeroSolution<std::complex<double> >::save(const char* filename) const
190  {
191  if(this->sln_type == HERMES_SLN)
192  {
193  Solution<std::complex<double> >::save(filename);
194  return;
195  }
196  try
197  {
198  XMLSolution::solution xmlsolution(1, 0, 0, 1, 1);
199 
200  xmlsolution.exactCXR() = 0;
201  xmlsolution.exactCXC() = 0;
202 
203  std::string solution_schema_location(Hermes2DApi.get_text_param_value(xmlSchemasDirPath));
204  solution_schema_location.append("/solution_h2d_xml.xsd");
205  ::xml_schema::namespace_info namespace_info_solution("XMLSolution", solution_schema_location);
206 
207  ::xml_schema::namespace_infomap namespace_info_map;
208  namespace_info_map.insert(std::pair<std::basic_string<char>, xml_schema::namespace_info>("solution", namespace_info_solution));
209 
210  std::ofstream out(filename);
211  ::xml_schema::flags parsing_flags = ::xml_schema::flags::dont_pretty_print;
212  XMLSolution::solution_(out, xmlsolution, namespace_info_map, "UTF-8", parsing_flags);
213  out.close();
214  }
215  catch (const xml_schema::exception& e)
216  {
217  throw Hermes::Exceptions::SolutionSaveFailureException(e.what());
218  }
219  }
220 
221  template<typename Scalar>
222  ZeroSolution<Scalar>::ZeroSolution(const Mesh* mesh) : ExactSolutionScalar<Scalar>(mesh) {};
223 
224  template<typename Scalar>
225  Scalar ZeroSolution<Scalar>::value (double x, double y) const {
226  return 0.0;
227  };
228 
229  template<typename Scalar>
231  {
232  if(this->sln_type == HERMES_SLN)
233  return Solution<Scalar>::clone();
234  ZeroSolution<Scalar>* sln = new ZeroSolution<Scalar>(this->mesh);
235  return sln;
236  }
237 
238  template<typename Scalar>
239  void ZeroSolution<Scalar>::derivatives (double x, double y, Scalar& dx, Scalar& dy) const {
240  dx = 0;
241  dy = 0;
242  };
243 
244  template<typename Scalar>
245  Ord ZeroSolution<Scalar>::ord(Ord x, Ord y) const {
246  return Ord(0);
247  }
248 
249 
250  template<>
251  void ConstantSolutionVector<double>::save(const char* filename) const
252  {
253  if(this->sln_type == HERMES_SLN)
254  {
255  Solution<double>::save(filename);
256  return;
257  }
258  try
259  {
260  XMLSolution::solution xmlsolution(2, 0, 0, 1, 0);
261 
262  xmlsolution.exactCXR() = this->constantX;
263  xmlsolution.exactCYR() = this->constantY;
264 
265  std::string solution_schema_location(Hermes2DApi.get_text_param_value(xmlSchemasDirPath));
266  solution_schema_location.append("/solution_h2d_xml.xsd");
267  ::xml_schema::namespace_info namespace_info_solution("XMLSolution", solution_schema_location);
268 
269  ::xml_schema::namespace_infomap namespace_info_map;
270  namespace_info_map.insert(std::pair<std::basic_string<char>, xml_schema::namespace_info>("solution", namespace_info_solution));
271 
272  std::ofstream out(filename);
273  ::xml_schema::flags parsing_flags = ::xml_schema::flags::dont_pretty_print;
274  XMLSolution::solution_(out, xmlsolution, namespace_info_map, "UTF-8", parsing_flags);
275  out.close();
276  }
277  catch (const xml_schema::exception& e)
278  {
279  throw Hermes::Exceptions::SolutionSaveFailureException(e.what());
280  }
281  }
282 
283  template<>
284  void ConstantSolutionVector<std::complex<double> >::save(const char* filename) const
285  {
286  if(this->sln_type == HERMES_SLN)
287  {
288  Solution<std::complex<double> >::save(filename);
289  return;
290  }
291  try
292  {
293  XMLSolution::solution xmlsolution(2, 0, 0, 1, 1);
294 
295  xmlsolution.exactCXR() = this->constantX.real();
296  xmlsolution.exactCXC() = this->constantX.imag();
297  xmlsolution.exactCYR() = this->constantY.real();
298  xmlsolution.exactCYC() = this->constantY.imag();
299 
300  std::string solution_schema_location(Hermes2DApi.get_text_param_value(xmlSchemasDirPath));
301  solution_schema_location.append("/solution_h2d_xml.xsd");
302  ::xml_schema::namespace_info namespace_info_solution("XMLSolution", solution_schema_location);
303 
304  ::xml_schema::namespace_infomap namespace_info_map;
305  namespace_info_map.insert(std::pair<std::basic_string<char>, xml_schema::namespace_info>("solution", namespace_info_solution));
306 
307  std::ofstream out(filename);
308 
309  ::xml_schema::flags parsing_flags = ::xml_schema::flags::dont_pretty_print;
310 
311  XMLSolution::solution_(out, xmlsolution, namespace_info_map, "UTF-8", parsing_flags);
312 
313  out.close();
314  }
315  catch (const xml_schema::exception& e)
316  {
317  throw Hermes::Exceptions::SolutionSaveFailureException(e.what());
318  }
319  }
320 
321  template<typename Scalar>
322  ConstantSolutionVector<Scalar>::ConstantSolutionVector(const Mesh* mesh, Scalar constantX, Scalar constantY) : ExactSolutionVector<Scalar>(mesh), constantX(constantX), constantY(constantY) {};
323 
324  template<typename Scalar>
325  MeshFunction<Scalar>* ConstantSolutionVector<Scalar>::clone() const
326  {
327  if(this->sln_type == HERMES_SLN)
328  return Solution<Scalar>::clone();
329  ConstantSolutionVector<Scalar>* sln = new ConstantSolutionVector<Scalar>(this->mesh, this->constantX, this->constantY);
330  return sln;
331  }
332 
333  template<typename Scalar>
334  Scalar2<Scalar> ConstantSolutionVector<Scalar>::value (double x, double y) const {
335  return Scalar2<Scalar>(constantX, constantY);
336  };
337 
338  template<typename Scalar>
339  void ConstantSolutionVector<Scalar>::derivatives (double x, double y, Scalar2<Scalar>& dx, Scalar2<Scalar>& dy) const {
340  dx = Scalar2<Scalar>(Scalar(0.0), Scalar(0.0));
341  dy = Scalar2<Scalar>(Scalar(0.0), Scalar(0.0));
342  };
343 
344  template<typename Scalar>
345  Ord ConstantSolutionVector<Scalar>::ord(Ord x, Ord y) const {
346  return Ord(0);
347  }
348 
349  template<>
350  void ZeroSolutionVector<double>::save(const char* filename) const
351  {
352  if(this->sln_type == HERMES_SLN)
353  {
354  Solution<double>::save(filename);
355  return;
356  }
357  try
358  {
359  XMLSolution::solution xmlsolution(2, 0, 0, 1, 0);
360 
361  xmlsolution.exactCXR() = 0;
362  xmlsolution.exactCYR() = 0;
363 
364  std::string solution_schema_location(Hermes2DApi.get_text_param_value(xmlSchemasDirPath));
365  solution_schema_location.append("/solution_h2d_xml.xsd");
366  ::xml_schema::namespace_info namespace_info_solution("XMLSolution", solution_schema_location);
367 
368  ::xml_schema::namespace_infomap namespace_info_map;
369  namespace_info_map.insert(std::pair<std::basic_string<char>, xml_schema::namespace_info>("solution", namespace_info_solution));
370 
371  std::ofstream out(filename);
372 
373  ::xml_schema::flags parsing_flags = ::xml_schema::flags::dont_pretty_print;
374  XMLSolution::solution_(out, xmlsolution, namespace_info_map, "UTF-8", parsing_flags);
375 
376  out.close();
377  }
378  catch (const xml_schema::exception& e)
379  {
380  throw Hermes::Exceptions::SolutionSaveFailureException(e.what());
381  }
382  }
383 
384  template<>
385  void ZeroSolutionVector<std::complex<double> >::save(const char* filename) const
386  {
387  if(this->sln_type == HERMES_SLN)
388  {
389  Solution<std::complex<double> >::save(filename);
390  return;
391  }
392  try
393  {
394  XMLSolution::solution xmlsolution(2, 0, 0, 1, 1);
395 
396  xmlsolution.exactCXR() = 0;
397  xmlsolution.exactCXC() = 0;
398  xmlsolution.exactCYR() = 0;
399  xmlsolution.exactCYC() = 0;
400 
401  std::string solution_schema_location(Hermes2DApi.get_text_param_value(xmlSchemasDirPath));
402  solution_schema_location.append("/solution_h2d_xml.xsd");
403  ::xml_schema::namespace_info namespace_info_solution("XMLSolution", solution_schema_location);
404 
405  ::xml_schema::namespace_infomap namespace_info_map;
406  namespace_info_map.insert(std::pair<std::basic_string<char>, xml_schema::namespace_info>("solution", namespace_info_solution));
407 
408  std::ofstream out(filename);
409  ::xml_schema::flags parsing_flags = ::xml_schema::flags::dont_pretty_print;
410  XMLSolution::solution_(out, xmlsolution, namespace_info_map, "UTF-8", parsing_flags);
411 
412  out.close();
413  }
414  catch (const xml_schema::exception& e)
415  {
416  throw Hermes::Exceptions::SolutionSaveFailureException(e.what());
417  }
418  }
419 
420  template<typename Scalar>
422 
423  template<typename Scalar>
424  Scalar2<Scalar> ZeroSolutionVector<Scalar>::value (double x, double y) const {
425  return Scalar2<Scalar>(0.0, 0.0);
426  };
427 
428  template<typename Scalar>
429  void ZeroSolutionVector<Scalar>::derivatives (double x, double y, Scalar2<Scalar>& dx, Scalar2<Scalar>& dy) const {
430  dx = Scalar2<Scalar>(0.0, 0.0);
431  dy = Scalar2<Scalar>(0.0, 0.0);
432  };
433 
434  template<typename Scalar>
435  Ord ZeroSolutionVector<Scalar>::ord(Ord x, Ord y) const {
436  return Ord(0);
437  }
438 
439  template<typename Scalar>
440  MeshFunction<Scalar>* ZeroSolutionVector<Scalar>::clone() const
441  {
442  if(this->sln_type == HERMES_SLN)
443  return Solution<Scalar>::clone();
444  ZeroSolutionVector<Scalar>* sln = new ZeroSolutionVector<Scalar>(this->mesh);
445  return sln;
446  }
447 
448  template HERMES_API class ExactSolutionScalar<double>;
449  template HERMES_API class ExactSolutionScalar<std::complex<double> >;
450  template HERMES_API class ExactSolutionVector<double>;
451  template HERMES_API class ExactSolutionVector<std::complex<double> >;
452  template HERMES_API class ConstantSolution<double>;
453  template HERMES_API class ConstantSolution<std::complex<double> >;
454  template HERMES_API class ConstantSolutionVector<double>;
455  template HERMES_API class ConstantSolutionVector<std::complex<double> >;
456  template HERMES_API class ZeroSolution<double>;
457  template HERMES_API class ZeroSolution<std::complex<double> >;
458  template HERMES_API class ZeroSolutionVector<double>;
459  template HERMES_API class ZeroSolutionVector<std::complex<double> >;
460  }
461 }