Exemple #1
0
 def deformation_from_mf(self, mf, U, scale):
     P = self.sl.pts()
     deform = getfem.compute(mf, U, 'interpolate on', self.sl)
     try:
         scale = float(scale)
     except ValueError:
         if scale.endswith('%'):
             a = max(abs(P.max()), abs(P.min()), 1e-10)
             b = max(abs(deform.max()), abs(deform.min()))
             scale = float(scale[:-1]) * 0.01 * a / b
     P = P + scale * deform
     self.sl.set_pts(P)
     #print "deformation!", repr(mf), U.size(), repr(self.sl), "\nDEFORM=",self.deform,"\n"
     sys.stdout.flush()
Exemple #2
0
 def deformation_from_mf(self, mf, U, scale):
     P=self.sl.pts()
     deform = getfem.compute(mf, U, 'interpolate on', self.sl)
     try:
         scale=float(scale)
     except ValueError:
         if scale.endswith('%'):
             a = max(abs(P.max()),abs(P.min()),1e-10);
             b = max(abs(deform.max()),abs(deform.min()));
             scale = float(scale[:-1]) * 0.01 * a/b;
     P=P + scale * deform
     self.sl.set_pts(P)
     #print "deformation!", repr(mf), U.size(), repr(self.sl), "\nDEFORM=",self.deform,"\n"
     sys.stdout.flush()
Exemple #3
0
 def dfield_on_slice(self, data):
     mf = None
     if (isinstance(data, tuple)):
         if (len(data) == 2):
             mf = data[0]
             U = data[1]
         elif (len(data) == 1):
             U = data[0]
         else:
             raise Exception("wrong data tuple..")
     else:
         U = data
     if mf is not None:
         return getfem.compute(mf, U, 'interpolate on', self.sl)
     else:
         return U
Exemple #4
0
 def dfield_on_slice(self, data):
     mf = None
     if (isinstance(data, tuple)):
         if (len(data) == 2):
             mf = data[0]
             U = data[1]
         elif (len(data) == 1):
             U = data[0]
         else:
             raise Exception("wrong data tuple..")
     else:
         U = data
     if mf is not None:
         return getfem.compute(mf, U, 'interpolate on', self.sl);
     else:
         return U
    md.solve('max_res', 1e-7)
    U = md.variable("u")
    nbd = mf_ls.nbdof()

    #% Computation of indicators (computation of K could be avoided)
    K = gf.asm('linear elasticity', mim, mf, mf_ls, λ * np.ones((1, nbd)),
               μ * np.ones((1, nbd)))
    print(f'Elastic energy at iteration {niter}: {np.dot(U,(K*U))}')
    S = gf.asm('volumic', 'V()+=comp()', mim)
    if (N == 2):
        print(f'Remaining surface of material: {S}')
    else:
        print(f'Remaining volume of material: {S}')

    DU = gf.compute(mf, U, 'gradient', mf_g)
    EPSU = DU + DU[[1, 0], :]

    #% Computation of the shape derivative
    if (N == 2):
        GF1 = (DU[0][0] + DU[1][1])**2 * λ + 2 * μ * np.sum(
            np.sum(EPSU, axis=0), axis=0)
    else:
        GF1 = (DU[0][0] + DU[1][1] + DU[2][2])**2 * λ + 2 * μ * np.sum(
            np.sum(EPSU, axis=0), axis=0)
    GF = GF1.reshape((1, len(GF1))) - threshold_shape

    #% computation of the topological gradient
    if (N == 2):
        GT = -np.pi * ((λ + 2 * μ) / (2 * μ * (λ + μ)) *
                       (4 * μ * GF1 + 2 * (λ - μ) * (λ + μ) *
Exemple #6
0
                                                DIRICHLET_BOUNDARY_NUM2,
                                                'DirichletData')
else:
    md.add_Dirichlet_condition_with_penalization(mim, 'u',
                                                 dirichlet_coefficient,
                                                 DIRICHLET_BOUNDARY_NUM2,
                                                 'DirichletData')
gf.memstats()
# md.listvar()
# md.listbricks()

# Assembly of the linear system and solve.
md.solve()

# Main unknown
U = md.variable('u')
L2error = gf.compute(mfu, U - Ue, 'L2 norm', mim)
H1error = gf.compute(mfu, U - Ue, 'H1 norm', mim)
print('Error in L2 norm : ', L2error)
print('Error in H1 norm : ', H1error)

# Export data
mfu.export_to_pos('laplacian.pos', Ue, 'Exact solution', U,
                  'Computed solution')
print('You can view the solution with (for example):')
print('gmsh laplacian.pos')

if (H1error > 1e-3):
    print('Error too large !')
    exit(1)
Exemple #7
0
    md.add_Dirichlet_condition_with_penalization(mim, 'u',
                                                 dirichlet_coefficient, top,
                                                 'g')

# Dirichlet condition on the right
if (Dirichlet_with_multipliers):
    md.add_Dirichlet_condition_with_multipliers(mim, 'u', mfu, right, 'g')
else:
    md.add_Dirichlet_condition_with_penalization(mim, 'u',
                                                 dirichlet_coefficient, right,
                                                 'g')

# md.listvar()
# md.listbricks()

# assembly of the linear system and solve.
md.solve()

# main unknown
u = md.variable('u')
L2error = gf.compute(mfu, u - g, 'L2 norm', mim)
H1error = gf.compute(mfu, u - g, 'H1 norm', mim)

if (H1error > 1e-3):
    print 'Error in L2 norm : ', L2error
    print 'Error in H1 norm : ', H1error
    print 'Error too large !'

# export data
mfu.export_to_pos('sol.pos', g, 'Exact solution', u, 'Computed solution')
Exemple #8
0
    colorbar;
    title('u');
    hold off;
    """
    
    # % Solving the adjoint problem.
    UBASIC = gf.compute_interpolate_on(mf, U, mf_basic)
    F = 2*UBASIC
    md.set_variable('VolumicData', F)
    md.solve()
    W = md.variable("u")

    # % Computation of the topological gradient
    mf_g = gf.MeshFem(m, 1)
    mf_g.set_fem(gf.Fem('FEM_PRODUCT(FEM_PK_DISCONTINUOUS(1,2),FEM_PK_DISCONTINUOUS(1,2))'))
    DU = gf.compute(mf, U, 'gradient', mf_g);
    DW = gf.compute(mf, W, 'gradient', mf_g);
    nbdof = mf_g.nbdof()
    DU = np.reshape(DU, [2, nbdof])
    DW = np.reshape(DW, [2, nbdof])
    
    UU = gf.compute_interpolate_on(mf, U, mf_g)
    UU0 = gf.compute_interpolate_on(mf_basic, U0, mf_g)
    LS = gf.compute_interpolate_on(mf_ls, ULS, mf_g)

    G = (-4*np.pi*( alpha*(DU[0]**2 + DU[1]**2 + DU[0]*DW[0] + DU[1]*DW[1]) + beta*(UU-UU0)**2)) * (np.sign(LS)+1.)/2;

    val = G.min()
    i   = G.argmin()
    point = mf_g.basic_dof_nodes(i)
    print('val =',val)