5 from optparse
import OptionParser
7 env = cf.Core.environment()
10 env.assertion_throws =
False
11 env.assertion_backtrace =
False
12 env.exception_backtrace =
False
13 env.regist_signal_handlers =
False
22 max_error = np.array([])
34 if self.
model !=
None:
35 self.model.delete_component()
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.]
45 points[4] = [-0.5,0.5]
46 points[5] = [0.5, 0.5]
48 block_nodes = blocks.create_blocks(2)
49 block_nodes[0] = [0, 1, 3, 2]
50 block_nodes[1] = [2, 3, 5, 4]
52 block_subdivs = blocks.create_block_subdivisions()
53 block_subdivs[0] = [segments, segments/2]
54 block_subdivs[1] = [segments, segments/2]
56 gradings = blocks.create_block_gradings()
57 gradings[0] = [1., 1., 1., 1.]
58 gradings[1] = [1., 1., 1., 1.]
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]
64 bottom_patch = blocks.create_patch_nb_faces(name =
'bottom', nb_faces = 1)
65 bottom_patch[0] = [0, 1]
67 top_patch = blocks.create_patch_nb_faces(name =
'top', nb_faces = 1)
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]
74 blocks.partition_blocks(nb_partitions = cf.Core.nb_procs(), direction = 0)
76 mesh = domain.create_component(
'Mesh',
'cf3.mesh.Mesh')
79 blocks.create_mesh(mesh.uri())
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()
88 triangulator = domain.create_component(
'Triangulator',
'cf3.mesh.MeshTriangulator')
89 triangulator.mesh = mesh
90 triangulator.execute()
107 make_boundary_global = domain.create_component(
'MakeBoundaryGlobal',
'cf3.mesh.actions.MakeBoundaryGlobal')
108 make_boundary_global.mesh = mesh
109 make_boundary_global.execute()
111 link_horizontal = domain.create_component(
'LinkHorizontal',
'cf3.mesh.actions.LinkPeriodicNodes')
112 link_horizontal.mesh = mesh
113 link_horizontal.source_region = mesh.topology.right
114 link_horizontal.destination_region = mesh.topology.left
115 link_horizontal.translation_vector = [-1., 0.]
116 link_horizontal.execute()
118 link_vertical = domain.create_component(
'LinkVertical',
'cf3.mesh.actions.LinkPeriodicNodes')
119 link_vertical.mesh = mesh
120 link_vertical.source_region = mesh.topology.top
121 link_vertical.destination_region = mesh.topology.bottom
122 link_vertical.translation_vector = [0., -1.]
123 link_vertical.execute()
125 partitioner = domain.create_component(
'Partitioner',
'cf3.zoltan.PHG')
126 partitioner.mesh = mesh
127 partitioner.execute()
129 coords = self.mesh.geometry.coordinates
133 for i
in range(len(coords)):
135 if x == (segments/4) * 1./float(segments)
and y == 0.:
136 self.probe_points.append(i)
146 ic_comp = self.solver.InitialConditions
148 ic_comp = self.solver.InitialConditions.NavierStokes
150 ic_u = ic_comp.create_initial_condition(builder_name =
'cf3.UFEM.InitialConditionFunction', field_tag = u_tag)
151 ic_u.variable_name =
'Velocity'
152 ic_u.regions = [self.mesh.topology.interior.uri()]
153 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)]
155 ic_p = ic_comp.create_initial_condition(builder_name =
'cf3.UFEM.InitialConditionFunction', field_tag = p_tag)
156 ic_p.regions = [self.mesh.topology.interior.uri()]
157 ic_p.variable_name =
'Pressure'
158 ic_p.value = [
'-0.25*(cos(2*pi/{D}*x) + cos(2*pi/{D}*y))'.format(D = self.
D)]
164 if self.
model !=
None:
165 self.model.delete_component()
167 model = cf.Core.root().
create_component(
'NavierStokes',
'cf3.solver.ModelUnsteady')
169 domain = model.create_domain()
170 physics = model.create_physics(
'cf3.UFEM.NavierStokesPhysics')
171 self.
solver = model.create_solver(
'cf3.UFEM.Solver')
175 physics.dynamic_viscosity = 0.001
180 bc.regions = [self.mesh.topology.uri()]
181 nu = self.model.NavierStokesPhysics.kinematic_viscosity
182 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)]
193 ns_solver = solver.add_unsteady_solver(
'cf3.UFEM.NavierStokes')
194 ns_solver.options.theta = theta
195 ns_solver.options.use_specializations =
False
199 ns_solver.regions = [mesh.topology.interior.uri()]
203 lss = ns_solver.create_lss(matrix_builder =
'cf3.math.LSS.TrilinosFEVbrMatrix', solution_strategy =
'cf3.math.LSS.TrilinosStratimikosStrategy')
210 solver.create_fields()
211 self.
setup_ic(
'navier_stokes_solution',
'navier_stokes_solution')
228 ns_solver = solver.add_unsteady_solver(
'cf3.UFEM.NavierStokesSemiImplicit')
230 ns_solver.options.theta = theta
231 ns_solver.options.nb_iterations = 2
237 ns_solver.regions = [mesh.topology.interior.uri()]
239 ns_solver.PressureLSS.LSS.SolutionStrategy.Parameters.linear_solver_type =
'Amesos'
240 ns_solver.VelocityLSS.LSS.SolutionStrategy.Parameters.linear_solver_type =
'Amesos'
241 ns_solver.PressureLSS.LSS.SolutionStrategy.print_settings =
False
242 ns_solver.VelocityLSS.LSS.SolutionStrategy.print_settings =
False
246 solver.create_fields()
247 self.
setup_ic(
'navier_stokes_u_solution',
'navier_stokes_p_solution')
249 def iterate(self, numsteps, save_interval = 1, process_interval = 1):
253 if (numsteps % save_interval) != 0:
254 raise RuntimeError(
'Number of time steps cannot be divided by save_interval')
257 if not os.path.exists(self.
basename)
and cf.Core.rank() == 0:
260 self.
outfile = open(
'uv_error-{modelname}-{element}-{segments}-dt_{tstep}-theta_{theta}-P{rank}.txt'.format(modelname = self.
modelname, element = self.
element, segments = self.
segments, tstep = tstep, theta = self.
theta, rank = cf.Core.rank()),
'w', 1)
261 self.outfile.write(
'# time (s), max u error, max v error, max p error')
263 self.outfile.write(
', probe {probe} u error, probe {probe} v error, probe {probe} p error'.format(probe = i))
264 self.outfile.write(
'\n')
267 time = self.model.create_time()
268 time.time_step = tstep
271 final_end_time = numsteps*tstep
277 self.
t = np.zeros(numsteps)
279 while time.current_time < final_end_time:
280 time.end_time += tstep
281 self.model.simulate()
284 if (self.
iteration % process_interval == 0)
or time.current_time >= final_end_time:
287 self.model.Domain.write_mesh(cf.URI(self.
basename+
'/taylor-green-' +str(self.
iteration) +
'.pvtu'))
290 self.model.Solver.options.disabled_actions = [
'InitialConditions']
294 self.model.print_timing_tree()
303 nu = self.model.NavierStokesPhysics.kinematic_viscosity
306 u_sol = self.mesh.geometry.navier_stokes_solution
307 p_sol = self.mesh.geometry.navier_stokes_solution
311 except AttributeError:
312 u_sol = self.mesh.geometry.navier_stokes_u_solution
313 p_sol = self.mesh.geometry.navier_stokes_p_solution
318 coords = self.mesh.geometry.coordinates
319 x_arr = np.zeros(len(coords))
320 y_arr = np.zeros(len(coords))
321 p_num = np.zeros(len(coords))
322 u_num = np.zeros(len(coords))
323 v_num = np.zeros(len(coords))
324 p_th = np.zeros(len(coords))
325 u_th = np.zeros(len(coords))
326 v_th = np.zeros(len(coords))
328 (x, y) = (x_arr[i], y_arr[i]) = coords[i]
329 (u_num[i], v_num[i], p_num[i]) = ( u_sol[i][u_idx], u_sol[i][v_idx], p_sol[i][p_idx] )
330 u_th[i] = Ua - np.cos(np.pi/D*(x-Ua*t))*np.sin(np.pi/D*(y-Va*t))*np.exp(-2.*nu*np.pi**2/D**2*t)
331 v_th[i] = Va + np.sin(np.pi/D*(x-Ua*t))*np.cos(np.pi/D*(y-Va*t))*np.exp(-2.*nu*np.pi**2/D**2*t)
332 p_th[i] = -0.25 * (np.cos(2*np.pi/D*(x - Ua*t)) + np.cos(2*np.pi/D*(y - Va*t)))*np.exp(-4.*nu*np.pi**2/D**2*t)
340 self.outfile.write(
',{u},{v},{p}'.format(u = u_th[i] - u_num[i], v = v_th[i] - v_num[i], p = p_th[i] - p_num[i]))
341 self.outfile.write(
'\n')
344 raise Exception(
'u error is too great')
346 raise Exception(
'v error is too great')
348 raise Exception(
'p error is too great')
350 parser = OptionParser()
351 parser.add_option(
'--dt', type=
'float')
352 parser.add_option(
'--elem', type=
'string')
353 parser.add_option(
'--segs', type=
'int')
354 parser.add_option(
'--theta', type=
'float')
355 parser.add_option(
'--tsteps', type=
'int')
356 (options, args) = parser.parse_args()
358 taylor_green_impl =
TaylorGreen(dt = options.dt, element=options.elem)
359 taylor_green_impl.setup_implicit(options.segs, 0.3, 0.2, D=0.5, theta=options.theta)
360 taylor_green_impl.iterate(options.tsteps, 1, 1)
362 taylor_green_semi =
TaylorGreen(dt = options.dt, element=options.elem)
363 taylor_green_semi.setup_semi_implicit(options.segs, 0.3, 0.2, D=0.5, theta=options.theta)
364 taylor_green_semi.iterate(options.tsteps, 1, 1)
def create_mesh(self, segments)
def setup_ic(self, u_tag, p_tag)
boost::python::object create_component(ComponentWrapper &self, const std::string &name, const std::string &builder_name)
def setup_semi_implicit(self, segments, Ua, Va, D, theta)
def __init__(self, dt, element)
def setup_implicit(self, segments, Ua, Va, D, theta)