Path: blob/devel/ElmerWorkflows/FreeCADBatchFEMTools/tests/massive_coil/massive_generation.py
3206 views
"""1Massive coil generation 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("Massive winding")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_elmergrid5253def create_core(h, w, a, b, name, mesh_sizes=None):5455core = doc.addObject('PartDesign::Body',name+'_obj')56sketch = core.newObject('Sketcher::SketchObject',name+' sketch')57#sketch.Support = (doc.XY_Plane, [''])58sketch.MapMode = 'FlatFace'5960sketch.addGeometry(Part.LineSegment(App.Vector(0,-h/2, 0),App.Vector(0,h/2, 0)),False)61sketch.addGeometry(Part.LineSegment(App.Vector(0,h/2, 0),App.Vector(w/2, h/2, 0)),False)62sketch.addGeometry(Part.LineSegment(App.Vector(w/2,h/2,0),App.Vector(w/2,a/2,0)),False)63sketch.addGeometry(Part.LineSegment(App.Vector(w/2,a/2,0),App.Vector(b/2,a/2,0)),False)64sketch.addGeometry(Part.LineSegment(App.Vector(b/2,a/2,0),App.Vector(b/2,-a/2,0)),False)65sketch.addGeometry(Part.LineSegment(App.Vector(b/2,-a/2,0),App.Vector(w/2,-a/2,0)),False)66sketch.addGeometry(Part.LineSegment(App.Vector(w/2,-a/2,0),App.Vector(w/2,-h/2,0)),False)67sketch.addGeometry(Part.LineSegment(App.Vector(w/2,-h/2,0),App.Vector(0,-h/2,0)),False)6869doc.recompute()70fit_view()7172rev = core.newObject("PartDesign::Revolution",name + " rev")73rev.Profile = sketch74rev.ReferenceAxis = (sketch,['V_Axis'])75rev.Angle=18076rev.Reversed = 177doc.recompute()78# #fit_view()7980# Here we define the entities dictionary.81face_picks=[82('infinity_xz0',6),83('infinity_xz1',0),84('cylinder_lateral_surface',1),85('cylinder_lateral_surface',5),86('xy0',7),87('xy0',8)88]89faces = pick_faces_from_geometry(core, face_picks)9091solids = []92add_entity_in_list(solids, name, core, mesh_sizes=mesh_sizes)9394entities_out = create_entities_dict(name, faces, solids, main_object=core)95return entities_out9697def create_coil (r, R, h, z, name, face_entities=True, mesh_sizes=None):98coil = doc.addObject('PartDesign::Body',name+'_obj')99sketch = coil.newObject('Sketcher::SketchObject',name+' sketch')100#sketch.Support = (doc.XY_Plane, [''])101sketch.MapMode = 'FlatFace'102103sketch.addGeometry(Part.LineSegment(App.Vector(r,-h/2+z, 0),App.Vector(r,h/2+z, 0)),False)104sketch.addGeometry(Part.LineSegment(App.Vector(r,h/2+z, 0),App.Vector(R,h/2+z, 0)),False)105sketch.addGeometry(Part.LineSegment(App.Vector(R,h/2+z, 0),App.Vector(R,-h/2+z, 0)),False)106sketch.addGeometry(Part.LineSegment(App.Vector(R,-h/2+z, 0),App.Vector(r,-h/2+z, 0)),False)107doc.recompute()108fit_view()109110rev = coil.newObject("PartDesign::Revolution",name + " rev")111rev.Profile = sketch112rev.ReferenceAxis = (sketch,['V_Axis'])113rev.Angle=180114rev.Reversed = 1115doc.recompute()116117coil_faces=coil.Shape.Faces118119# Here we define the entities dictionary. Note that each coil contain the same120# names for each geometric entity. That is why the 'name' key is required121# in the dictionary.122123# Naming of the faces can be avoided by giving face_entities=False as argument124faces=[]125if(face_entities):126face_picks=[('gamma0',4),127('gamma1',5)]128129faces = pick_faces_from_geometry(coil, face_picks)130131solids=[]132add_entity_in_list(solids, name, coil, mesh_sizes=mesh_sizes)133134entities_out = create_entities_dict(name, faces, solids, main_object=coil)135136return entities_out137138def create_air(coil_inner_radius, coil_outer_radius, coil_height, airgap, z, coil_entities, name, mesh_sizes=None):139140airpluscoil_entities=create_coil(coil_inner_radius-airgap, coil_outer_radius+airgap, coil_height+airgap*2, z, name = "air and coil", face_entities=False)141142air = doc.addObject("Part::Cut","air"+'_obj')143air.Base = airpluscoil_entities['main object']144air.Tool = coil_entities['main object']145doc.recompute()146147# Here we define the entities dictionary.148face_picks=[149('xy0',3),150('xy0',4),151('cylinder_lateral_surface',5)152]153154faces = pick_faces_from_geometry(air, face_picks)155156solids=[]157add_entity_in_list(solids, name, air, mesh_sizes=mesh_sizes)158entities_out = create_entities_dict(name, faces, solids, main_object=air)159160return entities_out161162163def create_solids(model_parameters):164coil_inner_radius = model_parameters['coil_inner_radius']165coil_outer_radius = model_parameters['coil_outer_radius']166coil_height = model_parameters['coil_height']167z = model_parameters['z']168core_height = model_parameters['core_height']169core_diameter = model_parameters['core_diameter']170core_center_length = model_parameters['core_center_length']171core_center_diameter = model_parameters['core_center_diameter']172airgap = model_parameters['airgap']173default_mesh_size = model_parameters['default_mesh_size']174coil_mesh_sizes = model_parameters['coil_mesh_sizes']175core_mesh_sizes = model_parameters['core_mesh_sizes']176air_mesh_sizes = model_parameters['air_mesh_sizes']177178coil_entities = create_coil(coil_inner_radius, coil_outer_radius,179coil_height, z, name = "L1", mesh_sizes=coil_mesh_sizes)180core_entities = create_core(core_height,core_diameter,core_center_length,181core_center_diameter, name = 'core', mesh_sizes=core_mesh_sizes)182air_entities = create_air(coil_inner_radius, coil_outer_radius, coil_height,183airgap, z, coil_entities, name = 'air', mesh_sizes=air_mesh_sizes)184185doc.recompute()186name = 'All'187solids_entities = merge_entities_dicts([core_entities, coil_entities, air_entities],188name, default_mesh_size = default_mesh_size, add_prefixes={'solids':False, 'faces':True})189return solids_entities190191##### Tests #####192def test_all():193model_parameters = {}194model_parameters['coil_inner_radius'] = 0.11/2195model_parameters['coil_outer_radius'] = 0.11/2+0.1196model_parameters['coil_height'] = 0.1197model_parameters['z'] =.0198model_parameters['core_height'] =.31199model_parameters['core_diameter'] =.32200model_parameters['core_center_length'] =.11201model_parameters['core_center_diameter'] =.1202model_parameters['airgap'] =.005203model_parameters['default_mesh_size'] = 0.04 #If default mesh size is smaller than given mesh sizes, fallback value is used.204model_parameters['coil_mesh_sizes'] = {205'L1':0.02,206'alpha0':0.01207}208model_parameters['core_mesh_sizes'] = {'core':0.04}209model_parameters['air_mesh_sizes'] = {'air':0.009}210211default_mesh_size = model_parameters['default_mesh_size']212213entities_dict = create_solids(model_parameters)214solid_objects = get_solids_from_entities_dict(entities_dict)215216mesh_object, compound_filter = create_mesh_object_and_compound_filter(solid_objects,default_mesh_size, doc)217find_boundaries_with_entities_dict(mesh_object, compound_filter, entities_dict, doc)218find_bodies_with_entities_dict(mesh_object, compound_filter, entities_dict, doc)219define_mesh_sizes(mesh_object, compound_filter, entities_dict, doc)220fit_view()221create_mesh(mesh_object)222export_path=PWD+"/circuits_harmonic_massive/1962.unv"223run_elmergrid(export_path, mesh_object)224#################225226test_all() #This test is with all the definitions and examples227if not FreeCAD.GuiUp:228exit()229230231232