Mcont = gf.asm_mass_matrix(mimls, mf_cont)
    #D = np.abs(np.diag(np.diag(Mcont.full())))
    #ind = np.argwhere(D > ((1/NY)**N)*1e-7)
    D = np.abs(Mcont.diag().T[0])
    ind = np.argwhere(D > ((1 / NY)**N) * 1e-7)
    mf = gf.MeshFem('partial', mf_basic, ind)

    #% Solving the direct problem
    print('solving the direct problem')

    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:
Exemple #2
0
        gf.Integ("IM_STRUCTURED_COMPOSITE(IM_GAUSS_PARALLELEPIPED(2,6),9)"),
        gf.Integ("IM_GAUSS_PARALLELEPIPED(2,4)"))
else:
    mim = gf.MeshIm(
        "levelset", mls, "all",
        gf.Integ("IM_STRUCTURED_COMPOSITE(IM_TRIANGLE(6),3)"),
        gf.Integ("IM_STRUCTURED_COMPOSITE(IM_GAUSS_PARALLELEPIPED(2,6),9)"),
        gf.Integ("IM_STRUCTURED_COMPOSITE(IM_TRIANGLE(6),5)"))

mim_bound = [
    gf.MeshIm("levelset", mls, boundary,
              gf.Integ("IM_STRUCTURED_COMPOSITE(IM_TRIANGLE(6),3)"))
    for boundary in ["boundary(a)", "boundary(b)"]
]

surf_crack = gf.asm("generic", mim_bound[0], 0, "1", -1)\
            +gf.asm("generic", mim_bound[1], 0, "1", -1)
print("surf_crack = %g" % surf_crack)

# Model definition:
md = gf.Model("real")
md.add_fem_variable("u", mf_u)
md.add_fem_variable("theta", mf_theta)
md.add_variable("P", 1)
md.add_variable("T", 1)
md.set_variable("T", 273 + room_temp)
# Data
md.add_initialized_data("surf_crack", surf_crack)
md.add_initialized_data("mu0", E0 / (2 * (1 + nu)))
md.add_initialized_data("la0", E0 * nu / ((1 - 2 * nu) * (1 + nu)))
md.add_initialized_data("ka0", E0 / (3 * (1 - 2 * nu)))
Exemple #3
0
    print('mayavi2 -d mesh%d.vtk -f ExtractEdges -m Surface \n' % refiter)

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

    # Main unknown
    U = md.variable('u')

    # Residual a posteriori estimator

    grad_jump = '( (Grad_u-Interpolate(Grad_u,neighbor_element)).Normal )'

    bulkresidual = 'sqr(element_size*Trace(Hess_u))*Test_psi'
    edgeresidual = '0.25*element_size*sqr(%s)*(Test_psi + Interpolate(Test_psi,neighbor_element))' % grad_jump

    ETA1tmp = gf.asm('generic', mim, 1, bulkresidual, -1, md, 'psi', 1, mfP0,
                     np.zeros(mfP0.nbdof()))
    ETA1 = ETA1tmp[ETA1tmp.size - mfP0.nbdof():ETA1tmp.size]
    ETA2tmp = gf.asm('generic', mim, 1, edgeresidual, INNER_FACES, md, 'psi',
                     1, mfP0, np.zeros(mfP0.nbdof()))
    ETA2 = ETA2tmp[ETA2tmp.size - mfP0.nbdof():ETA2tmp.size]
    ETA = np.sqrt(ETA1 + ETA2)

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

    mfP0.export_to_pos('eta1_%d.pos' % refiter, ETA1, 'Bulk residual')
    print('You can view eta1 with (for example):')
    print('gmsh eta1_%d.pos' % refiter)
Exemple #4
0
# Two Dirichlet brick in order to test the multiplier
# selection in the intersection.
if (Dirichlet_with_multipliers):
    md.add_Dirichlet_condition_with_multipliers(mim, 'u', mfu,
                                                DIRICHLET_BOUNDARY_NUM2, 'Ue')
else:
    md.add_Dirichlet_condition_with_penalization(mim, 'u',
                                                 dirichlet_coefficient,
                                                 DIRICHLET_BOUNDARY_NUM2, 'Ue')

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

# Error computation
U = md.variable('u')
L2error = gf.asm('generic', mim, 0, 'sqr(u-Ue)', -1, md)
H1error = gf.asm('generic', mim, 0, 'Norm_sqr(Grad_u-Grad_Ue)', -1, md)
H1error = np.sqrt(L2error + H1error)
L2error = np.sqrt(L2error)
print('Error in L2 norm (without reconstruction): %g' % L2error)
print('Error in H1 norm (without reconstruction): %g' % H1error)
if (using_HHO):
    L2error = gf.asm('generic', mim, 0, 'sqr(HHO_Val_u-Ue)', -1, md)
    H1error = gf.asm('generic', mim, 0, 'Norm_sqr(HHO_Grad_u-Grad_Ue)', -1, md)
    H1error = np.sqrt(L2error + H1error)
    L2error = np.sqrt(L2error)
    print('Error in L2 norm (with reconstruction): %g' % L2error)
    print('Error in H1 norm (with reconstruction): %g' % H1error)

# Export data
# mfur.export_to_pos('laplacian_e.pos', Ue, 'Exact solution')
             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)
md.set_variable('u2', U2)

md.add_standard_secondary_domain('secdom', mim2)

result = gf.asm('generic', mim1, 0, "u1*Secondary_Domain(u2)", -1, md,
                "Secondary_domain", "secdom")
if (abs(result - 0.5) > 1e-8):
    print("Bad value")
    exit(1)

result = gf.asm('generic', mim1, 0, "u1*(Secondary_Domain(Grad_u2)(1))", -1,
                md, "Secondary_domain", "secdom")
if (abs(result - 0.5) > 1e-8):
    print("Bad value")
    exit(1)

result = gf.asm('generic', mim1, 0,
                "u1*(Secondary_Domain(X)(1)+Secondary_Domain(X)(2))", -1, md,
                "Secondary_domain", "secdom")
if (abs(result - 0.5) > 1e-8):
    print("Bad value")
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)

# mfu = gf.MeshFem('partial', mfu0, dof_out, cv_in)

md = gf.Model('real')
md.add_fem_variable('u', mfu0)
md.add_initialized_data('lambda', [1])
md.add_initialized_data('mu', [1])
md.add_isotropic_linearized_elasticity_brick(mim, 'u', 'lambda', 'mu')
# 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 (verify_neighbour_computation):
    A = gf.asm('generic', mim, 1, 'u*Test_u*(Normal.Normal)', TEST_FACES, md)
    B = gf.asm(
        'generic', mim, 1,
        '-Interpolate(u,neighbour_elt)*Interpolate(Test_u,neighbour_elt)*(Interpolate(Normal,neighbour_elt).Normal)',
        TEST_FACES, md)
    err_v = np.linalg.norm(A - B)
    A = gf.asm('generic', mim, 1, '(Grad_u.Normal)*(Grad_Test_u.Normal)',
               TEST_FACES, md)
    B = gf.asm(
        'generic', mim, 1,
        '(Interpolate(Grad_u,neighbour_elt).Normal)*(Interpolate(Grad_Test_u,neighbour_elt).Normal)',
        TEST_FACES, md)
    err_v = err_v + np.linalg.norm(A - B)
    if (err_v > 1E-13):
        print('Test on neighbour element computation: error to big: ', err_v)
        exit(1)
Bexact = mdmag.interpolation(Bexact_exp, intonme)
intonme.export_to_vtk('field_exact3d.vtk', 'ascii', Bexact)
print('exact B norm l2', gf.compute_L2_norm(intonme, Bexact, mim))
#Interpolate and export exact fields END
######################################################################################################################

WITH_B_CROSS_N = True

# Main potential unknown
mdmag.add_fem_variable('b', mfb)
# lagrange augmentation unknown for potential
mdmag.add_fem_variable('p', mfdiv_mult)

tm = time.time()
# building (curl b, curl \tau) term
CURL_B = gf.asm('generic', mim, 2, curl_testb_curl_b_exp, -1, 'b', 1, mfb, 0)
tm = time.time() - tm
print("CURL.CURL elapsed time", tm)

# (curl b, curl \tau) adding to the model
magi = mdmag.add_explicit_matrix('b', 'b', CURL_B)

#####################################################################################################
# Interior penalty terms
if (DS_GA):
    J_CROSSTAU_DOT_M_CURLB = gf.asm(
        'generic', mim, 2, '({K}).{L}'.format(K=cross_jump_test_b,
                                              L=mean_curl_b), INNER_FACES, 'b',
        1, mfb, 0)
    #J_CROSSB_DOT_M_CURLTAU=gf.asm('generic', mim, 2, '({K}).{L}'.format(K=cross_jump_b, L=mean_curl_test_b), INNER_FACES, 'b', 1, mfb, 0)
    CROSS_CROSS_IP_EXP = "({alpha})*({L}).({M})".format(alpha=ALPHAF,
Exemple #9
0
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')
    md.add_multiplier('mult_dir', mf_mult, 'u')
    # % To be completely robust, a stabilization should be used on the Dirichlet
    # % boundary to ensure the inf-sup condition (Nitsche or Barbosa-Hughes)
    md.add_Dirichlet_condition_with_multipliers(mim_bound, 'u', 'mult_dir', -1)
Exemple #10
0
V = mfv.eval('[x*y, x*y, x*y]')
W = mfw.eval('[x*y, x*y]')


md = gf.Model('real')

md.add_fem_variable('u', mfu)
md.set_variable('u', U)
md.add_fem_variable('v', mfv)
md.set_variable('v', V)
md.add_fem_variable('w', mfw)
md.set_variable('w', W)


# Simple test on the integral of u
result = gf.asm('generic', mim, 0, "u", -1, md)
if (abs(result-1) > 1e-8) : print("Bad value"); exit(1)

# Single contraction and comparison with Trace
result1 = gf.asm('generic', mim, 0,
                 "Def P(a):=a*(a'); Contract(P(Grad_v), 1, 2)", -1, md)
result2 = gf.asm('generic', mim, 0,
                 "Def P(a):=a*(a'); Trace(P(Grad_v))", -1, md)
if (abs(result1-result2) > 1e-8) : print("Bad value"); exit(1)

# Constant order 3 tensor contraction test
result1 = gf.asm('generic', mim, 0,
                 "Contract([[[1,1],[2,2]],[[1,1],[2,2]]], 1, 2)", -1, md)
result2 = np.array([3., 3.]);
if (np.linalg.norm(result1-result2) > 1e-8) : print("Bad value"); exit(1)