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 if (initial_holes): if (N == 2): ULS = mf_basic.eval( '(-0.6-np.sin(np.pi*4*x)*np.cos(np.pi*4*y))/(4*np.pi)', globals(), locals()) else: ULS = mf_basic.eval( '(-0.6-np.sin(np.pi*4*x)*np.cos(np.pi*4*y)*np.cos(np.pi*4*z))/(4*np.pi)', globals(), locals()) else:
ftop = m.faces_from_pid(pidtop) fbot = m.faces_from_pid(pidbot) print "create boundary region" NEUMANN_BOUNDARY = 1 DIRICHLET_BOUNDARY = 2 m.set_region(NEUMANN_BOUNDARY,ftop) m.set_region(DIRICHLET_BOUNDARY,fbot) print "create a MeshFem object" mfd = gf.MeshFem(m, 1) # data print "assign the FEM" mfd.set_fem(gf.Fem('FEM_QK(3,3)')) print "assembly" nbd = mfd.nbdof() K = gf.asm_linear_elasticity(mim, mfu, mfd, np.repeat([Lambda], nbd), np.repeat([Mu], nbd)) M = gf.asm_mass_matrix(mim, mfu)*rho K.save('hb', 'K.hb') M.save('hb', 'M.hb') print "solve" A = io.hb_read('M.hb') B = io.hb_read('K.hb') K = B.todense() np.savetxt("K.txt", K, fmt=' (%+.18e) ')
H=2./3.*h2)) _K_ = "{0:.17g}".format(K) # Bulk modulus _mu_ = "{0:.17g}".format(mu) # Shear modulus ibplast = \ md.add_finite_strain_elastoplasticity_brick\ (mim, "Simo_Miehe", "displacement_and_plastic_multiplier_and_pressure", "u", "ksi", "p", "gamma0", "invCp0", _K_, _mu_, "hardening_law") print('Displacement dofs: %i' % mfu.nbdof()) print('Total model dofs: %i' % md.nbdof()) dirichlet_multname = md.mult_varname_Dirichlet(ibdir) mf_dirichlet_mult = md.mesh_fem_of_variable(dirichlet_multname) mass_mat = gf.asm_mass_matrix(mim, mfout) shutil.copyfile(os.path.abspath(sys.argv[0]), resultspath + '/' + sys.argv[0]) starttime_overall = time.process_time() with open('%s/finite_strain_plasticity_forces.dat' % resultspath, 'w') as f: for step in range(0, steps + 1): print('Solving step: %i' % step) backup = md.from_variables() dirichlet_data = mf_dirichlet_mult.eval(dirichlet_str.format(step), globals(), locals()) md.set_variable('dirichlet_data', dirichlet_data) starttime = time.process_time() iters = 41 nit = \
md = gf.Model("real") md.add_fem_variable("v", mfv) md.add_fem_data("v0", mfv) md.add_fem_variable("p", mfp) md.add_fem_data("p_in", mfp_) md.add_initialized_data("f", [0,0]) md.add_initialized_data("dt", [dt]) md.add_initialized_data("nu", [nu]) md.add_Dirichlet_condition_with_multipliers(mim, "p", mfp, IN_RG, "p_in") md.add_nonlinear_term\ (mim, "1/dt*(v-v0).Test_v + (Grad_v0*v0).Test_v + nu*Grad_v:Grad_Test_v - f.Test_v") md.add_nonlinear_term\ (mim, "Grad_p.Grad_Test_p + 1/dt*Trace(Grad_v)*Test_p") mmat_v = gf.asm_mass_matrix(mim, mfv) #mmat_v = gf.asm_generic(mim, 2, "Test2_v.Test_v", -1, "v", 1, mfv, np.zeros(mfv.nbdof())) IV = md.interval_of_variable("v") t = 0 step = 0 while t < T+1e-8: print("Solving step at t=%f" % t) md.set_variable\ ("p_in", mfp_.eval(p_in_str.format(t), globals(), locals()).flatten("F")) md.set_variable("v0", md.variable("v")) md.solve("noisy", "lsolver", "mumps", "max_res", 1e-8) vv = (gf.asm_generic(mim, 1, "(v-dt*Grad_p).Test_v", -1, md))[IV[0]:IV[0]+IV[1]] md.set_variable("v", gf.linsolve_mumps(mmat_v, vv)) mfv.export_to_vtk("results_%i.vtk" % step,
fbot = m.faces_from_pid(pidbot) print "create boundary region" NEUMANN_BOUNDARY = 1 DIRICHLET_BOUNDARY = 2 m.set_region(NEUMANN_BOUNDARY, ftop) m.set_region(DIRICHLET_BOUNDARY, fbot) print "create a MeshFem object" mfd = gf.MeshFem(m, 1) # data print "assign the FEM" mfd.set_fem(gf.Fem('FEM_QK(3,3)')) print "assembly" nbd = mfd.nbdof() K = gf.asm_linear_elasticity(mim, mfu, mfd, np.repeat([Lambda], nbd), np.repeat([Mu], nbd)) M = gf.asm_mass_matrix(mim, mfu) * rho K.save('hb', 'K.hb') M.save('hb', 'M.hb') print "solve" A = io.hb_read('M.hb') B = io.hb_read('K.hb') K = B.todense() np.savetxt("K.txt", K, fmt=' (%+.18e) ')
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
mfd.set_classical_fem(elements_degree) mfds.append(mfd) mim = gf.MeshIm(mesh, elements_degree * 2) mims.append(mim) ############################################################################### # We get the mass and stiffness matrices using asm function. # mass_matrixs = [] linear_elasticitys = [] for i, (mfu, mfd, mim) in enumerate(zip(mfus, mfds, mims)): mass_matrix = gf.asm_mass_matrix(mim, mfu, mfu) mass_matrix.scale(rho) mass_matrixs.append(mass_matrix) lambda_d = np.repeat(clambda, mfd.nbdof()) mu_d = np.repeat(cmu, mfd.nbdof()) linear_elasticity = gf.asm_linear_elasticity(mim, mfu, mfd, lambda_d, mu_d) linear_elasticitys.append(linear_elasticity) mass_matrix.save("hb", "M" + "{:02}".format(i) + ".hb") linear_elasticity.save("hb", "K" + "{:02}".format(i) + ".hb") print("Time for assemble matrix", time.process_time() - t) t = time.process_time() ###############################################################################
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
ldof = mflambda.dof_on_region(GAMMAC) mflambda_partial = gf.MeshFem('partial', mflambda, ldof) md.add_fem_variable('lambda_n', mflambda_partial) md.add_initialized_data('r', [r]) OBS = mfd.eval(obstacle) md.add_initialized_fem_data('obstacle', mfd, OBS) md.add_integral_contact_with_rigid_obstacle_brick(mim_friction, 'u', 'lambda_n', 'obstacle', 'r', GAMMAC, version - 4) elif version == 9: # The integral version, Uzawa on the augmented Lagrangian ldof = mflambda.dof_on_region(GAMMAC) mflambda_partial = gf.MeshFem('partial', mflambda, ldof) nbc = mflambda_partial.nbdof() M = gf.asm_mass_matrix(mim, mflambda_partial, mflambda_partial, GAMMAC) lambda_n = np.zeros(nbc) md.add_initialized_fem_data('lambda_n', mflambda_partial, lambda_n) md.add_initialized_data('r', [r]) OBS = mfd.eval(obstacle) # np.array([mfd.eval(obstacle)]) md.add_initialized_fem_data('obstacle', mfd, OBS) md.add_penalized_contact_with_rigid_obstacle_brick \ (mim_friction, 'u', 'obstacle', 'r', GAMMAC, 2, 'lambda_n') for ii in range(100): print('iteration %d' % (ii + 1)) md.solve('max_res', 1E-9, 'max_iter', niter) U = md.get('variable', 'u') lambda_n_old = lambda_n sol = gf.linsolve_superlu( M,
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) mf_mult = gf.MeshFem(m) mf_mult.set_fem(gf.Fem('FEM_QK(2,1)')) M = gf.asm_mass_matrix(mim, mf_basic) D = np.abs(M.diag().T[0]) ind = np.argwhere(D>1e-8) mf = gf.MeshFem('partial', mf_basic, ind) S = gf.asm('volumic','V()+=comp()',mim) print('remaining surface :',S) # % Problem definition (Laplace(u) + u = f) md = gf.Model('real') md.add_fem_variable('u', mf) md.add_Laplacian_brick(mim, 'u') md.add_fem_data('VolumicData', mf_basic) md.add_source_term_brick(mim, 'u', 'VolumicData') md.add_initialized_data('rho', [1.0]) md.add_mass_brick(mim, 'u', 'rho')