COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
atest-ufem-navier-stokes-semi-implicit-laminar-channel-2d.py
Go to the documentation of this file.
1 import sys
2 import coolfluid as cf
3 import math
4 
5 # Some shortcuts
6 root = cf.Core.root()
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 = 1
15 env.only_cpu0_writes = True
16 
17 # setup a model
18 model = root.create_component('NavierStokes', 'cf3.solver.ModelUnsteady')
19 domain = model.create_domain()
20 physics = model.create_physics('cf3.UFEM.NavierStokesPhysics')
21 solver = model.create_solver('cf3.UFEM.Solver')
22 
23 # Copy the pressure once, to get the history
24 p_copy = solver.add_unsteady_solver('cf3.solver.actions.CopyScalar')
25 
26 # Add the Navier-Stokes solver as an unsteady solver
27 ns_solver = solver.add_unsteady_solver('cf3.UFEM.NavierStokesSemiImplicit')
28 ns_solver.options.theta = 0.5
29 ns_solver.options.nb_iterations = 2
30 ns_solver.enable_body_force = True
31 ns_solver.options.pressure_rcg_solve = True
32 ns_solver.options.alpha_su = 0.
33 #ns_solver.PressureLSS.solution_strategy = 'cf3.math.LSS.DirectStrategy'
34 refinement_level = 1
35 
36 supg_terms = solver.add_unsteady_solver('cf3.UFEM.SUPGFields')
37 residuals = solver.add_unsteady_solver('cf3.UFEM.NSResidual')
38 
39 # Generate mesh
40 blocks = domain.create_component('blocks', 'cf3.mesh.BlockMesh.BlockArrays')
41 points = blocks.create_points(dimensions = 2, nb_points = 6)
42 points[0] = [0, 0.]
43 points[1] = [10., 0.]
44 points[2] = [0., 1.]
45 points[3] = [10., 1.]
46 points[4] = [0.,2.]
47 points[5] = [10., 2.]
48 
49 block_nodes = blocks.create_blocks(2)
50 block_nodes[0] = [0, 1, 3, 2]
51 block_nodes[1] = [2, 3, 5, 4]
52 
53 block_subdivs = blocks.create_block_subdivisions()
54 block_subdivs[0] = [refinement_level*20, refinement_level*16]
55 block_subdivs[1] = block_subdivs[0]
56 
57 gradings = blocks.create_block_gradings()
58 gradings[0] = [1., 1., 1., 1.]
59 gradings[1] = [1., 1., 1., 1.]
60 
61 left_patch = blocks.create_patch_nb_faces(name = 'left', nb_faces = 2)
62 left_patch[0] = [2, 0]
63 left_patch[1] = [4, 2]
64 
65 bottom_patch = blocks.create_patch_nb_faces(name = 'bottom', nb_faces = 1)
66 bottom_patch[0] = [0, 1]
67 
68 top_patch = blocks.create_patch_nb_faces(name = 'top', nb_faces = 1)
69 top_patch[0] = [5, 4]
70 
71 right_patch = blocks.create_patch_nb_faces(name = 'right', nb_faces = 2)
72 right_patch[0] = [1, 3]
73 right_patch[1] = [3, 5]
74 
75 nb_procs = cf.Core.nb_procs()
76 if block_subdivs[0][1] % nb_procs != 0:
77  raise Exception("Vertical slices can't be divided by the number of processors")
78 
79 blocks.partition_blocks(nb_partitions = nb_procs, direction = 1)
80 
81 mesh = domain.create_component('Mesh', 'cf3.mesh.Mesh')
82 blocks.create_mesh(mesh.uri())
83 
84 create_point_region = domain.create_component('CreatePointRegion', 'cf3.mesh.actions.AddPointRegion')
85 create_point_region.coordinates = [5., 1.]
86 create_point_region.region_name = 'center'
87 create_point_region.mesh = mesh
88 create_point_region.execute()
89 
90 link_horizontal = domain.create_component('LinkHorizontal', 'cf3.mesh.actions.LinkPeriodicNodes')
91 link_horizontal.mesh = mesh
92 link_horizontal.source_region = mesh.topology.right
93 link_horizontal.destination_region = mesh.topology.left
94 link_horizontal.translation_vector = [-10., 0.]
95 link_horizontal.execute()
96 
97 
98 # Physical constants
99 physics.options().set('density', 1.)
100 physics.options().set('dynamic_viscosity', 1.)
101 
102 tstep = 0.5
103 
104 ns_solver.regions = [mesh.topology.uri()]
105 supg_terms.regions = [mesh.topology.uri()]
106 residuals.regions = [mesh.topology.uri()]
107 p_copy.regions = [mesh.topology.uri()]
108 
109 for strat in [ns_solver.children.FirstPressureStrategy, ns_solver.children.SecondPressureStrategy]:
110  strat.MLParameters.aggregation_type = 'Uncoupled'
111  strat.MLParameters.max_levels = 4
112  strat.MLParameters.smoother_sweeps = 2
113  strat.MLParameters.coarse_type = 'Amesos-KLU'
114  strat.MLParameters.add_parameter(name = 'repartition: start level', value = 0)
115  strat.MLParameters.add_parameter(name = 'repartition: max min ratio', value = 1.1)
116  #strat.SolverParameters.convergence_tolerance = 1e-6
117 
118 lss = ns_solver.VelocityLSS.LSS
119 lss.SolutionStrategy.preconditioner_reset = 20000000
120 lss.SolutionStrategy.Parameters.preconditioner_type = 'Ifpack'
121 lss.SolutionStrategy.Parameters.PreconditionerTypes.Ifpack.overlap = 0
122 lss.SolutionStrategy.Parameters.LinearSolverTypes.Belos.solver_type = 'Block CG'
123 lss.SolutionStrategy.Parameters.LinearSolverTypes.Belos.SolverTypes.BlockCG.convergence_tolerance = 1e-6
124 lss.SolutionStrategy.Parameters.LinearSolverTypes.Belos.SolverTypes.BlockCG.maximum_iterations = 300
125 #lss.SolutionStrategy.Parameters.LinearSolverTypes.Belos.SolverTypes.BlockGMRES.num_blocks = 100
126 
127 # Initial conditions
128 ic_u = solver.InitialConditions.NavierStokes.create_initial_condition(builder_name = 'cf3.UFEM.InitialConditionFunction', field_tag = 'navier_stokes_u_solution')
129 ic_u.variable_name = 'Velocity'
130 ic_u.regions = [mesh.topology.uri()]
131 ic_u.value = ['0', '0']
132 ic_g = solver.InitialConditions.NavierStokes.create_initial_condition(builder_name = 'cf3.UFEM.InitialConditionFunction', field_tag = 'body_force')
133 ic_g.variable_name = 'Force'
134 ic_g.regions = [mesh.topology.uri()]
135 ic_g.value = ['2', '0']
136 
137 # Boundary conditions
138 bc_u = ns_solver.VelocityLSS.BC
139 bc_u.add_constant_bc(region_name = 'bottom', variable_name = 'Velocity').value = [0., 0.]
140 bc_u.add_constant_bc(region_name = 'top', variable_name = 'Velocity').value = [0., 0.]
141 # Pressure BC
142 ns_solver.PressureLSS.BC.add_constant_bc(region_name = 'center', variable_name = 'Pressure').value = 10.
143 
144 solver.create_fields()
145 supg_avg = solver.add_unsteady_solver('cf3.solver.actions.FieldTimeAverage')
146 supg_avg.field = mesh.geometry.supg_terms
147 
148 res_avg = solver.add_unsteady_solver('cf3.solver.actions.FieldTimeAverage')
149 res_avg.field = mesh.geometry.navier_stokes_residual
150 
151 # Time setup
152 time = model.create_time()
153 time.time_step = tstep
154 time.end_time = 50.*tstep
155 model.simulate()
156 
157 domain.write_mesh(cf.URI('semi-implicit-laminar-channel-2d.pvtu'))
158 
159 for ((x, y), (u, v)) in zip(mesh.geometry.coordinates, mesh.geometry.navier_stokes_u_solution):
160  u_ref = y*(2-y)
161  if abs(u_ref - u) > 1e-2:
162  raise Exception('Error in u component: {u} != {u_ref} at y = {y}'.format(u = u, u_ref = u_ref, y = y))
163  if abs(v) > 1e-3:
164  raise Exception('Non-zero v-component {v} at y = {y}'.format(v = v, y = y))
165 
166 # print timings
167 model.print_timing_tree()
Send comments to:
COOLFluiD Web Admin