def __init__(self, point_coords, element_nodes, spring_constant=1.): self.pts = array(point_coords, copy=1) self.elnodes = array(element_nodes, copy=1) self.npts = len(self.pts) self.nels = len(self.elnodes) self.mesh = gf.Mesh('empty', 2) for i in range(self.npts): self.mesh.add_point(self.pts[i, :]) for i in range(self.nels): self.mesh.add_convex(gf.GeoTrans('GT_PK(1,1)'), transpose(self.pts[self.elnodes[i, :]])) self.fem = gf.MeshFem(self.mesh) self.ptdm = zeros( (self.npts, 2), dtype=int ) # point-to-dof-map i.e. rhs=self.model.rhs() then rhs[ptdm[p,i]] applies to point p and coordinate axis i self.fem.set_qdim(2) self.fem.set_classical_fem(1) # classic means Lagrange polynomial self.mim = gf.MeshIm(self.mesh, gf.Integ('IM_GAUSS1D(2)')) self.model = gf.Model('real') # 2D model variable U is of real value self.EA = spring_constant # stiffness k = E*A self.model.add_initialized_data( 'lambda', self.EA) # define Lame coefficients lambda and mu self.model.add_initialized_data('mu', 0) # define Lame coefficients lambda and mu self.model.add_fem_variable('U', self.fem) self.model.add_isotropic_linearized_elasticity_brick( self.mim, 'U', 'lambda', 'mu') self.nreg = 0 # how many regions are defined on the mesh self.fixreg = 0 # how many regions have been created for the purpose of fixing nodes self.loadreg = 0 # how many regions have been created for the purpose of loading nodes self.s_pts = zeros((self.npts, 2)) # shifted points self.lengths = zeros(self.nels) # element lengths self.s_lengths = zeros( self.nels) # element lengths in deformed geometry self.elong = zeros(self.nels) # element elongation factor self.p_elong = zeros(self.nels) # element percentage of elongation self.delta_l = zeros(self.nels) # element length differences self.thescore = 0
nu = 0.5 # Poisson Coefficient epsilon = 0.001 # Plate thickness kappa = 5. / 6. # Shear correction factor f = -5. * pow(epsilon, 3.) # Prescribed force on the top of the plate variant = 0 # 0 : not reduced, 1 : with reduced integration, # 2 : MITC reduction quadrangles = True # Locking free only on quadrangle for the moment K = 1 # Degree of the finite element method dirichlet_version = 1 # 0 = simplification, 1 = with multipliers, # 2 = penalization r = 1.E8 # Penalization parameter. NX = 80 # Number of element per direction if (quadrangles): m = gf.Mesh('cartesian', np.arange(0., 1. + 1. / NX, 1. / NX), np.arange(0., 1. + 1. / NX, 1. / NX)) else: m = gf.Mesh( 'import', 'structured', 'GT="GT_PK(2,1)";SIZES=[1,1];NOISED=0;NSUBDIV=[%d,%d];' % (NX, NX)) ## Create a mesh_fem for a 2D vector field mftheta = gf.MeshFem(m, 2) mfu = gf.MeshFem(m, 1) mftheta.set_classical_fem(K) mfu.set_classical_fem(K) mim = gf.MeshIm(m, 6) mim_reduced = gf.MeshIm(m, 1) ## Detect the border of the mesh and assign it the boundary number 1 border = m.outer_faces()
import numpy as np import getfem as gf # parameters file_msh = 'tripod.GiD.msh' degree = 1 linear = False incompressible = False # ensure that degree > 1 when incompressible is on.. E = 1e3 Nu = 0.3 Lambda = E * Nu / ((1 + Nu) * (1 - 2 * Nu)) Mu = E / (2 * (1 + Nu)) # create a Mesh object (importing) m = gf.Mesh('import', 'gid', file_msh) m.set('optimize structure') # create a MeshFem object mfu = gf.MeshFem(m, 3) # displacement mfd = gf.MeshFem(m, 1) # data mfe = gf.MeshFem(m, 1) # for plot von-mises # assign the FEM mfu.set_fem(gf.Fem('FEM_PK(3,%d)' % (degree, ))) mfd.set_fem(gf.Fem('FEM_PK(3,0)')) mfe.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(3,%d,0.01)' % (degree, ))) # build a MeshIm object mim = gf.MeshIm(m, gf.Integ('IM_TETRAHEDRON(5)')) print 'nbcvs=%d, nbpts=%d, qdim=%d, fem = %s, nbdof=%d' % \
# You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. # ############################################################################ """ test export. This program is used to check that python-getfem is working. This is also a good example of use of python-getfem.. $Id$ """ import getfem as gf import numpy as np m0 = gf.Mesh('cartesian', [0, 1, 2, 3], [0, 1, 2], [-3, -2]) m0.add_convex( gf.GeoTrans('GT_QK(2,2)'), [[0, 0, 0, .4, .6, .5, 1.2, 1, 1], [0, .3, 1, 0, .5, 1, -.1, .5, 1.1], [0, 0, 0, 0, 0, 0, 0, 0, 0]]) m0.add_convex( gf.GeoTrans('GT_PK(2,2)'), [[2, 2, 2, 2.6, 2.5, 3.2], [0, .3, 1, 0, .5, 0], [0, 0, 0, 0, 0, 0]]) m0.add_convex(gf.GeoTrans('GT_PK(1,3)'), [[3.1, 2.8, 3.2, 3.7], [0, .3, .7, 1.3], [0, 0, 0, 0]]) m0.add_convex(gf.GeoTrans('GT_PRISM(3,1)'), [[0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1], [0, 0, 0, 1, 1, 1]])
$Id$ """ import numpy as np # Import basic modules import getfem as gf ## Parameters NX = 100 # Mesh parameter. Dirichlet_with_multipliers = True # Dirichlet condition with multipliers # or penalization dirichlet_coefficient = 1e10 # Penalization coefficient # Create a simple cartesian mesh m = gf.Mesh('regular_simplices', np.arange(0, 1 + 1. / NX, 1. / NX), np.arange(0, 1 + 1. / NX, 1. / NX)) # Create a MeshFem for u and rhs fields of dimension 1 (i.e. a scalar field) mfu = gf.MeshFem(m, 1) mfrhs = gf.MeshFem(m, 1) # assign the P2 fem to all elements of the both MeshFem mfu.set_fem(gf.Fem('FEM_PK(2,2)')) mfrhs.set_fem(gf.Fem('FEM_PK(2,2)')) # Integration method used mim = gf.MeshIm(m, gf.Integ('IM_TRIANGLE(4)')) # Boundary selection flst = m.outer_faces() fnor = m.normal_of_faces(flst) tleft = abs(fnor[1, :] + 1) < 1e-14
""" import getfem as gf import numpy as np import sys try: import pyvista as pv except: print("\n\n** Could not load pyvista. Did you install it ?\n") print( " ( https://docs.pyvista.org/getting-started/installation.html ) **\n\n" ) sys.exit() convex_connectivity = np.array([0, 1, 1, 2]) mesh = gf.Mesh("cartesian", [0.0, 1.0, 2.0]) pts = mesh.pts()[0] file_name = "check_mesh_ascii.vtk" mesh.export_to_vtk(file_name, "ascii") unstructured_grid = pv.read(file_name) expected = pts actual = unstructured_grid.points[:, 0] np.testing.assert_equal(expected, actual, "export of mesh pts is not correct.") expected = convex_connectivity actual = unstructured_grid.cell_connectivity np.testing.assert_equal(expected, actual, "export of mesh convex is not correct.") file_name = "check_mesh_binary.vtk" mesh.export_to_vtk(file_name)
import numpy as np import getfem as gf with_graphics = True try: import getfem_tvtk except: print( "\n** Could NOT import getfem_tvtk -- graphical output disabled **\n") import time time.sleep(2) with_graphics = False m = gf.Mesh('import', 'gid', '../meshes/tripod.GiD.msh') print('done!') mfu = gf.MeshFem(m, 3) # displacement mfp = gf.MeshFem(m, 1) # pressure mfd = gf.MeshFem(m, 1) # data mim = gf.MeshIm(m, gf.Integ('IM_TETRAHEDRON(5)')) degree = 2 linear = False incompressible = False # ensure that degree > 1 when incompressible is on.. mfu.set_fem(gf.Fem('FEM_PK(3,%d)' % (degree, ))) mfd.set_fem(gf.Fem('FEM_PK(3,0)')) mfp.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(3,0)')) print('nbcvs=%d, nbpts=%d, qdim=%d, fem = %s, nbdof=%d' % \ (m.nbcvs(), m.nbpts(), mfu.qdim(), mfu.fem()[0].char(), mfu.nbdof()))
from scipy import io from scipy.sparse import linalg from scipy import sparse print "parameters" rho = 1.000e+00 Lambda = 1.000e+00 Mu = 1.000e+00 print "create a Mesh object" d = 1.000e+00 x = 1.000e+00 y = 1.000e+00 z = 1.000e+00 m = gf.Mesh('cartesian Q1', np.arange(0., x + d, d), np.arange(0., y + d, d), np.arange(0., z + d, d)) m.set('optimize_structure') print "create a MeshFem object" mfu = gf.MeshFem(m, 3) # displacement print "assign the FEM" mfu.set_fem(gf.Fem('FEM_QK(3,1)')) print "build a MeshIm object" mim = gf.MeshIm(m, gf.Integ('IM_HEXAHEDRON(5)')) print "detect some boundary of the mesh" P = m.pts() ctop = (abs(P[0, :] - 0.) < 1e-6) cbot = (abs(P[1, :] - 0.) < 1e-6)
# Import basic modules import getfem as gf ## Parameters NX = 20 # Mesh parameter. N = 2 Dirichlet_with_multipliers = True # Dirichlet condition with multipliers # or penalization dirichlet_coefficient = 1e10 # Penalization coefficient using_HHO = True # Use HHO method or standard Lagrange FEM # Create a simple cartesian mesh I = np.arange(0, 1 + 1. / NX, 1. / NX) if (N == 2): m = gf.Mesh('regular_simplices', I, I) elif (N == 3): m = gf.Mesh('regular_simplices', I, I, I) # Meshfems mfu = gf.MeshFem(m, 1) mfgu = gf.MeshFem(m, N) mfur = gf.MeshFem(m, 1) mfrhs = gf.MeshFem(m, 1) if (using_HHO): mfu.set_fem( gf.Fem('FEM_HHO(FEM_SIMPLEX_IPK(%d,2),FEM_SIMPLEX_CIPK(%d,2))' % (N, N - 1))) mfur.set_fem(gf.Fem('FEM_PK(%d,3)' % N)) else:
steps = 10 anglestep = 2 * pi / abs(z_2) / float(steps) Lambda = 1.18e5 Mu = 0.83e5 f_coeff = 0. disp_fem_order = 2 mult_fem_order = 1 # but discontinuous integration_degree = 4 # 9 gauss points per quad integration_contact_degree = 10 # 6 gauss points per face # mesh import m_1 = gf.Mesh('import', 'gmsh', './static_contact_planetary_1.msh') m_2 = gf.Mesh('import', 'gmsh', './static_contact_planetary_2.msh') m_p1 = gf.Mesh('import', 'gmsh', './static_contact_planetary_3.msh') m_p2 = gf.Mesh('import', 'gmsh', './static_contact_planetary_4.msh') m_p3 = gf.Mesh('import', 'gmsh', './static_contact_planetary_5.msh') # regions definitions for boundary conditions RG_NEUMANN_1 = 1 RG_DIRICHLET_2 = 2 RG_CONTACT_1 = 3 RG_CONTACT_2 = 4 RG_CONTACT_p1_out = 5 RG_CONTACT_p2_out = 6 RG_CONTACT_p3_out = 7 RG_CONTACT_p1_in = 8 RG_CONTACT_p2_in = 9
# Numerical parameters # h = 1 # Approximate mesh size elements_degree = 2 # Degree of the finite element methods gamma0 = 1. / E # Augmentation parameter for the augmented Lagrangian # # Mesh generation. Meshes can also been imported from several formats. # mo1 = gf.MesherObject('ball', [0., 15.], 15.) mo2 = gf.MesherObject('ball', [0., 15.], 8.) mo3 = gf.MesherObject('set minus', mo1, mo2) print('Meshes generation') mesh1 = gf.Mesh('generate', mo3, h, 2) mesh2 = gf.Mesh( 'import', 'structured', 'GT="GT_PK(2,1)";SIZES=[30,10];NOISED=0;NSUBDIV=[%d,%d];' % (int(30 / h) + 1, int(10 / h) + 1)) mesh2.translate([-15., -10.]) if (export_mesh): mesh1.export_to_vtk('mesh1.vtk') mesh2.export_to_vtk('mesh2.vtk') print('\nYou can view the meshes for instance with') print( 'mayavi2 -d mesh1.vtk -f ExtractEdges -m Surface -d mesh2.vtk -f ExtractEdges -m Surface \n' ) #
msh_file_names.append("./LIN-HEX/HEX-1-0.msh") # Quad-Hex 0.5mm elements_degrees.append(2) msh_file_names.append("./LIN-HEX/HEX-0-5.msh") t = time.process_time() ############################################################################### # Importing the mesh from gmsh format. # meshs = [] for i, msh_file_name in enumerate(msh_file_names): mesh = gf.Mesh("import", "gmsh", msh_file_name) meshs.append(mesh) print("Time for import mesh", time.process_time() - t) t = time.process_time() ############################################################################### # Definition of finite elements methods and integration method # mfus = [] mfds = [] mims = [] for elements_degree, mesh in zip(elements_degrees, meshs):
# ############################################################################ """ test levelset. This program is used to check that python-getfem is working. This is also a good example of use of python-getfem.. $Id$ """ import getfem as gf import numpy as np from scipy import rand eps = 1.0/10 m = gf.Mesh('regular_simplices', np.arange(-1,1+eps,eps), np.arange(-1,1+eps,eps), 'degree', 2, 'noised') #m = gf.Mesh('cartesian', np.arange(-1,1+eps,eps), np.arange(-1,1+eps,eps)) ls1 = gf.LevelSet(m, 2, 'y', 'x') #ls1 = gf.LevelSet(m, 2, 'sqr(x) + sqr(y) - sqr(0.7)', 'x-.4') #ls1 = gf.LevelSet(m, 2, 'x + y - 0.2') #, 'x-5') #ls1 = gf.LevelSet(m, 2, 'x + y - 0.2', 'x-5') #ls2 = gf.LevelSet(m, 2, '0.6*sqr(x) + sqr(y-0.1) - sqr(0.6)'); #ls3 = gf.LevelSet(m, 4, 'sqr(x) + sqr(y+.08) - sqr(0.05)'); ls2 = gf.LevelSet(m, 2, 'y+0.1', 'x') ls3 = gf.LevelSet(m, 2, 'y-0.1', 'x') mls = gf.MeshLevelSet(m) mls.add(ls1) if True:
def import_fem(sico): """ Build a mesh object using getfem. We use getfem++ to build the finite element model and to fill in the operators required by siconos: - the mass matrix (Mass) - the stiffness matrix (Stiff) - the matrix that links global coordinates and local coord. at contact points (H) """ ############################ # The geometry and the mesh ############################ dimX = 10.01 dimY = 10.01 dimZ = 3.01 stepX = 1.0 stepY = 1.0 stepZ = 1.0 x = np.arange(0, dimX, stepX) y = np.arange(0, dimY, stepY) z = np.arange(0, dimZ, stepZ) m = gf.Mesh('regular simplices', x, y, z) m.set('optimize_structure') # Export the mesh to vtk m.export_to_vtk("BlockMesh.vtk") # Create MeshFem objects # (i.e. assign elements onto the mesh for each variable) mfu = gf.MeshFem(m, 3) # displacement mfd = gf.MeshFem(m, 1) # data mff = gf.MeshFem(m, 1) # for plot von-mises # assign the FEM mfu.set_fem(gf.Fem('FEM_PK(3,1)')) mfd.set_fem(gf.Fem('FEM_PK(3,0)')) mff.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(3,1,0.01)')) # mfu.export_to_vtk("BlockMeshDispl.vtk") # Set the integration method mim = gf.MeshIm(m, gf.Integ('IM_TETRAHEDRON(5)')) # Summary print(' ==================================== \n Mesh details: ') print(' Problem dimension:', mfu.qdim(), '\n Number of elements: ', m.nbcvs(), '\n Number of nodes: ', m.nbpts()) print(' Number of dof: ', mfu.nbdof(), '\n Element type: ', mfu.fem()[0].char()) print(' ====================================') ########################### # Set the parameters # for the constitutive law ########################### E = 1e3 # Young modulus Nu = 0.3 # Poisson coef. # Lame coeff. Lambda = E * Nu / ((1 + Nu) * (1 - 2 * Nu)) Mu = E / (2 * (1 + Nu)) # Density Rho = 1.0 #7.800 Gravity = -9.81 ############################ # Boundaries detection ############################ allPoints = m.pts() # Bottom points and faces cbot = (abs(allPoints[2, :]) < 1e-6) pidbot = np.compress(cbot, list(range(0, m.nbpts()))) fbot = m.faces_from_pid(pidbot) BOTTOM = 1 m.set_region(BOTTOM, fbot) # Top points and faces ctop = (abs(allPoints[2, :]) > dimZ - stepZ) pidtop = np.compress(ctop, list(range(0, m.nbpts()))) ftop = m.faces_from_pid(pidtop) TOP = 2 m.set_region(TOP, ftop) # Top-Left points and faces cleft = (abs(allPoints[1, :]) < 1e-6) clefttop = cleft * ctop pidlefttop = np.compress(clefttop, list(range(0, m.nbpts()))) flefttop = m.faces_from_pid(pidlefttop) pidleft = np.compress(cleft, list(range(0, m.nbpts()))) fleft = m.faces_from_pid(pidleft) LEFTTOP = 3 m.set_region(LEFTTOP, flefttop) LEFT = 4 m.set_region(LEFT, fleft) # Create a model md = gf.Model('real') md.add_fem_variable('u', mfu) md.add_initialized_data('lambda', Lambda) md.add_initialized_data('mu', Mu) md.add_initialized_data('source_term', [0, 0, -100]) md.add_initialized_data('push', [0, 100, 0]) md.add_initialized_data('rho', Rho) md.add_initialized_data('gravity', Gravity) # Weight = np.zeros(mfu.nbdof()) ## Weight = [] md.add_initialized_data('weight', [0, 0, Rho * Gravity]) md.add_isotropic_linearized_elasticity_brick(mim, 'u', 'lambda', 'mu') #md.add_source_term_brick(mim,'u','source_term',TOP) #md.add_source_term_brick(mim,'u','push',LEFT) md.add_source_term_brick(mim, 'u', 'weight') #md.add_Dirichlet_condition_with_multipliers(mim,'u',mfu,BOTTOM) md.assembly() sico.Stiff = md.tangent_matrix() sico.RHS = md.rhs() sico.q0 = md.variable('u') md2 = gf.Model('real') md2.add_fem_variable('u', mfu) md2.add_initialized_data('rho', Rho) md2.add_mass_brick(mim, 'u', 'rho') md2.assembly() sico.Mass = md2.tangent_matrix() sico.nbdof = mfu.nbdof() sico.mfu = mfu sico.mesh = m sico.pos = np.zeros(sico.nbdof) sico.pos[0:sico.nbdof:3] = m.pts()[0, :] sico.pos[1:sico.nbdof:3] = m.pts()[1, :] sico.pos[2:sico.nbdof:3] = m.pts()[2, :] sico.K0 = np.dot(sico.Stiff.full(), sico.pos) sico.bot = pidbot # running solve... #md.solve() # post-processing #VM=md.compute_isotropic_linearized_Von_Mises_or_Tresca('u','lambda','mu',mff) # extracted solution #U = md.variable('u') # export U and VM in a pos file #sl = gf.Slice(('boundary',),mfu,1) #sl.export_to_vtk('toto.vtk', mfu, U, 'Displacement', mff, VM, 'Von Mises Stress') # H-Matrix fillH(pidbot, sico, mfu.nbdof()) return md
def import_fem2(sico): """ Build a mesh object using getfem. We use getfem++ to build the finite element model and to fill in the operators required by siconos: - the mass matrix (Mass) - the stiffness matrix (Stiff) - the matrix that links global coordinates and local coord. at contact points (H) """ ############################ # The geometry and the mesh ############################ dimX = 10.01 dimY = 10.01 dimZ = 10.01 stepX = 1.0 stepY = 1.0 stepZ = 1.0 x = np.arange(0, dimX, stepX) y = np.arange(0, dimY, stepY) z = np.arange(0, dimZ, stepZ) m = gf.Mesh('regular simplices', x, y, z) m.set('optimize_structure') # Export the mesh to vtk m.export_to_vtk("BlockMesh.vtk") # Create MeshFem objects # (i.e. assign elements onto the mesh for each variable) mfu = gf.MeshFem(m, 3) # displacement mfd = gf.MeshFem(m, 1) # data mff = gf.MeshFem(m, 1) # for plot von-mises # assign the FEM mfu.set_fem(gf.Fem('FEM_PK(3,1)')) mfd.set_fem(gf.Fem('FEM_PK(3,0)')) mff.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(3,1,0.01)')) # mfu.export_to_vtk("BlockMeshDispl.vtk") # Set the integration method mim = gf.MeshIm(m, gf.Integ('IM_TETRAHEDRON(5)')) # Summary print(' ==================================== \n Mesh details: ') print(' Problem dimension:', mfu.qdim(), '\n Number of elements: ', m.nbcvs(), '\n Number of nodes: ', m.nbpts()) print(' Number of dof: ', mfu.nbdof(), '\n Element type: ', mfu.fem()[0].char()) print(' ====================================') ########################### # Set the parameters # for the constitutive law ########################### E = 1e3 # Young modulus Nu = 0.3 # Poisson coef. # Lame coeff. Lambda = E * Nu / ((1 + Nu) * (1 - 2 * Nu)) Mu = E / (2 * (1 + Nu)) # Density Rho = 7800 ############################ # Boundaries detection ############################ allPoints = m.pts() # Bottom points and faces cbot = (abs(allPoints[2, :]) < 1e-6) pidbot = np.compress(cbot, list(range(0, m.nbpts()))) fbot = m.faces_from_pid(pidbot) BOTTOM = 1 m.set_region(BOTTOM, fbot) # Top points and faces ctop = (abs(allPoints[2, :]) > dimZ - stepZ) pidtop = np.compress(ctop, list(range(0, m.nbpts()))) ftop = m.faces_from_pid(pidtop) TOP = 2 m.set_region(TOP, ftop) # Top-Left points and faces cleft = (abs(allPoints[1, :]) < 1e-6) clefttop = cleft * ctop pidlefttop = np.compress(clefttop, list(range(0, m.nbpts()))) flefttop = m.faces_from_pid(pidlefttop) pidleft = np.compress(cleft, list(range(0, m.nbpts()))) fleft = m.faces_from_pid(pidleft) LEFTTOP = 3 m.set_region(LEFTTOP, flefttop) LEFT = 4 m.set_region(LEFT, fleft) ############################ # Assembly ############################ nbd = mfd.nbdof() # Stiffness matrix sico.Stiff = gf.asm_linear_elasticity(mim, mfu, mfd, np.repeat([Lambda], nbd), np.repeat([Mu], nbd)) # Mass matrix sico.Mass = Rho * gf.asm_mass_matrix(mim, mfu) # Right-hand side Ftop = gf.asm_boundary_source(TOP, mim, mfu, mfd, np.repeat([[0], [0], [-1]], nbd, 1)) Fleft = gf.asm_boundary_source(LEFT, mim, mfu, mfd, np.repeat([[0], [10], [0]], nbd, 1)) sico.RHS = Ftop + Fleft sico.nbdof = mfu.nbdof() sico.q0 = mfu.basic_dof_from_cvid() sico.bot = pidbot # H-Matrix fillH(pidbot, sico, mfu.nbdof()) return m
alpha_crack = 0.01 # [N/mm/s/K] Heat transfer coefficient mR = 287e3 * 5e-9 # [N*mm/K] --> 287000 [N*mm/kg/K] * mass [kg] room_temp = 30. # [C] temperature on one side of the block jump_temp = 100. # [C] temperature jump between the two sides # Mesh definition: L = 40. # [mm] l = 10. # [mm] ny = 10 # Number of elements in each direction quad = True # quad mesh or triangle one h = l / ny nx = np.floor(L / h) if (quad): m = gf.Mesh("cartesian", -L / 2. + np.arange(nx + 1) * h, -l / 2 + np.arange(ny + 1) * h) else: m = gf.Mesh("regular_simplices", -L / 2 + np.arange(nx + 1) * h, -l / 2 + np.arange(ny + 1) * h) LEFT_RG = 101 RIGHT_RG = 102 BOTTOM_RG = 103 TOP_RG = 104 fleft = m.outer_faces_with_direction([-1., 0.], 0.5) fright = m.outer_faces_with_direction([1., 0.], 0.5) fbottom = m.outer_faces_with_direction([0., -1.], 0.5) ftop = m.outer_faces_with_direction([0., 1.], 0.5) m.set_region(LEFT_RG, fleft) m.set_region(RIGHT_RG, fright) m.set_region(BOTTOM_RG, fbottom)
# auxiliary constants B_BOUNDARY = 4 T_BOUNDARY = 6 TB_BOUNDARY = 7 NX_seed1 = np.linspace(-1, 0, NX / 3 + 1) NX_seed2 = np.linspace(0, 1, (NX - NX / 3) + 1)[1:] NY_seed = np.linspace(-1, 1, NY + 1) X_seed1 = LX / 2 * (0.2 * NX_seed1 + 0.8 * np.sign(NX_seed1) * np.power(np.abs(NX_seed1), 1.5)) X_seed2 = LX / 2 * (0.6 * NX_seed2 + 0.4 * np.sign(NX_seed2) * np.power(np.abs(NX_seed2), 1.5)) X_seed = np.concatenate((X_seed1, X_seed2)) Y_seed = LY / 2 * (0.2 * NY_seed + 0.8 * np.sign(NY_seed) * np.power(np.abs(NY_seed), 1.5)) m1 = gf.Mesh("cartesian", X_seed, Y_seed[0:NY / 2 + 1]) m2 = gf.Mesh("cartesian", X_seed, Y_seed[NY / 2:]) gap = 1e-5 pts = m1.pts() for i in range(pts.shape[1]): if pts[0, i] < -1e-10: pts[1, i] -= gap / 2 * (1 + pts[1, i] / (LY / 2)) m1.set_pts(pts) pts = m2.pts() for i in range(pts.shape[1]): if pts[0, i] < -1e-10: pts[1, i] += gap / 2 * (1 - pts[1, i] / (LY / 2)) m2.set_pts(pts) mesh = m1 mesh.merge(m2) N = mesh.dim()
# coding: utf-8 import getfem as gf import numpy as np m = gf.Mesh('cartesian', np.arange(0.0, 10.0, 1.0)) print m mfu = gf.MeshFem(m,1) mfu.set_fem(gf.Fem('FEM_PK(1,1)')) mim = gf.MeshIm(m, gf.Integ('IM_GAUSS1D(1)')) mfd = gf.MeshFem(m, 1) mfd.set_fem(gf.Fem('FEM_PK(1,1)')) nbd = mfd.nbdof() Lambda = 1.0; Mu = 1.0 K = gf.asm_linear_elasticity(mim, mfu, mfd, np.repeat([Lambda], nbd), np.repeat([Mu], nbd)) K.full() print 'Lambda = ', Lambda print 'Mu = ', Mu print K.full() print '2*Mu+Lambda = ', 2*Mu+Lambda
# define constants NEUMANN_BOUNDARY = 1 NEUMANN_BOUNDARY_NO_LOAD = 2 DIRICHLET_BOUNDARY = 3 OMEGA = 4 INNER_FACES=6 degree = 1 make_check=('srcdir' in os.environ); filename='../meshes/mixed_mesh.gmf' if (make_check): filename=os.environ['srcdir']+'/'+filename m = gf.Mesh('load', filename) #--------------- boundary condtions # detect some boundary of the mesh face_left = m.outer_faces_with_direction([0, 0., -1.0], 0.01) face_right = m.outer_faces_with_direction([0., 0., 1.0], 0.01) face_top = m.outer_faces_with_direction([0., 1., 0], 0.01) face_bottom = m.outer_faces_with_direction([0., -1., 0], 0.01) face_top1 = m.outer_faces_with_direction([1., 0., 0], 0.01) face_bottom1 = m.outer_faces_with_direction([-1., 0., 0], 0.01) face_top_bottom = np.append(np.append(np.append(face_top,face_bottom,axis=1),face_top1 , axis=1), face_bottom1, axis=1) # create boundary regions m.set_region(NEUMANN_BOUNDARY,face_right)
""" Plate problem test. This program is used to check that python-getfem is working. This is also a good example of use of GetFEM++. $Id$ """ import numpy as np import getfem as gf NX = 10.0 thickness = 0.01 f = -5. * pow(thickness, 3.) m = gf.Mesh('regular simplices', np.arange(0, 1.01, 1 / NX), np.arange(0, 1.01, 1 / NX)) mfu3 = gf.MeshFem(m, 1) mfth = gf.MeshFem(m, 2) mfd = gf.MeshFem(m, 1) mfu3.set_fem(gf.Fem('FEM_PK(2,1)')) mfth.set_fem(gf.Fem('FEM_PK(2,2)')) mfd.set_fem(gf.Fem('FEM_PK(2,2)')) mim = gf.MeshIm(m, gf.Integ('IM_TRIANGLE(5)')) #get the list of faces whose normal is [-1,0] flst = m.outer_faces() fnor = m.normal_of_faces(flst) fleft = np.compress(abs(fnor[1, :] + 1) < 1e-14, flst, axis=1) fright = np.compress(abs(fnor[1, :] - 1) < 1e-14, flst, axis=1)
import getfem as gf import numpy as np with_graphics = True try: import getfem_tvtk except: print "\n** Could NOT import getfem_tvtk -- graphical output disabled **\n" import time time.sleep(2) with_graphics = False L = 100 H = 20 m = gf.Mesh('triangles grid', np.arange(0, L + 0.01, 4), np.arange(0, H + 0.01, 2)) mim = gf.MeshIm(m, gf.Integ('IM_TRIANGLE(6)')) mfu = gf.MeshFem(m, 2) mfsigma = gf.MeshFem(m, 4) mfd = gf.MeshFem(m) mf0 = gf.MeshFem(m) mfdu = gf.MeshFem(m) mfu.set_fem(gf.Fem('FEM_PK(2,1)')) mfsigma.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(2,1)')) mfd.set_fem(gf.Fem('FEM_PK(2,1)')) mf0.set_fem(gf.Fem('FEM_PK(2,0)')) mfdu.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(2,1)')) Lambda = 121150
alpha = 1. # Alpha coefficient for "sliding velocity" f_coeff = 0. # Friction coefficient release_dist = 5. #------------------------------------ clambda1 = E1*nu1 / ((1+nu1)*(1-2*nu1)) cmu1 = E1 / (2*(1+nu1)) clambda2 = E2*nu2 / ((1+nu2)*(1-2*nu2)) cmu2 = E2 / (2*(1+nu2)) clambda = E*nu / ((1+nu)*(1-2*nu)) cmu = E / (2*(1+nu)) mesh_R = gf.Mesh('import', 'structured', 'GT="%s";ORG=[-1,-1];SIZES=[2,1];NSUBDIV=[%i,%i]' % (geotrans_R, Ncirc, Nt1+Nt2)) mesh_B = gf.Mesh('import', 'structured', 'GT="%s";ORG=[%f,%f];SIZES=[%f,%f];NSUBDIV=[%i,%i]' % (geotrans_B, -lx/2, -ly, lx, ly, Nx, Ny)) N = mesh_R.dim() CONTACT_BOUNDARY_R = 1 DIRICHLET_BOUNDARY_R = 3 CONTACT_BOUNDARY_B = 5 DIRICHLET_BOUNDARY_B = 7 RING1 = 11 RING2 = 12 outer_R = mesh_R.outer_faces()
B_BOUNDARY = 5 T_BOUNDARY = 6 DIR_BOUNDARY = 7 OUT_BOUNDARY = 8 xmin = -L / 2 ymin = -H / 2 dx = L dy = H if symmetric: xmin = 0. ymin = 0. dx = L / 2 dy = H / 2 mesh = gf.Mesh( 'import', 'structured', 'GT="%s";ORG=[%f,%f];SIZES=[%f,%f];NSUBDIV=[%i,%i]' % (geotrans, xmin, ymin, dx, dy, N_L, N_H)) N = mesh.dim() outer_faces = mesh.outer_faces() outer_normals = mesh.normal_of_faces(outer_faces) left_boundary = outer_faces[:, np.nonzero(outer_normals[0] < -0.95)[0]] right_boundary = outer_faces[:, np.nonzero(outer_normals[0] > 0.95)[0]] bottom_boundary = outer_faces[:, np.nonzero(outer_normals[1] < -0.95)[0]] top_boundary = outer_faces[:, np.nonzero(outer_normals[1] > 0.95)[0]] mesh.set_region(L_BOUNDARY, left_boundary) mesh.set_region(R_BOUNDARY, right_boundary) mesh.set_region(B_BOUNDARY, bottom_boundary) mesh.set_region(T_BOUNDARY, top_boundary)
# =============================== # Build FEM using getfem # =============================== # ==== The geometry and the mesh ==== dimX = 10.01 dimY = 10.01 dimZ = 3.01 stepX = 2.0 stepY = 2.0 stepZ = 1.5 x = np.arange(0, dimX, stepX) y = np.arange(0, dimY, stepY) z = np.arange(0, dimZ, stepZ) m = gf.Mesh('regular simplices', x, y, z) m.set('optimize_structure') # Export the mesh to vtk m.export_to_vtk("BlockMesh.vtk") # Create MeshFem objects # (i.e. assign elements onto the mesh for each variable) mfu = gf.MeshFem(m, 3) # displacement mff = gf.MeshFem(m, 1) # for plot von-mises # assign the FEM mfu.set_fem(gf.Fem('FEM_PK(3,1)')) mff.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(3,1,0.01)')) # mfu.export_to_vtk("BlockMeshDispl.vtk") # ==== Set the integration method ==== mim = gf.MeshIm(m, gf.Integ('IM_TETRAHEDRON(5)'))
# or penalization dirichlet_coefficient = 1e10 # Penalization coefficient export_mesh = True # Draw the mesh after mesh generation or not # Create a mesh mo1 = gf.MesherObject('rectangle', [0., 50.], [100., 100.]) mo2 = gf.MesherObject('rectangle', [50., 0.], [100., 100.]) mo3 = gf.MesherObject('union', mo1, mo2) mo4 = gf.MesherObject('ball', [25., 75], 8.) mo5 = gf.MesherObject('ball', [75., 25.], 8.) mo6 = gf.MesherObject('ball', [75., 75.], 8.) mo7 = gf.MesherObject('union', mo4, mo5, mo6) mo = gf.MesherObject('set minus', mo3, mo7) gf.util('trace level', 2) # No trace for mesh generation mesh = gf.Mesh('generate', mo, h, 3) # # Boundary selection # fb1 = mesh.outer_faces_with_direction([-1., 0.], 0.01) # Left (Dirichlet) fb2 = mesh.outer_faces_with_direction([0., -1.], 0.01) # Bottom (Neumann) fb3 = mesh.outer_faces_in_box([-1., 10.], [101., 101.]) fb4 = mesh.outer_faces_in_box([10., -1.], [101., 101.]) LEFT_BOUND = 1 BOTTOM_BOUND = 2 AUX_BOUND1 = 3 AUX_BOUND2 = 4 mesh.set_region(LEFT_BOUND, fb1) mesh.set_region(BOTTOM_BOUND, fb2) mesh.set_region(AUX_BOUND1, fb3)
dirichlet_version = 2 # 1 = simplification, 2 = penalisation test_tangent_matrix = False # Test or not tangent system validity incompressible = False; # Incompressibility option explicit_potential = False; # Elasticity law with explicit potential # lawname = 'Ciarlet Geymonat' # params = [1.,1.,0.25] lawname = 'SaintVenant Kirchhoff' params = [1.,1.] if (incompressible): lawname = 'Incompressible Mooney Rivlin' params = [1.,1.] N1 = 2; N2 = 4; h = 20.; DX = 1./N1; DY = (1.*h)/N2; m = gf.Mesh('cartesian', np.arange(-0.5, 0.5+DX,DX), np.arange(0., h+DY,DY), np.arange(-1.5, 1.5+3*DX,3*DX)) mfu = gf.MeshFem(m, 3) # mesh-fem supporting a 3D-vector field mfdu = gf.MeshFem(m,1) # The mesh_im stores the integration methods for each tetrahedron mim = gf.MeshIm(m, gf.Integ('IM_GAUSS_PARALLELEPIPED(3,4)')) # We choose a P2 fem for the main unknown mfu.set_fem(gf.Fem('FEM_QK(3,2)')) if (dirichlet_version == 1): mfd = mfu; else: mfd = gf.MeshFem(m,1) mfd.set_fem(gf.Fem('FEM_QK(3,1)')) # The P2 fem is not derivable across elements, hence we use a discontinuous # fem for the derivative of U.
#% Draw solution each NBDRAW iterations hole_radius = max(0.03, 2. / NY) #% Hole radius for topological optimization ls_degree = 1 #% Degree of the level-set. Should be one for the moment. DEBUG = False if DEBUG: NG = 3 else: NG = 2 #% Mesh definition #% m=gfMesh('cartesian', -1:(1/NY):1, -.5:(1/NY):.5); if (N == 2): m = gf.Mesh('regular simplices', np.linspace(-1., 1., NY), np.linspace(-.5, .5, NY)) else: m = gf.Mesh('regular simplices', np.linspace(-1., 1., NY), np.linspace(-.5, .5, NY), np.linspace(-.5, .5, NY)) pts = m.pts() #% Find the boundary GammaD and GammaN pidleft = np.compress((abs(pts[0, :] + 1.0) < 1e-7), list(range(0, m.nbpts()))) fidleft = m.faces_from_pid(pidleft) normals = m.normal_of_faces(fidleft) fidleft = fidleft[:, abs(normals[0, :] + 1.0) < 1e-3] GAMMAD = 2 m.set_region(GAMMAD, fidleft) pidright = np.compress((abs(pts[0, :] - 1.0) < 1e-7), list(range(0, m.nbpts())))
dt = 0.01 T = 3. nu = 0.01 p_in_str = "np.sin(3*{0})" #Mesh and MeshRegion IN_RG = 1 OUT_RG = 2 INOUT_RG = 3 WALL_RG = 4 geotrans = "GT_QK(2,2)" m = gf.Mesh("import", "structured", "GT='%s';ORG=[%f,%f];SIZES=[%f,%f];NSUBDIV=[%i,%i]" % (geotrans, 0, H2, W1, H1, NW1, NH1)) m.merge(gf.Mesh("import", "structured", "GT='%s';ORG=[%f,%f];SIZES=[%f,%f];NSUBDIV=[%i,%i]" % (geotrans, 0, 0, W1, H2, NW1, NH2))) m.merge(gf.Mesh("import", "structured", "GT='%s';ORG=[%f,%f];SIZES=[%f,%f];NSUBDIV=[%i,%i]" % (geotrans, W1, 0, W2, H2, NW2, NH2))) m.optimize_structure() l_rg = m.outer_faces_with_direction([-1,0], np.pi/180) b_rg = m.outer_faces_with_direction([0,-1], np.pi/180) r_rg = m.outer_faces_with_direction([1,0], np.pi/180) t_rg = m.outer_faces_with_direction([0,1], np.pi/180) in_rg = m.outer_faces_in_box([-1e-6,H1+H2-1e-6],[W1+1e-6,H1+H2+1e-6]) out_rg = m.outer_faces_in_box([W1+W2-1e-6,-1e-6],[W1+W2+1e-6,H2+1e-6])
u = 1. / 2. - x / 2. dxu = -1. / 2. dtu = 0 elif zone == 4: u = 1. / 2. - x / 2. dxu = -1. / 2. dtu = 0. return (u if (d == 0) else (dxu if (d == 1) else dtu)) def linsolve(M, B): # Call Superlu to solve a sparse linear system return (((gf.linsolve_superlu(M, B))[0]).T)[0] # Mesh m = gf.Mesh('cartesian', np.arange(0, 1 + 1. / NX, 1. / NX)) # Selection of the contact and Dirichlet boundaries GAMMAC = 1 GAMMAD = 2 border = m.outer_faces() normals = m.normal_of_faces(border) contact_boundary = border[:, np.nonzero(normals[0] < -0.01)[0]] m.set_region(GAMMAC, contact_boundary) contact_boundary = border[:, np.nonzero(normals[0] > 0.01)[0]] m.set_region(GAMMAD, contact_boundary) # Finite element methods mfu = gf.MeshFem(m) mfu.set_classical_fem( u_degree) # Assumed to be a Lagrange FEM in the following
############################################################################ """ Test of the assembly on the direct product of two domains. This program is used to check that Python-GetFEM interface, and more generally GetFEM are working. It focuses on testing some operations of the high generic assembly language. $Id$ """ import numpy as np import getfem as gf import os NX = 4 m1 = gf.Mesh('cartesian', np.arange(0, 1 + 1. / NX, 1. / NX)) # Structured 1D mesh mfu1 = gf.MeshFem(m1, 1) mfu1.set_fem(gf.Fem('FEM_PK(1,1)')) mim1 = gf.MeshIm(m1, gf.Integ('IM_GAUSS1D(4)')) U1 = mfu1.eval('x') m2 = gf.Mesh('triangles grid', np.arange(0, 1 + 1. / NX, 1. / NX), np.arange(0, 1 + 1. / NX, 1. / NX)) # Structured 2D mesh mfu2 = gf.MeshFem(m2, 1) mfu2.set_fem(gf.Fem('FEM_PK(2,1)')) mim2 = gf.MeshIm(m2, gf.Integ('IM_TRIANGLE(4)')) U2 = mfu2.eval('x+y') md = gf.Model('real') md.add_fem_variable('u1', mfu1)