Path: blob/devel/ElmerWorkflows/FreeCADBatchFEMTools/tests/variable_topology/variable_topology_generation.py
3206 views
"""1Geometry generation for with variable topology and solving test for2FreeCADBatchFEMTools - A library for using FreeCAD for FEM preprocessing in batch mode34Copyright 1st May 2018 - , Trafotek Oy, Finland56This library is free software; you can redistribute it and/or7modify it under the terms of the GNU Lesser General Public8License as published by the Free Software Foundation; either9version 2.1 of the License, or (at your option) any later version.1011This library is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14Lesser General Public License for more details.1516You should have received a copy of the GNU Lesser General Public17License along with this library (in file ../LGPL-2.1); if not, write18to the Free Software Foundation, Inc., 51 Franklin Street,19Fifth Floor, Boston, MA 02110-1301 USA2021Authors: Eelis Takala, Janne Keranen22Emails: [email protected], [email protected]23Address: Trafotek Oy24Kaarinantie 7002520540 Turku26Finland2728Original Date: May 201829"""30doc = App.newDocument("Variable_geometry")31import os32import sys33import Part34import ObjectsFem3536PWD = os.path.dirname(os.path.realpath(__file__))37module_path = PWD + "/../.."38sys.path.insert(0, module_path)3940from FreeCADBatchFEMTools import fit_view41from FreeCADBatchFEMTools import pick_faces_from_geometry42from FreeCADBatchFEMTools import add_entity_in_list43from FreeCADBatchFEMTools import create_entities_dict44from FreeCADBatchFEMTools import merge_entities_dicts45from FreeCADBatchFEMTools import get_solids_from_entities_dict46from FreeCADBatchFEMTools import create_mesh_object_and_compound_filter47from FreeCADBatchFEMTools import find_boundaries_with_entities_dict48from FreeCADBatchFEMTools import find_bodies_with_entities_dict49from FreeCADBatchFEMTools import define_mesh_sizes50from FreeCADBatchFEMTools import create_mesh51from FreeCADBatchFEMTools import run_elmergrid52from FreeCADBatchFEMTools import remove_compare_faces_from_list53from FreeCADBatchFEMTools import faces_with_vertices_in_symmetry_plane54from FreeCADBatchFEMTools import create_xor_object55from FreeCADBatchFEMTools import reduce_half_symmetry5657def create_bar(box_size, bar_length, center, name, symmetry_planes=None, mesh_size=None):58bar_obj = doc.addObject('Part::Box', name+'_obj')59bar_obj.Height = box_size60bar_obj.Width = bar_length61bar_obj.Length = box_size62bar_obj.Placement.Base = FreeCAD.Vector(center)- FreeCAD.Vector(box_size/2,0,box_size/2)63doc.recompute()6465bar_obj = reduce_half_symmetry(bar_obj, name, App, doc, planes=symmetry_planes)6667doc.recompute()68face_picks=[]69face_picks=[('alpha0',0),70('alpha1',5),71('beta0',3),72('beta1',1),73('gamma0',4),74('gamma1',2)]7576faces = pick_faces_from_geometry(bar_obj, face_picks)7778solids = []79add_entity_in_list(solids, name, bar_obj, {'mesh size':mesh_size})80entities_dict = create_entities_dict(name, faces, solids, main_object=bar_obj)8182return entities_dict8384def create_air(bars, x, y, z, name, symmetry_planes=None, mesh_size=None):8586air_with_bars = doc.addObject('Part::Box', name+ '_whole')87air_with_bars.Height = z88air_with_bars.Width = y89air_with_bars.Length = x90air_with_bars.Placement.Base = FreeCAD.Vector(-x/2,-y/2,-z/2)91air_with_bars = reduce_half_symmetry(air_with_bars, name, App, doc, planes=symmetry_planes)9293bar_objs = [bar['main object'] for bar in bars]94solid_objects = [air_with_bars]+bar_objs9596air = create_xor_object(solid_objects, doc)9798doc.recompute()99100faces_in_symmetry_plane = faces_with_vertices_in_symmetry_plane(air.Shape.Faces, plane='zx')101102# remove faces that belong to bar103bar_face_objects = []104for bar in bars:105bar_face_objects += [face_entity['geometric object'] for face_entity in bar['faces']]106107remove_compare_faces_from_list(bar_face_objects, faces_in_symmetry_plane)108109faces = []110add_entity_in_list(faces, 'gamma1', faces_in_symmetry_plane[0])111112solids = []113add_entity_in_list(solids, name, air, {'mesh size':mesh_size})114entities_dict = create_entities_dict(name, faces, solids, main_object=air)115doc.recompute()116return entities_dict117118119def create_solids(model_parameters):120x = model_parameters['x']121y = model_parameters['y']122z = model_parameters['z']123n1 = model_parameters['n1']124n2 = model_parameters['n2']125box_size = model_parameters['box_size']126airgap = model_parameters['airgap']127default_mesh_size = model_parameters['default_mesh_size']128bar_mesh_sizes = model_parameters['bar_mesh_sizes']129air_mesh_sizes = model_parameters['air_mesh_sizes']130bar_centers = []131for j in range (0, n2):132for i in range (0, n1):133bar_centers.append(((airgap+box_size/2)+i*(airgap+box_size)-x/2,-y/2,(airgap+box_size/2)+j*(airgap+box_size)-z/2))134bars = []135for i,center in enumerate(bar_centers):136name = "bar_" + str(i)137bar = create_bar(box_size, y, center, name = name, symmetry_planes=['zx'], mesh_size=bar_mesh_sizes)138bars.append(bar)139140air_entities = create_air(bars, x, y, z, name = 'air', symmetry_planes=['zx'], mesh_size=air_mesh_sizes)141142entities_dicts = [air_entities] + bars143144entities_dict = merge_entities_dicts(entities_dicts,145'variable_topology', default_mesh_size=default_mesh_size)146147doc.recompute()148149return entities_dict150151def create_sif(model_parameters):152153T_BC=model_parameters['T_BC']154n1=model_parameters['n1']155n2=model_parameters['n2']156157filepart1='''158Header159CHECK KEYWORDS Warn160Mesh DB "." "variable_topology"161Include variable_topology/mesh.names162Results Directory ""163End164165Simulation166Max Output Level = 5167Coordinate System = Cartesian !3D168Coordinate Mapping(3) = 1 2 3169170Steady State Max Iterations = 1171Output Intervals = 1172Simulation Type = Steady state !Transient173Timestepping Method = BDF174BDF Order = 1175Post File = variable_topology.vtu176177Timestep Sizes(1) = 0.1178Timestep Intervals(1) = 20179Output Intervals(1) = 1180181End182183184Constants185Gravity(4) = 0 -1 0 9.82186Stefan Boltzmann = 5.67e-08187End188189Body 1190Target Bodies(1) = $ air191Name = "Air"192Equation = 1193Material = 1194Initial Condition = 1195End196197Body 2198Target Bodies('''199200filepart1+=str(n1*n2)+ ') = $ '201202for i in range (0, n1*n2-1):203filepart1=filepart1+'bar_'+str(i)+' \\ \n '204filepart1=filepart1+'bar_'+str(i+1)205206filepart2='''207Name = "Bars"208Equation = 1209Material = 2210211Initial Condition = 1212End213214Solver 1215Equation = Heat Equation216Procedure = "HeatSolve" "HeatSolver"217Variable = Temperature218Exec Solver = Always219Stabilize = True220Bubbles = False221Lumped Mass Matrix = False222Optimize Bandwidth = True223Steady State Convergence Tolerance = 1.0e-5224Nonlinear System Convergence Tolerance = 1.0e-7225Nonlinear System Max Iterations = 1226Nonlinear System Newton After Iterations = 3227Nonlinear System Newton After Tolerance = 1.0e-3228Nonlinear System Relaxation Factor = 1229Linear System Solver = Iterative230Linear System Iterative Method = BiCGStab231Linear System Max Iterations = 500232Linear System Convergence Tolerance = 1.0e-10233BiCGstabl polynomial degree = 2234Linear System Preconditioning = ILU1235Linear System ILUT Tolerance = 1.0e-3236Linear System Abort Not Converged = False237Linear System Residual Output = 20238Linear System Precondition Recompute = 1239End240241Solver 2242Exec Solver = Always243Equation = "save scalars"244Procedure = "SaveData" "SaveScalars"245! Variable 1 = Temperature246! Operator 1 = int mean247Variable 1 = Temperature248Operator 1 = max abs249Variable 2 = Temperature250Operator 2 = min abs251Variable 3 = Time252Filename = scalars.dat253End254255Equation 1256Name = "HeatEquation"257Active Solvers(1) = 1258Convection = "constant"259End260261Initial Condition 1262Name = "InitialState"263Temperature = 0.0264End265266267Material 1268Name = "Air"269Heat Conductivity = Real 1.0 !0.026270Density = Real 1.0 !1.1644271272Heat Capacity = Real 1.0 !0.00121273End274275Material 2276Name = "Copper"277Heat Conductivity = Real 1.0 !401278Density = Real 1.0 !8940279280Heat Capacity = Real 1.0 !3.45281End282283284Boundary Condition 1285Target Boundaries(1) = $ air_gamma1286Name = "Symmetry_air"287288Heat Flux = Real 0.0289End290291'''292293BCstring1='''294Boundary Condition '''295296BCstring2='''297Target Boundaries(1) = $ bar_'''298299BCstring3='''_gamma1300Name = "Bar '''301BCstring4=''' BC"302Temperature = Real '''303304BCstring5='''305End306307'''308309filepart3=""310num=0311for j in range (0, n2):312for i in range (0, n1):313filepart3=filepart3+BCstring1+str(num+2)+BCstring2+str(num)+BCstring3+str(num)+BCstring4+str(T_BC[j][i])+BCstring5314num=num+1315316export_path=PWD+u"/variable_topology.sif"317file = open(export_path, "w+")318file.write(filepart1+filepart2+filepart3)319file.close()320321def get_test_params(test_number=1):322if test_number == 1:323n1=5324n2=2325T_BC=[[1.,2.,3.,4., 5.],[2.,3.,4.,5.,6.]]326elif test_number == 2:327n1=5328n2=3329T_BC=[[1.,2.,3.,4., 5.],[2.,3.,4.,5.,6.], [3.,4.,5.,6.,7.]]330elif test_number == 3:331n1=4332n2=2333T_BC=[[1.,2.,3.,4.],[2.,3.,4.,5.]]334elif test_number == 4:335n1=3336n2=3337T_BC=[[1.,2.,3.],[2.,3.,4.], [3.,4.,5]]338elif test_number == 5:339n1=10340n2=1341T_BC=[[1.,2.,3.,4., 5.,2.,3.,4.,5.,6.]]342elif test_number == 6:343n1=2344n2=1345T_BC=[[1.,6.]]346return n1, n2, T_BC347348##### Tests #####349def test_all():350n1, n2, T_BC = get_test_params(6)351box_size=10.352airgap=5.353depth=25.354model_parameters = {}355model_parameters['n1']=n1356model_parameters['n2']=n2357model_parameters['box_size']=box_size358model_parameters['airgap']=airgap359model_parameters['T_BC']=T_BC360model_parameters['x'] =n1*box_size+(n1+1)*airgap361model_parameters['y'] =depth362model_parameters['z'] =n2*box_size+(n2+1)*airgap363model_parameters['default_mesh_size'] = 10 #If default mesh size is smaller than given mesh sizes, fallback value is used.364model_parameters['bar_mesh_sizes'] = 2365model_parameters['air_mesh_sizes'] = 2366367default_mesh_size = model_parameters['default_mesh_size']368369entities_dict = create_solids(model_parameters)370solid_objects = get_solids_from_entities_dict(entities_dict)371372fit_view()373374mesh_object, compound_filter = create_mesh_object_and_compound_filter(solid_objects, default_mesh_size, doc)375376find_boundaries_with_entities_dict(mesh_object, compound_filter, entities_dict, doc)377find_bodies_with_entities_dict(mesh_object, compound_filter, entities_dict, doc)378379define_mesh_sizes(mesh_object, compound_filter, entities_dict, doc)380create_mesh(mesh_object)381export_path=PWD+u"/variable_topology.unv"382run_elmergrid(export_path, mesh_object)383create_sif(model_parameters)384#################385386test_all() #This test is with all the definitions and examples387if not FreeCAD.GuiUp:388exit()389390391392