fish_poisson_simple_adapt.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 // Driver for solution of 2D Poisson equation in fish-shaped domain with
31 // simple adaptivity (no macro elements)
32 
33 // Generic oomph-lib headers
34 #include "generic.h"
35 
36 // The Poisson equations
37 #include "poisson.h"
38 
39 // The fish mesh
40 #include "meshes/fish_mesh.h"
41 
42 using namespace std;
43 
44 using namespace oomph;
45 
46 //=================================================================
47 /// Fish shaped mesh with simple adaptivity (no macro elements).
48 //=================================================================
49 template<class ELEMENT>
50 class SimpleRefineableFishMesh : public virtual FishMesh<ELEMENT>,
51  public RefineableQuadMesh<ELEMENT>
52 {
53 
54 
55 public:
56 
57 
58  /// \short Constructor: Pass pointer to timestepper
59  /// (defaults to (Steady) default timestepper defined in Mesh)
60  SimpleRefineableFishMesh(TimeStepper* time_stepper_pt=
61  &Mesh::Default_TimeStepper) :
62  FishMesh<ELEMENT>(time_stepper_pt)
63  {
64 
65  // Setup quadtree forest
66  this->setup_quadtree_forest();
67 
68  // Loop over all elements and null out macro element pointer
69  unsigned n_element=this->nelement();
70  for (unsigned e=0;e<n_element;e++)
71  {
72  // Get pointer to element
73  FiniteElement* el_pt=this->finite_element_pt(e);
74 
75  // Null out the pointer to macro elements to disable them
76  el_pt->set_macro_elem_pt(0);
77  }
78  }
79 
80 
81  /// Destructor: Empty
83 
84 };
85 
86 
87 
88 
89 //============ start_of_namespace=====================================
90 /// Namespace for const source term in Poisson equation
91 //====================================================================
92 namespace ConstSourceForPoisson
93 {
94 
95  /// Strength of source function: default value -1.0
96  double Strength=-1.0;
97 
98 /// Const source function
99  void get_source(const Vector<double>& x, double& source)
100  {
101  source = Strength;
102  }
103 
104 } // end of namespace
105 
106 
107 
108 
109 //======start_of_problem_class========================================
110 /// Poisson problem in fish-shaped domain.
111 /// Template parameter identifies the element type.
112 //====================================================================
113 template<class ELEMENT>
115 {
116 
117 public:
118 
119  /// Constructor
121 
122  /// Destructor: Empty
124 
125  /// Update the problem specs after solve (empty)
127 
128  /// Update the problem specs before solve (empty)
130 
131  /// \short Overloaded version of the problem's access function to
132  /// the mesh. Recasts the pointer to the base Mesh object to
133  /// the actual mesh type.
135  {
136  return dynamic_cast<SimpleRefineableFishMesh<ELEMENT>*>(Problem::mesh_pt());
137  }
138 
139  /// \short Doc the solution. Output directory and labels are specified
140  /// by DocInfo object
141  void doc_solution(DocInfo& doc_info);
142 
143 }; // end of problem class
144 
145 
146 
147 
148 
149 //===========start_of_constructor=========================================
150 /// Constructor for adaptive Poisson problem in fish-shaped
151 /// domain.
152 //========================================================================
153 template<class ELEMENT>
155 {
156 
157  // Build fish mesh -- this is a coarse base mesh consisting
158  // of four elements.
159  Problem::mesh_pt()=new SimpleRefineableFishMesh<ELEMENT>;
160 
161  // Create/set error estimator
162  mesh_pt()->spatial_error_estimator_pt()=new Z2ErrorEstimator;
163 
164  // Set the boundary conditions for this problem: All nodes are
165  // free by default -- just pin the ones that have Dirichlet conditions
166  // here. Since the boundary values are never changed, we set
167  // them here rather than in actions_before_newton_solve().
168  unsigned num_bound = mesh_pt()->nboundary();
169  for(unsigned ibound=0;ibound<num_bound;ibound++)
170  {
171  unsigned num_nod= mesh_pt()->nboundary_node(ibound);
172  for (unsigned inod=0;inod<num_nod;inod++)
173  {
174  // Pin the single scalar value at this node
175  mesh_pt()->boundary_node_pt(ibound,inod)->pin(0);
176 
177  // Assign the homogenous boundary condition to the one
178  // and only nodal value
179  mesh_pt()->boundary_node_pt(ibound,inod)->set_value(0,0.0);
180  }
181  }
182 
183  // Loop over elements and set pointers to source function
184  unsigned n_element = mesh_pt()->nelement();
185  for(unsigned i=0;i<n_element;i++)
186  {
187  // Upcast from FiniteElement to the present element
188  ELEMENT *el_pt = dynamic_cast<ELEMENT*>(mesh_pt()->element_pt(i));
189 
190  //Set the source function pointer
191  el_pt->source_fct_pt() = &ConstSourceForPoisson::get_source;
192  }
193 
194  // Setup the equation numbering scheme
195  cout <<"Number of equations: " << assign_eqn_numbers() << std::endl;
196 
197 } // end of constructor
198 
199 
200 
201 
202 //=======start_of_doc=====================================================
203 /// Doc the solution in tecplot format.
204 //========================================================================
205 template<class ELEMENT>
207 {
208 
209  ofstream some_file;
210  char filename[100];
211 
212  // Number of plot points in each coordinate direction.
213  unsigned npts;
214  npts=5;
215 
216  // Output solution
217  sprintf(filename,"%s/soln%i.dat",doc_info.directory().c_str(),
218  doc_info.number());
219  some_file.open(filename);
220  mesh_pt()->output(some_file,npts);
221  some_file.close();
222 
223 } // end of doc
224 
225 
226 
227 
228 
229 
230 //=====================start_of_main======================================
231 /// Demonstrate how to solve 2D Poisson problem in
232 /// fish-shaped domain with mesh adaptation. First we solve on the original
233 /// coarse mesh. Next we do a few uniform refinement steps and resolve.
234 /// Finally, we enter into an automatic adapation loop.
235 //========================================================================
236 int main()
237 {
238 
239  //Set up the problem with 4 node Poisson elements
241 
242  // Setup labels for output
243  //------------------------
244  DocInfo doc_info;
245 
246  // Set output directory
247  doc_info.set_directory("RESLT");
248 
249  // Step number
250  doc_info.number()=0;
251 
252 
253  // Doc adaptivity targets
254  //-----------------------
255  problem.mesh_pt()->doc_adaptivity_targets(cout);
256 
257 
258  // Solve/doc the problem
259  //----------------------
260 
261  // Solve the problem
262  problem.newton_solve();
263 
264  //Output solution
265  problem.doc_solution(doc_info);
266 
267  //Increment counter for solutions
268  doc_info.number()++;
269 
270 
271  // Do two rounds of uniform mesh refinement and re-solve
272  //-------------------------------------------------------
273  problem.refine_uniformly();
274  problem.refine_uniformly();
275 
276  // Solve the problem
277  problem.newton_solve();
278 
279  //Output solution
280  problem.doc_solution(doc_info);
281 
282  //Increment counter for solutions
283  doc_info.number()++;
284 
285 
286  // Now do (up to) four rounds of fully automatic adapation in response to
287  //-----------------------------------------------------------------------
288  // error estimate
289  //---------------
290  unsigned max_solve=4;
291  for (unsigned isolve=0;isolve<max_solve;isolve++)
292  {
293  // Adapt problem/mesh
294  problem.adapt();
295 
296  // Re-solve the problem if the adaptation has changed anything
297  if ((problem.mesh_pt()->nrefined() !=0)||
298  (problem.mesh_pt()->nunrefined()!=0))
299  {
300  problem.newton_solve();
301  }
302  else
303  {
304  cout << "Mesh wasn't adapted --> we'll stop here" << std::endl;
305  break;
306  }
307 
308  //Output solution
309  problem.doc_solution(doc_info);
310 
311  //Increment counter for solutions
312  doc_info.number()++;
313  }
314 
315 
316 } // end of main
317 
318 
319 
void actions_before_newton_solve()
Update the problem specs before solve (empty)
double Strength
Strength of source function: default value -1.0.
virtual ~SimpleRefineableFishPoissonProblem()
Destructor: Empty.
void get_source(const Vector< double > &x, double &source)
Const source function.
SimpleRefineableFishMesh(TimeStepper *time_stepper_pt=&Mesh::Default_TimeStepper)
Constructor: Pass pointer to timestepper (defaults to (Steady) default timestepper defined in Mesh) ...
Fish shaped mesh with simple adaptivity (no macro elements).
void actions_after_newton_solve()
Update the problem specs after solve (empty)
virtual ~SimpleRefineableFishMesh()
Destructor: Empty.
void doc_solution(DocInfo &doc_info)
Doc the solution. Output directory and labels are specified by DocInfo object.
Namespace for const source term in Poisson equation.
SimpleRefineableFishMesh< ELEMENT > * mesh_pt()
Overloaded version of the problem&#39;s access function to the mesh. Recasts the pointer to the base Mesh...