COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
atest-ufem-navier-stokes-restart.py
Go to the documentation of this file.
1 import sys
2 import coolfluid as cf
3 import numpy as np
4 import os
5 from optparse import OptionParser
6 
7 env = cf.Core.environment()
8 
9 # Global configuration
10 env.assertion_throws = False
11 env.assertion_backtrace = False
12 env.exception_backtrace = False
13 env.regist_signal_handlers = False
14 env.log_level = 4
15 
17  Ua = 0.
18  Va = 0.
19  D = 0.5
20 
21  model = None
22  solver = None
23  mesh = None
24 
25 
26  def __init__(self, builder, dt, element, prefix):
27  self.dt = dt
28  self.element = element
29  self.prefix = prefix
30  self.setup_model(builder)
31  self.builder = builder
32 
33  def __del__(self):
34  if self.model != None:
35  self.model.delete_component()
36 
37  def create_mesh(self, segments):
38  domain = self.model.Domain
39  blocks = domain.create_component('blocks', 'cf3.mesh.BlockMesh.BlockArrays')
40  points = blocks.create_points(dimensions = 2, nb_points = 6)
41  points[0] = [-0.5, -0.5]
42  points[1] = [0.5, -0.5]
43  points[2] = [-0.5, 0.]
44  points[3] = [0.5, 0.]
45  points[4] = [-0.5,0.5]
46  points[5] = [0.5, 0.5]
47 
48  block_nodes = blocks.create_blocks(2)
49  block_nodes[0] = [0, 1, 3, 2]
50  block_nodes[1] = [2, 3, 5, 4]
51 
52  block_subdivs = blocks.create_block_subdivisions()
53  block_subdivs[0] = [segments, segments/2]
54  block_subdivs[1] = [segments, segments/2]
55 
56  gradings = blocks.create_block_gradings()
57  gradings[0] = [1., 1., 1., 1.]
58  gradings[1] = [1., 1., 1., 1.]
59 
60  left_patch = blocks.create_patch_nb_faces(name = 'left', nb_faces = 2)
61  left_patch[0] = [2, 0]
62  left_patch[1] = [4, 2]
63 
64  bottom_patch = blocks.create_patch_nb_faces(name = 'bottom', nb_faces = 1)
65  bottom_patch[0] = [0, 1]
66 
67  top_patch = blocks.create_patch_nb_faces(name = 'top', nb_faces = 1)
68  top_patch[0] = [5, 4]
69 
70  right_patch = blocks.create_patch_nb_faces(name = 'right', nb_faces = 2)
71  right_patch[0] = [1, 3]
72  right_patch[1] = [3, 5]
73 
74  blocks.partition_blocks(nb_partitions = cf.Core.nb_procs(), direction = 0)
75 
76  mesh = domain.create_component('Mesh', 'cf3.mesh.Mesh')
77  self.mesh = mesh
78 
79  blocks.create_mesh(mesh.uri())
80 
81  create_point_region = domain.create_component('CreatePointRegion', 'cf3.mesh.actions.AddPointRegion')
82  create_point_region.coordinates = [0., 0.]
83  create_point_region.region_name = 'center'
84  create_point_region.mesh = mesh
85  create_point_region.execute()
86 
87  if self.element == 'triag':
88  triangulator = domain.create_component('Triangulator', 'cf3.mesh.MeshTriangulator')
89  triangulator.mesh = mesh
90  triangulator.execute()
91 
92  partitioner = domain.create_component('Partitioner', 'cf3.mesh.actions.PeriodicMeshPartitioner')
93  partitioner.mesh = mesh
94 
95  link_horizontal = partitioner.create_link_periodic_nodes()
96  link_horizontal.source_region = mesh.topology.right
97  link_horizontal.destination_region = mesh.topology.left
98  link_horizontal.translation_vector = [-1., 0.]
99 
100  link_vertical = partitioner.create_link_periodic_nodes()
101  link_vertical.source_region = mesh.topology.top
102  link_vertical.destination_region = mesh.topology.bottom
103  link_vertical.translation_vector = [0., -1.]
104 
105  partitioner.execute()
106 
107  domain.write_mesh(cf.URI(self.prefix + '.cf3mesh'))
108 
109  def read_mesh(self, filename):
110  reader = self.model.Domain.create_component('CF3MeshReader', 'cf3.mesh.cf3mesh.Reader')
111  reader.mesh = self.model.Domain.create_component('Mesh','cf3.mesh.Mesh')
112  reader.file = cf.URI(filename)
113  reader.execute()
114  self.mesh = reader.mesh
115 
116  def setup_ic(self):
117  if self.builder == 'cf3.UFEM.NavierStokes':
118  ic_comp = self.solver.InitialConditions
119  u_tag = 'navier_stokes_solution'
120  p_tag = 'navier_stokes_solution'
121  else:
122  ic_comp = self.solver.InitialConditions.NavierStokes
123  u_tag = 'navier_stokes_u_solution'
124  p_tag = 'navier_stokes_p_solution'
125  #initial condition for the velocity. Unset variables (i.e. the pressure) default to zero
126  ic_u = ic_comp.create_initial_condition(builder_name = 'cf3.UFEM.InitialConditionFunction', field_tag = u_tag)
127  ic_u.variable_name = 'Velocity'
128  ic_u.regions = [self.mesh.topology.interior.uri()]
129  ic_u.value = ['{Ua} - cos(pi/{D}*x)*sin(pi/{D}*y)'.format(Ua = self.Ua, D = self.D), '{Va} + sin(pi/{D}*x)*cos(pi/{D}*y)'.format(Va = self.Va, D = self.D)]
130 
131  ic_p = ic_comp.create_initial_condition(builder_name = 'cf3.UFEM.InitialConditionFunction', field_tag = p_tag)
132  ic_p.regions = [self.mesh.topology.interior.uri()]
133  ic_p.variable_name = 'Pressure'
134  ic_p.value = ['-0.25*(cos(2*pi/{D}*x) + cos(2*pi/{D}*y))'.format(D = self.D)]
135 
136  def set_restart(self, filename):
137  if self.builder == 'cf3.UFEM.NavierStokes':
138  reader = self.solver.InitialConditions.create_component('Reader', 'cf3.solver.actions.ReadRestartFile')
139  else:
140  reader = self.solver.InitialConditions.Restarts.create_component('Reader', 'cf3.solver.actions.ReadRestartFile')
141  reader.mesh = self.mesh
142  reader.file = cf.URI(filename)
143 
144  def setup_model(self, builder):
145  if self.model != None:
146  self.model.delete_component()
147 
148  model = cf.Core.root().create_component('NavierStokes'+self.prefix, 'cf3.solver.ModelUnsteady')
149  self.model = model
150  domain = model.create_domain()
151  physics = model.create_physics('cf3.UFEM.NavierStokesPhysics')
152  self.solver = model.create_solver('cf3.UFEM.Solver')
153  self.ns_solver = self.solver.add_unsteady_solver(builder)
154  self.restart_writer = self.solver.add_restart_writer()
155  self.restart_writer.Writer.file = cf.URI(self.prefix+'-{iteration}.cf3restart')
156 
157  # Physical constants
158  physics.density = 1.
159  physics.dynamic_viscosity = 0.001
160 
161  return self.solver
162 
163  def add_pressure_bc(self, bc, var_name = 'Pressure'):
164  bc.regions = [self.mesh.topology.uri()]
165  nu = self.model.NavierStokesPhysics.kinematic_viscosity
166  bc.add_function_bc(region_name = 'center', variable_name = var_name).value = ['-0.25 * (cos(2*pi/{D}*(x - {Ua}*(t+{dt}))) + cos(2*pi/{D}*(y - {Va}*(t+{dt})))) * exp(-4*{nu}*pi^2/{D}^2*(t+{dt})) '.format(D = self.D, nu = nu, Ua = self.Ua, Va = self.Va, dt = self.dt)]
167 
168  def setup(self, Ua, Va, D, theta):
169  self.Ua = Ua
170  self.Va = Va
171  self.D = D
172 
173  mesh = self.mesh
174  self.ns_solver.regions = [mesh.topology.interior.uri()]
175  self.ns_solver.options.theta = theta
176  self.theta = theta
177 
178  series_writer = self.solver.TimeLoop.create_component('TimeWriter', 'cf3.solver.actions.TimeSeriesWriter')
179  writer = series_writer.create_component('Writer', 'cf3.mesh.VTKXML.Writer')
180  writer.file = cf.URI(self.prefix+'-{iteration}.pvtu')
181  writer.mesh = self.mesh
182 
183  if self.builder == 'cf3.UFEM.NavierStokes':
184  self.add_pressure_bc(self.ns_solver.BoundaryConditions)
185  self.solver.create_fields()
186  writer.fields = [mesh.geometry.navier_stokes_solution.uri()]
187  lss = self.ns_solver.LSS
188  lss.SolutionStrategy.Parameters.preconditioner_type = 'ML'
189  lss.SolutionStrategy.Parameters.LinearSolverTypes.Belos.SolverTypes.BlockGMRES.convergence_tolerance = 1e-14
190  lss.SolutionStrategy.Parameters.LinearSolverTypes.Belos.SolverTypes.BlockGMRES.maximum_iterations = 2000
191  lss.SolutionStrategy.Parameters.PreconditionerTypes.ML.MLSettings.default_values = 'NSSA'
192  lss.SolutionStrategy.Parameters.PreconditionerTypes.ML.MLSettings.eigen_analysis_type = 'Anorm'
193  lss.SolutionStrategy.Parameters.PreconditionerTypes.ML.MLSettings.add_parameter(name = 'PDE equations', value =3)
194  lss.SolutionStrategy.Parameters.PreconditionerTypes.ML.MLSettings.aggregation_type = 'Uncoupled-MIS'
195  lss.SolutionStrategy.Parameters.PreconditionerTypes.ML.MLSettings.smoother_type = 'Chebyshev'#'symmetric block Gauss-Seidel'
196  lss.SolutionStrategy.Parameters.PreconditionerTypes.ML.MLSettings.smoother_sweeps = 3
197 
198  else:
199  self.ns_solver.PressureLSS.LSS.SolutionStrategy.Parameters.linear_solver_type = 'Amesos'
200  self.ns_solver.VelocityLSS.LSS.SolutionStrategy.Parameters.linear_solver_type = 'Amesos'
201  self.ns_solver.PressureLSS.LSS.SolutionStrategy.print_settings = False
202  self.ns_solver.VelocityLSS.LSS.SolutionStrategy.print_settings = False
203 
204  self.add_pressure_bc(self.ns_solver.PressureLSS.BC)
205 
206  self.solver.create_fields()
207  writer.fields = [mesh.geometry.navier_stokes_p_solution.uri(), mesh.geometry.navier_stokes_u_solution.uri()]
208 
209  def iterate(self, numsteps, save_interval = 1):
210  tstep = self.dt
211  self.restart_writer.interval = save_interval
212 
213  # Time setup
214  time = self.model.create_time()
215  time.time_step = tstep
216  if self.builder == 'cf3.UFEM.NavierStokes':
217  time.end_time = numsteps*tstep
218  self.model.simulate()
219  else:
220  time.end_time = 0.
221  while time.end_time < numsteps*tstep:
222  time.end_time += save_interval*tstep
223  self.model.simulate()
224  self.solver.InitialConditions.options.disabled_actions = ['NavierStokes', 'linearized_velocity']
225  self.model.print_timing_tree()
226 
227  def iterate_restart(self, end_time, save_interval = 1):
228  self.restart_writer.interval = save_interval
229 
230  # Time setup
231  time = self.model.create_time()
232  time.end_time = end_time
233  self.model.simulate()
234  self.model.print_timing_tree()
235 
236 dt = 0.004
237 elem = 'quad'
238 segs = 32
239 theta = 0.5
240 
241 # Run a simulation, saving restarts every 5 steps
242 tg_impl = TaylorGreen(builder = 'cf3.UFEM.NavierStokes', dt = dt, element=elem, prefix='restart-implicit-full')
243 tg_impl.create_mesh(segs)
244 tg_impl.setup(0.3, 0.2, D=0.5, theta=theta)
245 tg_impl.setup_ic()
246 tg_impl.iterate(10, 5)
247 
248 # Restart after the 5 first steps
249 tg_impl_restart = TaylorGreen(builder = 'cf3.UFEM.NavierStokes', dt = dt, element=elem, prefix='restart-implicit-restarted')
250 tg_impl_restart.read_mesh(tg_impl.prefix + '.cf3mesh')
251 tg_impl_restart.setup(0.3, 0.2, D=0.5, theta=theta)
252 tg_impl_restart.set_restart(tg_impl.prefix+'-5.cf3restart')
253 tg_impl_restart.iterate_restart(10.*dt, 5)
254 
255 root = cf.Core.root()
256 meshdiff = root.create_component('MeshDiff', 'cf3.mesh.actions.MeshDiff')
257 meshdiff.left = tg_impl.mesh
258 meshdiff.right = tg_impl_restart.mesh
259 meshdiff.max_ulps = 1000000000 # this relatively high number is due to the use of the iterative LSS solver
260 meshdiff.execute()
261 if not meshdiff.properties()['mesh_equal']:
262  raise Exception('Bad restart for implicit solve')
263 
264 # Run a simulation, saving restarts every 5 steps
265 tg_semi_impl = TaylorGreen(builder = 'cf3.UFEM.NavierStokesSemiImplicit', dt = dt, element=elem, prefix='restart-semi-full')
266 tg_semi_impl.create_mesh(segs)
267 tg_semi_impl.setup(0.3, 0.2, D=0.5, theta=theta)
268 tg_semi_impl.setup_ic()
269 tg_semi_impl.iterate(10, 5)
270 
271 # Restart after the 5 first steps
272 tg_semi_impl_restart = TaylorGreen(builder = 'cf3.UFEM.NavierStokesSemiImplicit', dt = dt, element=elem, prefix='restart-semi-restarted')
273 tg_semi_impl_restart.read_mesh(tg_semi_impl.prefix + '.cf3mesh')
274 tg_semi_impl_restart.setup(0.3, 0.2, D=0.5, theta=theta)
275 tg_semi_impl_restart.set_restart(tg_semi_impl.prefix+'-5.cf3restart')
276 tg_semi_impl_restart.iterate_restart(10.*dt, 5)
277 
278 meshdiff.max_ulps = 1000000000
279 meshdiff.left = tg_semi_impl.mesh
280 meshdiff.right = tg_semi_impl_restart.mesh
281 meshdiff.execute()
282 if not meshdiff.properties()['mesh_equal']:
283  raise Exception('Bad restart for semi-implicit solve')
boost::python::object create_component(ComponentWrapper &self, const std::string &name, const std::string &builder_name)
Send comments to:
COOLFluiD Web Admin