Skip to content

Commit 503df76

Browse files
Merge pull request #977 from KratosMultiphysics/fluid-p2-example
Fluid p2 example
2 parents 7957d67 + 2dac437 commit 503df76

6 files changed

Lines changed: 335 additions & 1 deletion

File tree

kratos.gid/apps/Fluid/app.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"start.tcl",
1616
"examples/examples.tcl",
1717
"examples/CylinderInFlow.tcl",
18+
"examples/CylinderInFlowP2P1.tcl",
1819
"examples/HighRiseBuilding.tcl",
1920
"xml/XmlController.tcl",
2021
"write/write.tcl",
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
2+
namespace eval ::Fluid::examples::CylinderInFlowP2P1 {
3+
namespace path ::Fluid::examples
4+
Kratos::AddNamespace [namespace current]
5+
}
6+
7+
proc ::Fluid::examples::CylinderInFlowP2P1::Init {args} {
8+
if {![Kratos::IsModelEmpty]} {
9+
set txt "We are going to draw the example geometry.\nDo you want to lose your previous work?"
10+
set retval [tk_messageBox -default ok -icon question -message $txt -type okcancel]
11+
if { $retval == "cancel" } { return }
12+
}
13+
DrawGeometry$::Model::SpatialDimension
14+
AssignGroups$::Model::SpatialDimension
15+
AssignMeshSizes$::Model::SpatialDimension
16+
TreeAssignation$::Model::SpatialDimension
17+
18+
GiD_Process 'Redraw
19+
GidUtils::UpdateWindow GROUPS
20+
GidUtils::UpdateWindow LAYER
21+
GiD_Process 'Zoom Frame
22+
}
23+
24+
25+
# Draw Geometry
26+
proc ::Fluid::examples::CylinderInFlowP2P1::DrawGeometry3D {args} {
27+
DrawGeometry2D
28+
GiD_Process Mescape Utilities Copy Surfaces Duplicate DoExtrude Volumes MaintainLayers Translation FNoJoin 0.0,0.0,0.0 FNoJoin 0.0,0.0,1.0 1 escape escape escape
29+
GiD_Layers edit opaque Fluid 0
30+
31+
GiD_Process escape escape 'Render Flat escape 'Rotate Angle 270 90 escape escape escape escape 'Rotate objaxes x -150 y -30 escape escape
32+
}
33+
proc ::Fluid::examples::CylinderInFlowP2P1::DrawGeometry2D {args} {
34+
Kratos::ResetModel
35+
GiD_Layers create Fluid
36+
GiD_Layers edit to_use Fluid
37+
38+
# Geometry creation
39+
## Points ##
40+
set coordinates [list 0 1 0 5 1 0 5 0 0 0 0 0]
41+
set fluidPoints [list ]
42+
foreach {x y z} $coordinates {
43+
lappend fluidPoints [GiD_Geometry create point append Fluid $x $y $z]
44+
}
45+
46+
## Lines ##
47+
set fluidLines [list ]
48+
set initial [lindex $fluidPoints 0]
49+
foreach point [lrange $fluidPoints 1 end] {
50+
lappend fluidLines [GiD_Geometry create line append stline Fluid $initial $point]
51+
set initial $point
52+
}
53+
lappend fluidLines [GiD_Geometry create line append stline Fluid $initial [lindex $fluidPoints 0]]
54+
55+
## Surface ##
56+
GiD_Process Mescape Geometry Create NurbsSurface {*}$fluidLines escape escape
57+
58+
# Body #
59+
GiD_Layers create Body
60+
GiD_Layers edit to_use Body
61+
set circle_center_x 1.25
62+
set circle_center_y 0.5
63+
set circle_center_z 0.0
64+
set center_radius 0.1
65+
GiD_Process Mescape Geometry Create Object CirclePNR $circle_center_x $circle_center_y $circle_center_z 0.0 0.0 1.0 $center_radius escape
66+
GiD_Geometry delete surface 2
67+
68+
# Create the hole
69+
GiD_Layers edit to_use Fluid
70+
GiD_Process MEscape Geometry Edit HoleNurb 1 5 escape escape
71+
72+
}
73+
74+
75+
# Group assign
76+
proc ::Fluid::examples::CylinderInFlowP2P1::AssignGroups2D {args} {
77+
# Create the groups
78+
GiD_Groups create Fluid
79+
GiD_Groups edit color Fluid "#26d1a8ff"
80+
GiD_EntitiesGroups assign Fluid surfaces 1
81+
82+
GiD_Groups create Inlet
83+
GiD_Groups edit color Inlet "#e0210fff"
84+
GiD_EntitiesGroups assign Inlet lines 4
85+
86+
GiD_Groups create Outlet
87+
GiD_Groups edit color Outlet "#42eb71ff"
88+
GiD_EntitiesGroups assign Outlet lines 2
89+
90+
GiD_Groups create No_Slip_Walls
91+
GiD_Groups edit color No_Slip_Walls "#3b3b3bff"
92+
GiD_EntitiesGroups assign No_Slip_Walls lines {1 3}
93+
94+
GiD_Groups create No_Slip_Cylinder
95+
GiD_Groups edit color No_Slip_Cylinder "#3b3b3bff"
96+
GiD_EntitiesGroups assign No_Slip_Cylinder lines 5
97+
}
98+
proc ::Fluid::examples::CylinderInFlowP2P1::AssignGroups3D {args} {
99+
# Create the groups
100+
GiD_Groups create Fluid
101+
GiD_Groups edit color Fluid "#26d1a8ff"
102+
GiD_EntitiesGroups assign Fluid volumes 1
103+
104+
GiD_Groups create Inlet
105+
GiD_Groups edit color Inlet "#e0210fff"
106+
GiD_EntitiesGroups assign Inlet surfaces 5
107+
108+
GiD_Groups create Outlet
109+
GiD_Groups edit color Outlet "#42eb71ff"
110+
GiD_EntitiesGroups assign Outlet surfaces 3
111+
112+
GiD_Groups create No_Slip_Walls
113+
GiD_Groups edit color No_Slip_Walls "#3b3b3bff"
114+
GiD_EntitiesGroups assign No_Slip_Walls surfaces {1 2 4 7}
115+
116+
GiD_Groups create No_Slip_Cylinder
117+
GiD_Groups edit color No_Slip_Cylinder "#3b3b3bff"
118+
GiD_EntitiesGroups assign No_Slip_Cylinder surfaces 6
119+
}
120+
121+
122+
# Mesh sizes
123+
proc ::Fluid::examples::CylinderInFlowP2P1::AssignMeshSizes3D {args} {
124+
set cylinder_mesh_size 0.005
125+
set walls_mesh_size 0.05
126+
set fluid_mesh_size 0.05
127+
GiD_Process Mescape Utilities Variables SizeTransitionsFactor 0.4 escape escape
128+
GiD_Process Mescape Meshing AssignSizes Surfaces $cylinder_mesh_size {*}[GiD_EntitiesGroups get No_Slip_Cylinder surfaces] escape escape
129+
GiD_Process Mescape Meshing AssignSizes Surfaces $walls_mesh_size {*}[GiD_EntitiesGroups get Inlet surfaces] escape escape
130+
GiD_Process Mescape Meshing AssignSizes Surfaces $walls_mesh_size {*}[GiD_EntitiesGroups get Outlet surfaces] escape escape
131+
GiD_Process Mescape Meshing AssignSizes Surfaces $walls_mesh_size {*}[GiD_EntitiesGroups get No_Slip_Walls surfaces] escape escape
132+
GiD_Process Mescape Meshing AssignSizes Volumes $fluid_mesh_size [GiD_EntitiesGroups get Fluid volumes] escape escape
133+
Kratos::Event_BeforeMeshGeneration $fluid_mesh_size
134+
}
135+
proc ::Fluid::examples::CylinderInFlowP2P1::AssignMeshSizes2D {args} {
136+
set cylinder_mesh_size 0.005
137+
set fluid_mesh_size 0.05
138+
GiD_Process Mescape Utilities Variables SizeTransitionsFactor 0.4 escape escape
139+
GiD_Process Mescape Meshing AssignSizes Lines $cylinder_mesh_size {*}[GiD_EntitiesGroups get No_Slip_Cylinder lines] escape escape
140+
GiD_Process Mescape Meshing AssignSizes Surfaces $fluid_mesh_size [GiD_EntitiesGroups get Fluid surfaces] escape escape
141+
Kratos::Event_BeforeMeshGeneration $fluid_mesh_size
142+
}
143+
144+
145+
# Tree assign
146+
proc ::Fluid::examples::CylinderInFlowP2P1::TreeAssignation3D {args} {
147+
TreeAssignation2D
148+
::Fluid::examples::AddCuts
149+
}
150+
proc ::Fluid::examples::CylinderInFlowP2P1::TreeAssignation2D {args} {
151+
set nd $::Model::SpatialDimension
152+
set root [customlib::GetBaseRoot]
153+
154+
set condtype line
155+
if {$nd eq "3D"} { set condtype surface }
156+
157+
# Monolithic solution strategy set
158+
spdAux::SetValueOnTreeItem v "Monolithic" FLSolStrat
159+
spdAux::SetValueOnTreeItem v "bdf2" FLScheme
160+
161+
# Fluid Parts
162+
set fluidParts [spdAux::getRoute "FLParts"]
163+
set fluidNode [customlib::AddConditionGroupOnXPath $fluidParts Fluid]
164+
# set props [list Element Monolithic$nd ConstitutiveLaw Newtonian2DLaw DENSITY 1.0 DYNAMIC_VISCOSITY 0.002 YIELD_STRESS 0 POWER_LAW_K 1 POWER_LAW_N 1]
165+
set props [list Element P2P1$nd ConstitutiveLaw Newtonian2DLaw DENSITY 1.0 DYNAMIC_VISCOSITY 0.002]
166+
spdAux::SetValuesOnBaseNode $fluidNode $props
167+
168+
set fluidConditions [spdAux::getRoute "FLBC"]
169+
::Fluid::examples::ErasePreviousIntervals
170+
171+
# Fluid Inlet
172+
Fluid::xml::CreateNewInlet Inlet {new true name inlet1 ini 0 end 1} true "6*y*(1-y)*sin(pi*t*0.5)"
173+
Fluid::xml::CreateNewInlet Inlet {new true name inlet2 ini 1 end End} true "6*y*(1-y)"
174+
175+
# Fluid Outlet
176+
set fluidOutlet "$fluidConditions/condition\[@n='Outlet$nd'\]"
177+
set outletNode [customlib::AddConditionGroupOnXPath $fluidOutlet Outlet]
178+
$outletNode setAttribute ov $condtype
179+
set props [list value 0.0]
180+
spdAux::SetValuesOnBaseNode $outletNode $props
181+
182+
# Fluid Conditions
183+
[customlib::AddConditionGroupOnXPath "$fluidConditions/condition\[@n='NoSlip$nd'\]" No_Slip_Walls] setAttribute ov $condtype
184+
[customlib::AddConditionGroupOnXPath "$fluidConditions/condition\[@n='NoSlip$nd'\]" No_Slip_Cylinder] setAttribute ov $condtype
185+
186+
# Time parameters
187+
set parameters [list EndTime 45 DeltaTime 0.1]
188+
set xpath [spdAux::getRoute "FLTimeParameters"]
189+
spdAux::SetValuesOnBasePath $xpath $parameters
190+
191+
# Output
192+
set parameters [list OutputControlType step OutputDeltaStep 1]
193+
set xpath "[spdAux::getRoute FLResults]/container\[@n='GiDOutput'\]/container\[@n='GiDOptions'\]"
194+
spdAux::SetValuesOnBasePath $xpath $parameters
195+
196+
# Parallelism
197+
set parameters [list ParallelSolutionType OpenMP OpenMPNumberOfThreads 4]
198+
set xpath [spdAux::getRoute "Parallelization"]
199+
spdAux::SetValuesOnBasePath $xpath $parameters
200+
201+
# Set the linear solver as bicgstab
202+
spdAux::SetValueOnTreeItem v "bicgstab" FLMonolithiclinear_solver_settings Solver
203+
204+
spdAux::RequestRefresh
205+
}

kratos.gid/apps/Fluid/examples/examples.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@
1414
<Example id="CylinderInFlow3D" app="Fluid" logo="CylinderInFlow3D.png" name="Cylinder in flow 3D" dim="3D" cmd="::Fluid::examples::CylinderInFlow::Init">
1515
<Description></Description>
1616
</Example>
17+
18+
<Example id="CylinderInFlowP2P12D" app="Fluid" logo="CylinderInFlow2D.png" name="Cylinder in flow 2D with P2 P1 element" dim="2D" cmd="::Fluid::examples::CylinderInFlowP2P1::Init">
19+
<Description></Description>
20+
</Example>
21+
<!-- <Example id="CylinderInFlow3D" app="Fluid" logo="CylinderInFlow3D.png" name="Cylinder in flow 3D" dim="3D" cmd="::Fluid::examples::CylinderInFlow::Init">
22+
<Description></Description>
23+
</Example> -->
1724
</Group>

kratos.gid/apps/Fluid/xml/Elements.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
</ElementItem>
272272

273273
<ElementItem n="P2P12D" pn="P2-P1" ImplementedInFile="incompressible_navier_stokes_p2_p1_continuous.cpp" ImplementedInApplication="FluidDynamicsApplication" FormulationElementType="p2p1"
274-
MinimumKratosVersion="9000" WorkingSpaceDimension="2D" LocalSpaceDimension="2" RequiresLocalAxes="False"
274+
MinimumKratosVersion="9000" WorkingSpaceDimension="2D" LocalSpaceDimension="2" RequiresLocalAxes="False" MeshOrder="Quadratic"
275275
LargeDeformation="False" ElementType="Fluid" help="This element implements a P2-P1 incompressible Navier-Stokes element">
276276
<!--here we could add a list of all of the possible geometries-->
277277
<TopologyFeatures>

kratos.gid/kratos.tcl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace eval ::Kratos {
1111

1212
variable tmp_init_mesh_time
1313
variable namespaces
14+
15+
variable mesh_criteria_forced
1416
}
1517

1618
proc GiD_Event_InitProblemtype { dir } {
@@ -174,6 +176,9 @@ proc Kratos::InitGlobalVariables {dir} {
174176

175177
variable pip_packages_required
176178
set pip_packages_required [list KratosMultiphysics-all==9.5.1]
179+
180+
variable mesh_criteria_forced
181+
set mesh_criteria_forced [dict create]
177182
}
178183

179184
proc Kratos::LoadCommonScripts { } {
@@ -431,6 +436,12 @@ proc Kratos::Event_BeforeMeshGeneration {elementsize} {
431436
GiD_MeshData mesh_criteria to_be_meshed 2 surfaces [GiD_EntitiesGroups get $group surfaces]
432437
GiD_MeshData mesh_criteria to_be_meshed 2 volumes [GiD_EntitiesGroups get $group volumes]
433438
}
439+
440+
# Change the mesh settings depending on the element requirements
441+
if {[Kratos::CheckMeshCriteria $elementsize]<0} {
442+
return "-cancel-"
443+
}
444+
434445
# Maybe the current application needs to do some extra job
435446
set ret [apps::ExecuteOnCurrentApp BeforeMeshGeneration $elementsize]
436447
set endtime [clock seconds]
@@ -446,6 +457,10 @@ proc Kratos::Event_MeshProgress { total_percent partial_percents_0 partial_perce
446457

447458
proc Kratos::Event_AfterMeshGeneration {fail} {
448459
variable tmp_init_mesh_time
460+
461+
# Change the mesh settings depending on the element requirements. Reset previous settings
462+
# catch {Kratos::ResetMeshCriteria $fail}
463+
449464
# Maybe the current application needs to do some extra job
450465
apps::ExecuteOnCurrentApp AfterMeshGeneration $fail
451466
set endtime [clock seconds]

kratos.gid/scripts/Utils.tcl

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,112 @@ proc Kratos::GetMeshBasicData { } {
292292
return $result
293293
}
294294

295+
proc Kratos::CheckMeshCriteria { elementsize } {
296+
297+
set force_mesh_order [dict create]
298+
set elements_used [spdAux::GetUsedElements]
299+
set forced_mesh_order -1
300+
foreach element_id $elements_used {
301+
set element [Model::getElement $element_id]
302+
if {[$element hasAttribute "MeshOrder"]} {
303+
set element_forces [$element getAttribute "MeshOrder"]
304+
if {$element_forces eq "Quadratic"} {
305+
set element_forces 1
306+
} else {
307+
set element_forces 0
308+
}
309+
dict set force_mesh_order $element_id $element_forces
310+
if {$forced_mesh_order eq -1} {
311+
set forced_mesh_order $element_forces
312+
} else {
313+
if {$forced_mesh_order ne $element_forces} {
314+
# W "The element $element_id requires a different mesh order"
315+
W "Incompatible mesh orders in elements"
316+
return -1
317+
}
318+
}
319+
}
320+
}
321+
322+
if {$forced_mesh_order ne -1} {
323+
324+
set element [lindex [dict keys $force_mesh_order] 0]
325+
set previous_mesh_order [write::isquadratic]
326+
set current_mesh_type [Kratos::GetMeshOrderName $previous_mesh_order]
327+
set desired_mesh_type [Kratos::GetMeshOrderName $forced_mesh_order]
328+
if {$previous_mesh_order ne $forced_mesh_order} {
329+
W "The element $element requires a different mesh order: $desired_mesh_type"
330+
W "Currently the mesh order is $current_mesh_type. please change it in the menu Mesh > Quadratic type"
331+
return -1
332+
}
333+
}
334+
return 0
335+
}
336+
337+
proc Kratos::GetMeshOrderName {order} {
338+
switch $order {
339+
0 {return "Linear"}
340+
1 {return "Quadratic"}
341+
2 {return "Quadratic9"}
342+
default {return "Unknown"}
343+
}
344+
}
345+
346+
proc Kratos::SetMeshCriteria { elementsize } {
347+
348+
set force_mesh_order [dict create]
349+
set elements_used [spdAux::GetUsedElements]
350+
set forced_mesh_order -1
351+
foreach element_id $elements_used {
352+
set element [Model::getElement $element_id]
353+
if {[$element hasAttribute "MeshOrder"]} {
354+
set element_forces [$element getAttribute "MeshOrder"]
355+
if {$element_forces eq "Quadratic"} {
356+
set element_forces 1
357+
} else {
358+
set element_forces 0
359+
}
360+
dict set force_mesh_order $element_id $element_forces
361+
if {$forced_mesh_order eq -1} {
362+
set forced_mesh_order $element_forces
363+
} else {
364+
if {$forced_mesh_order ne $element_forces} {
365+
# W "The element $element_id requires a different mesh order"
366+
error "Incompatible mesh orders in elements"
367+
}
368+
}
369+
}
370+
}
371+
372+
if {$forced_mesh_order ne -1} {
373+
374+
set previous_mesh_order [write::isquadratic]
375+
variable mesh_criteria_forced
376+
dict set mesh_criteria_forced "PreviousMeshOrder" [write::isquadratic]
377+
GiD_Set Model(QuadraticType) $forced_mesh_order
378+
set mesh_type "Quadratic"
379+
if {$forced_mesh_order eq 0} {
380+
set mesh_type "Linear"
381+
}
382+
::GidUtils::SetWarnLine "Setting mesh mode: $mesh_type"
383+
}
384+
}
385+
386+
387+
proc Kratos::ResetMeshCriteria { fail } {
388+
variable mesh_criteria_forced
389+
if {[dict exists $mesh_criteria_forced "PreviousMeshOrder"]} {
390+
set previous_mesh_order [dict get $mesh_criteria_forced "PreviousMeshOrder"]
391+
GiD_Set Model(QuadraticType) $previous_mesh_order
392+
set mesh_type "Quadratic"
393+
if {$previous_mesh_order eq 0} {
394+
set mesh_type "Linear"
395+
}
396+
::GidUtils::SetWarnLine "Restoring mesh mode: $mesh_type"
397+
dict unset mesh_criteria_forced "PreviousMeshOrder"
398+
}
399+
}
400+
295401
proc ? {question true_val false_val} {
296402
return [expr $question ? $true_val : $false_val]
297403
}

0 commit comments

Comments
 (0)