xa = np.arange(0, CUBE_H, MESHSIZE) ya = np.arange(0, CUBE_H, MESHSIZE) za = np.arange(0, CUBE_H, MESHSIZE) m = gf.Mesh('regular simplices', xa, ya, za) #m.export_to_vtk('3dregular_simplices.vtk', 'ascii') # Creation of a simple 3d simplex mesh END ###################################################################### ###################################################################### # Create a MeshFem for b and la mult fields DS = '_DISCONTINUOUS' if DS_GA else "" ALPHA = ',0.5' if DS_GA else "" mfb = gf.MeshFem(m, 3) mfb.set_qdim(3) mfb.set_fem(gf.Fem(f'FEM_PK{DS}(3,1{ALPHA})')) mfdiv_mult = gf.MeshFem(m, 1) mfdiv_mult.set_qdim(1) mfdiv_mult.set_fem( gf.Fem(f'FEM_PK{DS}(3,1{ALPHA})')) # MeshFem FEM_PK FEM_PK_DISCONTINUOUS # Create a MeshFem for b and la mult fields END ###################################################################### # Integration method used mim = gf.MeshIm(m, gf.Integ('IM_TETRAHEDRON(2)')) ######################################################################### # Make it boundary m.set_region(ALL_SIDES, m.outer_faces()) if (DS_GA):
## 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 ttop = abs(fnor[0, :] - 1) < 1e-14 fleft = np.compress(tleft, flst, axis=1) ftop = np.compress(ttop, flst, axis=1) fneum = np.compress(np.logical_not(ttop + tleft), flst, axis=1) # Mark it as boundary
fidright = fidright[:, abs(normals[0, :] - 1.0) < 1e-3] GAMMAN = 3 m.set_region(GAMMAN, fidright) #% Definition of the finite element methods ls = gf.LevelSet(m, ls_degree) mls = gf.MeshLevelSet(m) mls.add(ls) mf_ls = ls.mf() if N == 2: mimls = gf.MeshIm(m, gf.Integ('IM_TRIANGLE(4)')) else: mimls = gf.MeshIm(m, gf.Integ('IM_TETRAHEDRON(5)')) mf_basic = gf.MeshFem(m, N) mf_basic.set_fem(gf.Fem(f'FEM_PK({N},{k})')) mf_g = gf.MeshFem(m, 1) mf_g.set_fem(gf.Fem(f'FEM_PK_DISCONTINUOUS({N},{k-1})')) mf_cont = gf.MeshFem(m, 1) mf_cont.set_fem(gf.Fem(f'FEM_PK({N},{ls_degree})')) print(f'There is {mf_basic.nbdof()} elasticity dofs') Mcont = gf.asm_mass_matrix(mimls, mf_cont) RMcont = np.linalg.cholesky(Mcont.full()) Mcontls = gf.asm_mass_matrix(mimls, mf_ls) RMcontls = np.linalg.cholesky(Mcontls.full()) #% Definition of the initial level-set
# Levelset definition: R1 = 2.5 R2 = 16 ytip = R1 xtip = np.sqrt(R2 * R2 - R1 * R1) ls1 = gf.LevelSet(m, 2, "y-%g*tanh(x/7.)" % R1, "x*x+y*y-%g" % (R2 * R2)) ls2 = gf.LevelSet(m, 2, "y+%g*tanh(x/7.)" % R1, "x*x+y*y-%g" % (R2 * R2)) mls = gf.MeshLevelSet(m) mls.add(ls1) mls.add(ls2) mls.adapt() # Basic mesh_fem without enrichment: mf_pre = gf.MeshFem(m) if (quad): mf_pre.set_fem(gf.Fem("FEM_QK(2,2)")) else: mf_pre.set_fem(gf.Fem("FEM_PK(2,2)")) # Definition of the enriched finite element method (MeshFemLevelSet): mfls = gf.MeshFem("levelset", mls, mf_pre) # Global functions for asymptotic enrichment: mf_part_unity = gf.MeshFem(m) mf_part_unity.set_classical_fem(1) DOFpts = mf_part_unity.basic_dof_nodes() ctip_dofs = [ np.nonzero(np.linalg.norm(DOFpts - x, axis=0) < 0.5)[0] for x in [[[xtip], [-ytip]], [[-xtip], [ytip]], [[xtip], [ytip]], [[-xtip], [-ytip]]] ]
listPrism.append(i) elif str(gt[0])=='GT_QK(3,1)': #print 'index of HEXA: ', i listHexa.append(i) else: print('Geometric transformation: ', gt[0]) print('num Tetra: ', len(listTetra)) print('num Hexa: ', len(listHexa)) print('num Hexa: ', len(listHexa)) print('num Hexa: ', len(listHexa)) mfu = gf.MeshFem(m, 3) mfu.set_fem(gf.Fem('FEM_QK(3,{d})'.format(d=degree)),listHexa) mfu.set_fem(gf.Fem('FEM_PYRAMID_LAGRANGE({d})'.format(d=degree)),listPyramid) mfu.set_fem(gf.Fem('FEM_PK_PRISM(3,{d})'.format(d=degree)),listPrism) mfu.set_fem(gf.Fem('FEM_PK(3,{d})'.format(d=degree)),listTetra) mim = gf.MeshIm(m, 3) # Model md = gf.Model('real') md.add_fem_variable('u',mfu) md.add_initialized_data('mu_para', Mu) md.add_initialized_data('lambda_para', Lambda) md.add_linear_generic_assembly_brick(mim,"lambda_para*Div_u*Div_Test_u + 2*mu_para*Sym(Grad_u):Grad_Test_u") md.add_initialized_data('Fdata',[0.0,-1.0, 0.0]) md.add_source_term_brick(mim, 'u', 'Fdata', NEUMANN_BOUNDARY)
""" 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) CLAMPED_BOUNDARY = 1 m.set_region(CLAMPED_BOUNDARY, fleft) SIMPLE_SUPPORT_BOUNDARY = 2 m.set_region(SIMPLE_SUPPORT_BOUNDARY, fright)
# 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. mfdu.set_fem(gf.Fem('FEM_QK_DISCONTINUOUS(3,2)')); # Display some information about the mesh print('nbcvs=%d, nbpts=%d, nbdof=%d' % (m.nbcvs(), m.nbpts(), mfu.nbdof())) # Assign boundary numbers
""" test user global functions. This program is used to check that python-getfem is working. This is also a good example of use of python-getfem.. $Id$ """ import numpy as np import getfem as gf import os # mesh fem to export m = gf.Mesh('triangles grid', [-1, -0.5, 0, 0.5, 1], [-1, -0.5, 0, 0.5, 1]) # m = gf.Mesh('import','gmsh','quad.msh') mf = gf.MeshFem(m) mf.set_fem(gf.Fem('FEM_PK(2,1)')) PTs = mf.basic_dof_nodes() # crack: ck0 = gf.GlobalFunction('crack', 0) ck1 = gf.GlobalFunction('crack', 1) ck2 = gf.GlobalFunction('crack', 2) ck3 = gf.GlobalFunction('crack', 3) ck4 = gf.GlobalFunction('crack', 4) ck5 = gf.GlobalFunction('crack', 5) ck6 = gf.GlobalFunction('crack', 6) ck7 = gf.GlobalFunction('crack', 7) ck8 = gf.GlobalFunction('crack', 8) ck9 = gf.GlobalFunction('crack', 9) ck10 = gf.GlobalFunction('crack', 10) ck11 = gf.GlobalFunction('crack', 11)
#!/usr/bin/env python # -*- coding: UTF8 -*- # import basic modules import getfem as gf import numpy as np # creation of a simple cartesian mesh m = gf.Mesh('cartesian', np.arange(0,1.1,0.1), np.arange(0,1.1,0.1)) # create a MeshFem of for a field of dimension 1 (i.e. a scalar field) mf = gf.MeshFem(m, 1) # assign the Q2 fem to all convexes of the MeshFem mf.set_fem(gf.Fem('FEM_QK(2,2)')) # view the expression of its basis functions on the reference convex print gf.Fem('FEM_QK(2,2)').poly_str() # an exact integration will be used mim = gf.MeshIm(m, gf.Integ('IM_EXACT_PARALLELEPIPED(2)')) # detect the border of the mesh border = m.outer_faces() # mark it as boundary #42 m.set_region(42, border) # empty real model md = gf.Model('real') # declare that "u" is an unknown of the system # on the finite element method `mf`
L = 100 H = 20 gf.util('trace_level', 1) 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)')) mim_data = gf.MeshImData(mim, -1, [2, 2]) mfu = gf.MeshFem(m, 2) mfxi = gf.MeshFem(m, 1) mfd = gf.MeshFem(m) mfdu = gf.MeshFem(m) mfu.set_fem(gf.Fem('FEM_PK(2,2)')) mfxi.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(2,2)')) mfd.set_fem(gf.Fem('FEM_PK(2,2)')) mfdu.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(2,1)')) Lambda = 121150 Mu = 80769 sigma_y = 4000 P = m.pts() pidleft = np.compress((abs(P[0, :]) < 1e-6), list(range(0, m.nbpts()))) pidright = np.compress((abs(P[0, :] - L) < 1e-6), list(range(0, m.nbpts()))) fleft = m.faces_from_pid(pidleft) fright = m.faces_from_pid(pidright)
t = np.sin(2*np.pi*(np.arange(0, T+DT/2, DT))/10.) + 0.1; # Create the mesh # m = gfMesh('triangles grid', [0:(LX/NX):LX], [0:(LY/NY):LY]) m = gf.Mesh('import','structured', 'GT="GT_PK(2,1)";SIZES=[%d,%d];NOISED=0;NSUBDIV=[%d,%d];' % (LX, LY, NX, NY)) N = m.dim() # Define used MeshIm mim=gf.MeshIm(m, gf.Integ('IM_TRIANGLE(6)')) # Define used MeshFem mf_u=gf.MeshFem(m,2) mf_u.set_fem(gf.Fem('FEM_PK(2,%d)' % order)) if (option == 1): mf_sigma=gf.MeshFem(m,2,2) mf_sigma.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(2,1)')) # mf_xi = gf.MeshFem(m); mf_xi.set_fem(gf.Fem('FEM_PK(2,2)')); mf_xi = gf.MeshFem(m); mf_xi.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(2,1)')) mf_data=gf.MeshFem(m); mf_data.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(2,0)')) mf_vm = gf.MeshFem(m); mf_vm.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(2,1)')) # Find the boundary of the domain P=m.pts() pidleft=np.compress((abs(P[0,:])<1e-6), range(0, m.nbpts())) pidright=np.compress((abs(P[0,:] - LX)<1e-6), range(0, m.nbpts())) fleft = m.faces_from_pid(pidleft)
Dirichlet_with_multipliers = True # Dirichlet condition with multipliers # or penalization dirichlet_coefficient = 1e10 # Penalization coefficient # Create a simple cartesian mesh m = gf.Mesh('pyramidal', np.arange(0, 1 + 1. / NX, 1. / NX), np.arange(0, 1 + 1. / NX, 1. / NX), np.arange(0, 1 + 1. / NX, 1. / NX)) # m = gf.Mesh('regular_simplices', np.arange(0,1+1./NX,1./NX), # 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 Lagrange linear fem to all pyramids of the both MeshFem mfu.set_fem(gf.Fem('FEM_PYRAMID_LAGRANGE(2)')) mfrhs.set_fem(gf.Fem('FEM_PYRAMID_LAGRANGE(2)')) # mfu.set_fem(gf.Fem('FEM_PK(3,1)')) # mfrhs.set_fem(gf.Fem('FEM_PK(3,1)')) if (export_mesh): m.export_to_vtk('mesh.vtk') print('\nYou can view the mesh for instance with') print('mayavi2 -d mesh.vtk -f ExtractEdges -m Surface \n') # Integration method used # mim = gf.MeshIm(m, gf.Integ('IM_PYRAMID_COMPOSITE(IM_TETRAHEDRON(6))')) mim = gf.MeshIm(m, gf.Integ('IM_PYRAMID(IM_GAUSS_PARALLELEPIPED(3,3))')) # mim = gf.MeshIm(m, gf.Integ('IM_TETRAHEDRON(5)')) # Boundary selection
else: m = gf.Mesh('regular_simplices', I, I) # m=gf.Mesh('import','structured','GT="GT_PK(2,1)";SIZES=[1,1];NOISED=1;NSUBDIV=[%d,%d];' % (NX, NX)) N = m.dim(); # Meshfems mfu = gf.MeshFem(m, N) mfgu = gf.MeshFem(m, N, N) mfur = gf.MeshFem(m, N) mfrhs = gf.MeshFem(m, 1) if (using_HHO): if (use_quad): mfu.set_fem(gf.Fem('FEM_HHO(FEM_QUAD_IPK(2,2),FEM_SIMPLEX_CIPK(1,2))')) # mfu.set_fem(gf.Fem('FEM_HHO(FEM_QK_DISCONTINUOUS(2,2,0.1),FEM_SIMPLEX_CIPK(1,2))')) # mfu.set_fem(gf.Fem('FEM_HHO(FEM_QK(2,2),FEM_PK(1,2))')) mfur.set_fem(gf.Fem('FEM_QUAD_IPK(2,3)')) # mfur.set_fem(gf.Fem('FEM_QK(2,3)')) else: mfu.set_fem(gf.Fem('FEM_HHO(FEM_SIMPLEX_IPK(2,2),FEM_SIMPLEX_CIPK(1,2))')) mfur.set_fem(gf.Fem('FEM_PK(2,3)')) else: if (use_quad): mfu.set_fem(gf.Fem('FEM_QK(2,2)')) mfur.set_fem(gf.Fem('FEM_QK(2,2)')) else: mfu.set_fem(gf.Fem('FEM_PK(2,2)')) mfur.set_fem(gf.Fem('FEM_PK(2,2)'))
NX = 20 # Mesh parameter. Dirichlet_with_multipliers = True # Dirichlet condition with multipliers # or penalization dirichlet_coefficient = 1e10 # Penalization coefficient interior_penalty_factor = 1e2 * NX # Parameter of the interior penalty term verify_neighbour_computation = True # 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 discontinuous P2 fem to all convexes of the both MeshFem mfu.set_fem(gf.Fem('FEM_PK_DISCONTINUOUS(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 ttop = abs(fnor[0, :] - 1) < 1e-14 fleft = np.compress(tleft, flst, axis=1) ftop = np.compress(ttop, flst, axis=1) fneum = np.compress(np.logical_not(ttop + tleft), flst, axis=1) # Mark it as boundary
# 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
# In[11]: print mfd # FEM手法として古典的なLagrange要素$P_k$を割り当てます。 # # | degree | dimension | d.o.f. number | class | vectorial | $\tau$-equivalent | Polynomial | # |:--------------------:|:--------------------:|:------------------------------------:|:------------:|:------------------------:|:------------------------:|:------------:| # | $K,0\leq K\leq255$ | $P,1\leq K\leq255$ | $\dfrac{\left(K+P\right)!}{K!P!}$ | $C^0$ | No$\left(Q=1\right)$ | Yes$\left(M=Id\right)$ | Yes | # # In[12]: degree = 1 mfu.set_fem(gf.Fem('FEM_PK(3,%d)' % (degree, ))) mfd.set_fem(gf.Fem('FEM_PK(3,0)')) # 立体求積法として15積分点・5次のtetrahedronを使用します。 # # <img src="getfemlistintmethodtetrahedron5.png"> # # In[13]: mim = gf.MeshIm(m, gf.Integ('IM_TETRAHEDRON(5)')) # # 境界条件の設定 # 最後に境界条件を設定します。今回は三脚の上端にNEUMANN条件を、下端にDIRICHLET条件を設定する。 # In[14]:
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License and GCC Runtime Library Exception for more details. # 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. # ############################################################################ import numpy as np import getfem as gf NX = 10 m = gf.Mesh('cartesian', np.arange(0,1+1./NX,1./NX), np.arange(0,1+1./NX,1./NX)) mf = gf.MeshFem(m,1) # create a meshfem of for a field of dimension 1 mf.set('fem',gf.Fem('FEM_QK(2,2)')) print (gf.Fem('FEM_QK(2,2)').poly_str()) # mim=gf.MeshIm(m, gf.Integ('IM_EXACT_PARALLELEPIPED(2)')); // not allowed mim=gf.MeshIm(m, gf.Integ('IM_GAUSS_PARALLELEPIPED(2, 4)')); border = m.outer_faces() m.set_region(42, border) # create the region B42 (:- md=gf.Model('real') md.add_fem_variable('u', mf) md.add_Laplacian_brick(mim, 'u') R = mf.eval('(x-.5)*(x-.5) + (y-.5)*(y-.5) + x/5 - y/3')
ckoff2 = ck2 * coff ckoff3 = ck3 * coff # Levelset definition: ls = gf.LevelSet(m, 1, 'y', 'x') mls = gf.MeshLevelSet(m) mls.add(ls) if variant > 2: ls2 = gf.LevelSet(m, 1, 'x+0.125', 'abs(y)-0.375') mls.add(ls2) mls.adapt() # Basic mesh_fem without enrichment: mf_pre_u = gf.MeshFem(m) mf_pre_u.set_fem(gf.Fem('FEM_PK(2,1)')) # Definition of the enriched finite element method (MeshFemLevelSet): mfls_u = gf.MeshFem('levelset', mls, mf_pre_u) if variant == 1: # Cutoff enrichement # MeshFemGlobalFunction: mf_sing_u = gf.MeshFem('global function', m, ls, [ckoff0, ckoff1, ckoff2, ckoff3], 1) # MeshFemDirectSum: mf_u = gf.MeshFem('sum', mf_sing_u, mfls_u) else: mf_sing_u = gf.MeshFem('global function', m, ls, [ck0, ck1, ck2, ck3], 1) mf_part_unity = gf.MeshFem(m) mf_part_unity.set_classical_fem(1) DOFpts = mf_part_unity.basic_dof_nodes()
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)')) # ==== 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(' ====================================') # ==== Boundaries detection ====
ls2.set_values(ULS2, ULS2s) # '-y-x+.2') # '(y-.2)^2 - 0.04') mls = gf.MeshLevelSet(m) mls.add(ls) mls.add(ls2) mls.adapt() mls.cut_mesh().export_to_pos('ver.pos') mim_bound = gf.MeshIm('levelset', mls, 'boundary(a+b)', gf.Integ('IM_TRIANGLE(6)')) #, gf.Integ('IM_QUAD(5)')) mim = gf.MeshIm('levelset', mls, 'all(a+b)', gf.Integ('IM_TRIANGLE(6)')) mim.set_integ(4) mfu0 = gf.MeshFem(m, 2) mfu0.set_fem(gf.Fem('FEM_QK(2,3)')) mfdu = gf.MeshFem(m, 1) mfdu.set_fem(gf.Fem('FEM_QK_DISCONTINUOUS(2,2)')) mf_mult = gf.MeshFem(m, 2) mf_mult.set_fem(gf.Fem('FEM_QK(2,1)')) A = gf.asm('volumic', 'V()+=comp()', mim_bound) #mls.cut_mesh().export_to_pos('mls.pos','cut mesh') #mf_ls.export_to_pos('mf_ls.pos',ULS,'ULS') dof_out = mfu0.dof_from_im(mim) cv_out = mim.convex_index() cv_in = setdiff1d(m.cvid(), cv_out)
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) md.set_variable('u1', U1) md.add_fem_variable('u2', mfu2)
is_in_circle = (P[0, :] - xc)**2 + (P[1, :] - yc)**2 <= radius**2 areap = np.zeros(idx.size - 1) for cv in range(idx.size - 1): if all(is_in_circle[pid[idx[cv]:idx[cv + 1]]]): areap[cv] = 1 mfu = gf.MeshFem(m, 2) mfd = gf.MeshFem(m, 1) mfm = gf.MeshFem(m, 2) mfdu = gf.MeshFem(m) mim = gf.MeshIm(m, 5) mfu.set_fem(gf.Fem('FEM_QK(2,2)')) mfd.set_fem(gf.Fem('FEM_QK(2,1)')) mfm.set_fem(gf.Fem('FEM_QK(2,2)')) mfdu.set_fem(gf.Fem('FEM_QK_DISCONTINUOUS(2,2)')) mfu.set_dof_partition(areap) b_in = m.outer_faces(np.nonzero(areap == 1)) b_out = m.outer_faces(np.nonzero(areap == 0)) b_border = m.outer_faces() b_out = np.array( tuple( set(tuple(r) for r in b_out.transpose()) - set(tuple(r) for r in b_border.transpose()))).transpose() fleft = m.faces_from_pid(np.nonzero(abs(P[1]) < 1e-6))
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) pidtop = np.compress(ctop, range(0, m.nbpts())) pidbot = np.compress(cbot, range(0, m.nbpts())) ftop = m.faces_from_pid(pidtop) fbot = m.faces_from_pid(pidbot) print "create boundary region" NEUMANN_BOUNDARY = 1
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
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 Mu = 80769 von_mises_threshold = 4000 P = m.pts() pidleft = np.compress((abs(P[0, :]) < 1e-6), range(0, m.nbpts())) pidright = np.compress((abs(P[0, :] - L) < 1e-6), range(0, m.nbpts())) fleft = m.faces_from_pid(pidleft) fright = m.faces_from_pid(pidright)
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
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) mesh.set_region(AUX_BOUND2, fb4) mesh.region_subtract(LEFT_BOUND, AUX_BOUND2) mesh.region_subtract(BOTTOM_BOUND, AUX_BOUND1) # Create a MeshFem for u and rhs fields of dimension 1 (i.e. a scalar field) mfu = gf.MeshFem(mesh, 1) mfP0 = gf.MeshFem(mesh, 1) # Assign the discontinuous P2 fem to all convexes of the both MeshFem mfu.set_fem(gf.Fem('FEM_PK(2,2)')) mfP0.set_fem(gf.Fem('FEM_PK(2,0)')) # Integration method used mim = gf.MeshIm(mesh, gf.Integ('IM_TRIANGLE(4)')) # Inner edges for the computation of the normal derivative jump in_faces = mesh.inner_faces() INNER_FACES = 18 mesh.set_region(INNER_FACES, in_faces) # Model md = gf.Model('real') # Main unknown md.add_fem_variable('u', mfu)
# 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: mfu.set_fem(gf.Fem('FEM_PK(%d,2)' % N)) mfur.set_fem(gf.Fem('FEM_PK(%d,2)' % N)) mfgu.set_fem(gf.Fem('FEM_PK(%d,2)' % N)) mfrhs.set_fem(gf.Fem('FEM_PK(%d,2)' % N)) print('nbdof : %d' % mfu.nbdof()) # Integration method used if (N == 2): mim = gf.MeshIm(m, gf.Integ('IM_TRIANGLE(4)')) else: mim = gf.MeshIm(m, gf.Integ('IM_TETRAHEDRON(5)'))
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' % \ (m.nbcvs(), m.nbpts(), mfu.qdim(), mfu.fem()[0].char(), mfu.nbdof()) # detect some boundary of the mesh P = m.pts() ctop = (abs(P[1, :] - 13) < 1e-6) cbot = (abs(P[1, :] + 10) < 1e-6) pidtop = np.compress(ctop, range(0, m.nbpts())) pidbot = np.compress(cbot, range(0, m.nbpts()))
import getfem as gf import numpy as np # parameters NX=30; ls_degree = 1; alpha = 1; beta = 1; rayon_trous = 0.2; m = gf.Mesh('cartesian', np.arange(-.5,.5+1./NX,1./NX), np.arange(-.5,.5+1./NX,1./NX)) mf_basic = gf.MeshFem(m, 1); mf_basic.set_fem(gf.Fem('FEM_QK(2,2)')) mls = gf.MeshLevelSet(m) ls = gf.LevelSet(m, ls_degree) mls.add(ls) mf_ls = ls.mf() P = mf_ls.basic_dof_nodes() x = P[0] y = P[1] ULS = 1000*np.ones(x.shape) #% Loop on the topological optimization #while True: for col in range(1000): ls.set_values(ULS) mls.adapt() mim_bound = gf.MeshIm('levelset',mls,'boundary', gf.Integ('IM_TRIANGLE(6)')) mim = gf.MeshIm('levelset',mls,'outside', gf.Integ('IM_TRIANGLE(6)')) mim.set_integ(4)