old_for_doc.cc
Go to the documentation of this file.
1 //LIC// ====================================================================
2 //LIC// This file forms part of oomph-lib, the object-oriented,
3 //LIC// multi-physics finite-element library, available
4 //LIC// at http://www.oomph-lib.org.
5 //LIC//
6 //LIC// Version 1.0; svn revision $LastChangedRevision$
7 //LIC//
8 //LIC// $LastChangedDate$
9 //LIC//
10 //LIC// Copyright (C) 2006-2016 Matthias Heil and Andrew Hazel
11 //LIC//
12 //LIC// This library is free software; you can redistribute it and/or
13 //LIC// modify it under the terms of the GNU Lesser General Public
14 //LIC// License as published by the Free Software Foundation; either
15 //LIC// version 2.1 of the License, or (at your option) any later version.
16 //LIC//
17 //LIC// This library is distributed in the hope that it will be useful,
18 //LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 //LIC// Lesser General Public License for more details.
21 //LIC//
22 //LIC// You should have received a copy of the GNU Lesser General Public
23 //LIC// License along with this library; if not, write to the Free Software
24 //LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 //LIC// 02110-1301 USA.
26 //LIC//
27 //LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
28 //LIC//
29 //LIC//====================================================================
30 //###########################################################
31 // OLD VERSION OF CODE -- KEEP ALIVE BECAUSE IT ALLOWS
32 // PLOT OF COUPLED AND UNCOUPLED SOLUTIONS!
33 //###########################################################
34 
35 // Driver for solution of "free boundary" 2D Poisson equation in
36 // fish-shaped domain with adaptivity
37 
38 
39 // Generic oomph-lib headers
40 #include "generic.h"
41 
42 // The Poisson equations
43 #include "poisson.h"
44 
45 // The fish mesh
46 #include "meshes/fish_mesh.h"
47 
48 // Circle as generalised element:
50 
51 using namespace std;
52 
53 using namespace oomph;
54 
55 ///////////////////////////////////////////////////////////////////////
56 ///////////////////////////////////////////////////////////////////////
57 ///////////////////////////////////////////////////////////////////////
58 
59 
60 
61 //====================================================================
62 /// Namespace for const source term in Poisson equation
63 //====================================================================
64 namespace ConstSourceForPoisson
65 {
66  /// Strength of source function: default value 1.0
67  double Strength=1.0;
68 
69 /// Const source function
70  void get_source(const Vector<double>& x, double& source)
71  {
72  source = -Strength;
73  }
74 
75 }
76 
77 
78 ////////////////////////////////////////////////////////////////////////
79 ////////////////////////////////////////////////////////////////////////
80 ////////////////////////////////////////////////////////////////////////
81 
82 
83 
84 //====================================================================
85 /// Refineable Poisson problem in deformable fish-shaped domain.
86 /// Template parameter identifies the element.
87 //====================================================================
88 template<class ELEMENT>
89 class RefineableFishPoissonProblem : public Problem
90 {
91 
92 public:
93 
94  /// \short Constructor: Bool flag specifies if position of fish back is
95  /// prescribed or computed from the coupled problem. String specifies
96  /// output directory
97  RefineableFishPoissonProblem(bool fix_position, string directory_name);
98 
99  /// Destructor
100  virtual ~RefineableFishPoissonProblem();
101 
102  /// \short Update after Newton step: Update in response to possible changes
103  /// in the wall shape
105  {
106  fish_mesh_pt()->node_update();
107  }
108 
109 
110  /// Update the problem specs before solve: Update nodal positions
112  {
113  fish_mesh_pt()->node_update();
114  }
115 
116  /// Update the problem specs after solve (empty)
118 
119  //Access function for the fish mesh
120  MacroElementNodeUpdateRefineableFishMesh<ELEMENT>* fish_mesh_pt()
121  {
122  return Fish_mesh_pt;
123  }
124 
125  /// \short Return value of the "load" on the elastically supported ring
126  /// that represents the fish's back
127  double& load()
128  {
129  return *Load_pt->value_pt(0);
130  }
131 
132  /// \short Return value of the vertical displacement of the ring that
133  /// represents the fish's back
134  double& y_c()
135  {
136  return static_cast<ElasticallySupportedRingElement*>(fish_mesh_pt()->
137  fish_back_pt())->y_c();
138  }
139 
140  /// Doc the solution
141  void doc_solution();
142 
143  /// Access to DocInfo object
144  DocInfo& doc_info() {return Doc_info;}
145 
146 private:
147 
148  /// \short Node at which the solution of the Poisson equation is documented
149  /// This solution at this node is also used as the "load" on the ring
150  /// that represents the fish's back
151  Node* Doc_node_pt;
152 
153  /// Trace file
154  ofstream Trace_file;
155 
156  /// Pointer to fish mesh
157  MacroElementNodeUpdateRefineableFishMesh<ELEMENT>* Fish_mesh_pt;
158 
159  /// Pointer to single-element mesh that stores the GeneralisedElement
160  /// that represents the fish's back
161  Mesh* Fish_back_mesh_pt;
162 
163  /// \short Pointer to data item that stores the "load" on the fish back
164  Data* Load_pt;
165 
166  /// \short Is the position of the fish's back prescribed?
167  bool Fix_position;
168 
169  /// Doc info object
170  DocInfo Doc_info;
171 
172 };
173 
174 
175 
176 
177 
178 //========================================================================
179 /// Constructor for adaptive Poisson problem in deformable fish-shaped
180 /// domain. Pass flag if position of fish back is fixed, and the output
181 /// directory.
182 //========================================================================
183 template<class ELEMENT>
185  bool fix_position, string directory_name) : Fix_position(fix_position)
186 {
187 
188  // Set output directory
189  Doc_info.set_directory(directory_name);
190 
191  // Initialise step number
192  Doc_info.number()=0;
193 
194  // Open trace file
195  char filename[100];
196  sprintf(filename,"%s/trace.dat",directory_name.c_str());
197  Trace_file.open(filename);
198  Trace_file
199  << "VARIABLES=\"load\",\"y<sub>circle</sub>\",\"u<sub>control</sub>\""
200  << std::endl;
201 
202  // Set coordinates and radius for the circle that will become the fish back
203  double x_c=0.5;
204  double y_c=0.0;
205  double r_back=1.0;
206 
207  // Build geometric object that will become the fish back
208  GeomObject* fish_back_pt=new ElasticallySupportedRingElement(x_c,y_c,r_back);
209 
210  // Build fish mesh with geometric object that specifies the fish back
211  Fish_mesh_pt=new MacroElementNodeUpdateRefineableFishMesh<ELEMENT>(fish_back_pt);
212 
213  // Add the fish mesh to the problem's collection of submeshes:
214  add_sub_mesh(Fish_mesh_pt);
215 
216  // Build mesh that will store only the geometric wall element
217  Fish_back_mesh_pt=new Mesh;
218 
219  // So far, the mesh is completely empty. Let's add the
220  // one (and only!) GeneralisedElement which represents the shape
221  // of the fish's back to it:
222  Fish_back_mesh_pt->add_element_pt(dynamic_cast<GeneralisedElement*>(
223  Fish_mesh_pt->fish_back_pt()));
224 
225  // Add the fish back mesh to the problem's collection of submeshes:
226  add_sub_mesh(Fish_back_mesh_pt);
227 
228  // Now build global mesh from the submeshes
229  build_global_mesh();
230 
231  // Create/set error estimator
232  fish_mesh_pt()->spatial_error_estimator_pt()=new Z2ErrorEstimator;
233 
234 
235  // Choose a node at which the solution is documented: Choose
236  // the central node that is shared by all four elements in
237  // the base mesh because it exists at all refinement levels.
238 
239  // How many nodes does element 0 have?
240  unsigned nnod=fish_mesh_pt()->finite_element_pt(0)->nnode();
241 
242  // The central node is the last node in element 0:
243  Doc_node_pt=fish_mesh_pt()->finite_element_pt(0)->node_pt(nnod-1);
244 
245  // Doc
246  cout << std::endl << "Control node is located at: "
247  << Doc_node_pt->x(0) << " " << Doc_node_pt->x(1) << std::endl << std::endl;
248 
249  // Position of fish back is prescribed
250  if (Fix_position)
251  {
252  // Create the load data object
253  Load_pt=new Data(1);
254 
255  // Pin the prescribed load
256  Load_pt->pin(0);
257 
258  // Pin the vertical displacement
259  dynamic_cast<ElasticallySupportedRingElement*>(
260  Fish_mesh_pt->fish_back_pt())->pin_yc();
261 
262  }
263  // Coupled problem: The position of the fish back is determined
264  // via the solution of the Poisson equation: The solution at
265  // the control node acts as the load for the displacement of the
266  // fish back
267  else
268  {
269  // Use the solution (value 0) at the control node as the load
270  // that acts on the ring. [Note: Node == Data by inheritance]
272  }
273 
274 
275  // Set the pointer to the Data object that specifies the
276  // load on the fish's back
277  dynamic_cast<ElasticallySupportedRingElement*>(Fish_mesh_pt->fish_back_pt())->
278  set_load_pt(Load_pt);
279 
280 
281  // Set the boundary conditions for this problem: All nodes are
282  // free by default -- just pin the ones that have Dirichlet conditions
283  // here.
284  unsigned num_bound = fish_mesh_pt()->nboundary();
285  for(unsigned ibound=0;ibound<num_bound;ibound++)
286  {
287  unsigned num_nod= fish_mesh_pt()->nboundary_node(ibound);
288  for (unsigned inod=0;inod<num_nod;inod++)
289  {
290  fish_mesh_pt()->boundary_node_pt(ibound,inod)->pin(0);
291  }
292  }
293 
294 
295  // Set homogeneous boundary conditions on all boundaries
296  for(unsigned ibound=0;ibound<num_bound;ibound++)
297  {
298  // Loop over the nodes on boundary
299  unsigned num_nod=fish_mesh_pt()->nboundary_node(ibound);
300  for (unsigned inod=0;inod<num_nod;inod++)
301  {
302  fish_mesh_pt()->boundary_node_pt(ibound,inod)->set_value(0,0.0);
303  }
304  }
305 
306  /// Loop over elements and set pointers to source function
307  unsigned n_element = fish_mesh_pt()->nelement();
308  for(unsigned i=0;i<n_element;i++)
309  {
310  // Upcast from FiniteElement to the present element
311  ELEMENT *el_pt = dynamic_cast<ELEMENT*>(fish_mesh_pt()->element_pt(i));
312 
313  //Set the source function pointer
314  el_pt->source_fct_pt() = &ConstSourceForPoisson::get_source;
315  }
316 
317  // Do equation numbering
318  cout << "Number of equations: " << assign_eqn_numbers() << std::endl;
319 
320 }
321 
322 
323 
324 //========================================================================
325 /// Destructor for Poisson problem in deformable fish-shaped domain.
326 //========================================================================
327 template<class ELEMENT>
329 {
330  // Close trace file
331  Trace_file.close();
332 
333 }
334 
335 
336 
337 
338 //========================================================================
339 /// Doc the solution in tecplot format.
340 //========================================================================
341 template<class ELEMENT>
343 {
344 
345  ofstream some_file;
346  char filename[100];
347 
348  // Number of plot points in each coordinate direction.
349  unsigned npts;
350  npts=5;
351 
352 
353  // Output solution
354  sprintf(filename,"%s/soln%i.dat",Doc_info.directory().c_str(),
355  Doc_info.number());
356  some_file.open(filename);
357  fish_mesh_pt()->output(some_file,npts);
358  some_file.close();
359 
360  // Write "load" on the fish's back, vertical position of the
361  // fish back, and solution at control node to trace file
362  Trace_file
363  << static_cast<ElasticallySupportedRingElement*>(fish_mesh_pt()->
364  fish_back_pt())->load()
365  << " "
366  << static_cast<ElasticallySupportedRingElement*>(fish_mesh_pt()->
367  fish_back_pt())->y_c()
368  << " " << Doc_node_pt->value(0) << std::endl;
369 }
370 
371 
372 
373 
374 
375 
376 
377 ////////////////////////////////////////////////////////////////////////
378 ////////////////////////////////////////////////////////////////////////
379 ////////////////////////////////////////////////////////////////////////
380 
381 
382 //========================================================================
383 /// Demonstrate how to solve 2D Poisson problem in deformable
384 /// fish-shaped domain with mesh adaptation.
385 //========================================================================
386 template<class ELEMENT>
387 void demo_fish_poisson(const string& directory_name)
388 {
389 
390  // Set up the problem with prescribed displacement of fish back
391  bool fix_position=true;
392  RefineableFishPoissonProblem<ELEMENT> problem(fix_position,directory_name);
393 
394 
395  // Change/doc targets for mesh adaptation
396  if (CommandLineArgs::Argc>1)
397  {
398  problem.fish_mesh_pt()->max_permitted_error()=0.05;
399  problem.fish_mesh_pt()->min_permitted_error()=0.005;
400  }
401  else
402  {
403  problem.fish_mesh_pt()->max_permitted_error()=0.005;
404  problem.fish_mesh_pt()->min_permitted_error()=0.0005;
405  }
406  problem.fish_mesh_pt()->doc_adaptivity_targets(cout);
407 
408 
409  // Do some uniform mesh refinement first
410  //--------------------------------------
411  problem.refine_uniformly();
412  problem.refine_uniformly();
413 
414  // Initial value for the vertical displacement of the fish's back
415  problem.y_c()=-0.3;
416 
417  // Loop for different fish shapes
418  //-------------------------------
419 
420  // Number of steps
421  unsigned nstep=5;
422  if (CommandLineArgs::Argc>1) nstep=1;
423 
424  // Increment in displacement
425  double dyc=0.6/double(nstep-1);
426 
427  // Loop over different fish shapes
428  for (unsigned istep=0;istep<nstep;istep++)
429  {
430  // Solve/doc
431  unsigned max_solve=2;
432  problem.newton_solve(max_solve);
433  problem.doc_solution();
434 
435  //Increment counter for solutions
436  problem.doc_info().number()++;
437 
438  // Change vertical displacement
439  problem.y_c()+=dyc;
440  }
441 
442 }
443 
444 
445 //========================================================================
446 /// Demonstrate how to solve coupled "elastic" 2D Poisson problem in deformable
447 /// fish-shaped domain with mesh adaptation.
448 //========================================================================
449 template<class ELEMENT>
450 void demo_elastic_fish_poisson(const string& directory_name)
451 {
452 
453  //Set up the problem with "elastic" fish back
454  bool fix_position=false;
455  RefineableFishPoissonProblem<ELEMENT> problem(fix_position,directory_name);
456 
457  // Change/doc targets for mesh adaptation
458  if (CommandLineArgs::Argc>1)
459  {
460  problem.fish_mesh_pt()->max_permitted_error()=0.05;
461  problem.fish_mesh_pt()->min_permitted_error()=0.005;
462  }
463  problem.fish_mesh_pt()->doc_adaptivity_targets(cout);
464 
465 
466  // Do some uniform mesh refinement first
467  problem.refine_uniformly();
468  problem.refine_uniformly();
469 
470  // Initial guess for "load" on fish back
471  problem.load()=0.0;
472 
473  // Solve/doc fully coupled problem
474  unsigned max_solve=2;
475  problem.newton_solve(max_solve);
476  problem.doc_solution();
477 
478 }
479 
480 
481 
482 
483 
484 //========================================================================
485 /// Driver for "elastic" fish poisson solver with adaptation.
486 /// If there are any command line arguments, we regard this as a
487 /// validation run and reduce the targets for the mesh adaptation.
488 //========================================================================
489 int main(int argc, char* argv[])
490 {
491 
492  // Store command line arguments
493  CommandLineArgs::setup(argc,argv);
494 
495  // Shorthand for element type
496  typedef MacroElementNodeUpdateElement<RefineableQPoissonElement<2,3> > ELEMENT;
497 
498  // Compute solution of Poisson equation in various domains -- prescribed
499  // boundary shape.
500  demo_fish_poisson<ELEMENT>("RESLT");
501 
502  // Compute coupled "elastic" coupled solution directly
503  demo_elastic_fish_poisson<ELEMENT>("RESLT_coupled");
504 
505 }
506 
507 
MacroElementNodeUpdateRefineableFishMesh< ELEMENT > * Fish_mesh_pt
Pointer to fish mesh.
Definition: old_for_doc.cc:157
void actions_after_newton_solve()
Update the problem specs after solve (empty)
Definition: old_for_doc.cc:117
RefineableFishPoissonProblem(const bool &fix_position, const string &directory_name, const unsigned &i_case)
Constructor: Bool flag specifies if position of fish back is prescribed or computed from the coupled ...
Node * Doc_node_pt
Node at which the solution of the Poisson equation is documented.
void actions_before_newton_convergence_check()
Update after Newton step: Update in response to possible changes in the wall shape.
Definition: old_for_doc.cc:104
double & y_c()
Return value of the vertical displacement of the ring that represents the fish&#39;s back.
Definition: old_for_doc.cc:134
double Strength
Strength of source function: default value 1.0.
MacroElementNodeUpdateRefineableFishMesh< ELEMENT > * fish_mesh_pt()
Definition: old_for_doc.cc:120
Definition: circle.h:37
DocInfo & doc_info()
Access to DocInfo object.
Definition: old_for_doc.cc:144
void get_source(const Vector< double > &x, double &source)
Const source function.
AlgebraicRefineableFishMesh< ELEMENT > * fish_mesh_pt()
void actions_before_newton_solve()
Update the problem specs before solve: Update nodal positions.
Definition: old_for_doc.cc:111
double & load()
Return value of the "load" on the elastically supported ring that represents the fish&#39;s back...
Definition: old_for_doc.cc:127
bool Fix_position
Is the position of the fish back prescribed?
Data * Load_pt
Pointer to data item that stores the "load" on the fish back.
void demo_elastic_fish_poisson(const string &directory_name)
Definition: old_for_doc.cc:450
AlgebraicRefineableFishMesh< ELEMENT > * Fish_mesh_pt
Pointer to fish mesh.
void demo_fish_poisson(const string &directory_name)
Definition: old_for_doc.cc:387
int main(int argc, char *argv[])
Definition: old_for_doc.cc:489
Namespace for const source term in Poisson equation.
GeneralCircle "upgraded" to a GeneralisedElement: Circular ring whose position is given by The ring...