homog2d_test.py 6.15 KB
Newer Older
1 2 3 4 5 6 7
# coding: utf-8
"""
Created on 09/01/2019
@author: baptiste

"""

8 9
import logging
import time
10

11
import dolfin as fe
12 13 14 15 16 17
import gmsh
import meshio
import numpy as np
from pytest import approx

from ho_homog import geometry, homog2d, materials, mesh_generate, part
18 19 20 21

logger = logging.getLogger("Test_homog2d")
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
22 23
    "%(asctime)s :: %(levelname)s :: %(name)s :: %(message)s", "%H:%M:%S"
)
24 25 26 27
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
28

29
np.set_printoptions(suppress=False, floatmode="fixed", precision=8, linewidth=150)
30

31

32
def test_homog_EGG_pantograph_1x1(generate_mesh=False):
33 34
    logger.debug("Start test_homog_EGG_pantograph_1x1")
    start = time.time()
35 36
    if generate_mesh:
        geometry.init_geo_tools()
37
        geometry.set_gmsh_option("Mesh.MshFileVersion", 4.1)
38
        a = 1
39
        b, k = a, a / 3
40
        r = 0.02
41 42 43 44 45 46
        panto_test = mesh_generate.pantograph.pantograph_RVE(
            a, b, k, r, nb_cells=(1, 1), soft_mat=False, name="panto_rve_1x1"
        )
        lc_ratio = 1 / 6
        lc_min_max = (lc_ratio * r * a, lc_ratio * a)
        panto_test.main_mesh_refinement((2 * r * a, a), lc_min_max, False)
47
        panto_test.mesh_generate()
48 49 50
        gmsh.model.mesh.renumberNodes()
        gmsh.model.mesh.renumberElements()
        gmsh.write("panto_rve_1x1.msh")
51 52 53
        mesh = meshio.read("panto_rve_1x1.msh")
        mesh.points = mesh.points[:, :2]
        geo_only = meshio.Mesh(
54 55
            points=mesh.points, cells={"triangle": mesh.cells["triangle"]}
        )
56
        meshio.write("panto_rve_1x1.xdmf", geo_only)
57
        geometry.reset()
58

59 60 61 62
    E, nu = 1.0, 0.3
    material = materials.Material(E, nu, "cp")
    gen_vect = np.array([[4.0, 0.0], [0.0, 8.0]])
    rve = part.Fenics2DRVE.rve_from_file("panto_rve_1x1.xdmf", gen_vect, material)
63 64

    hom_model = homog2d.Fenics2DHomogenization(rve)
65
    *localzt_dicts, constit_tensors = hom_model.homogenizationScheme("EGG")
66 67

    Chom_ref = np.array(
68 69 70 71 72 73
        [
            [2.58608139e-04, 3.45496903e-04, 5.16572422e-12],
            [3.45496903e-04, 3.81860676e-02, 6.48384646e-11],
            [5.16572422e-12, 6.48384646e-11, 3.27924466e-04],
        ]
    )
Baptiste Durand's avatar
Baptiste Durand committed
74

75
    D_ref = np.array(
76 77 78 79 80 81 82 83
        [
            [3.7266e-02, 2.2039e-02, 4.6218e-10, 1.5420e-10, 1.2646e-09, -1.3939e-03],
            [2.2039e-02, -4.3185e-03, -3.4719e-11, 2.7544e-10, 7.0720e-10, 1.2415e-02],
            [4.6218e-10, -3.4719e-11, 1.3071e-01, 1.5918e-02, -9.9492e-03, -2.9101e-10],
            [1.5420e-10, 2.7544e-10, 1.5918e-02, 1.5802e-01, 1.2891e-01, 1.5962e-09],
            [1.2646e-09, 7.0720e-10, -9.9492e-03, 1.2891e-01, 1.1628e-01, 1.3358e-09],
            [-1.3939e-03, 1.2415e-02, -2.9101e-10, 1.5962e-09, 1.3358e-09, 6.3893e-05],
        ]
84 85
    )

86 87 88 89 90 91 92
    Chom = constit_tensors["E"]["E"]
    G = constit_tensors["E"]["EGGbis"]
    D = (
        constit_tensors["EG"]["EG"]
        - np.vstack((G[:, :6], G[:, 6:]))
        - np.vstack((G[:, :6], G[:, 6:])).T
    )
Baptiste Durand's avatar
Baptiste Durand committed
93 94 95 96 97
    logger.debug(f"Chom : \n {Chom}")
    logger.debug(f"D : \n {D}")
    logger.debug(f"Duration : {time.time() - start}")
    assert Chom == approx(Chom_ref, rel=1e-3, abs=1e-10)
    assert D == approx(D_ref, rel=1e-3, abs=1e-8)
98
    logger.debug("End test_homog_EGG_pantograph_1x1")
99
    logger.debug(f"Duration : {time.time() - start}")
100 101


102
def test_homogeneous_pantograph(generate_mesh=False, save_results=False):
103 104 105 106 107 108 109 110 111 112 113 114 115 116
    """ Test élémentaire.

    Homogénéisation d'une cellule homogène,
    construite avec la géométrie du 'pantographe'.
    #* test_homogeneous_pantograph_cell()
    #* >> [[ 1.0989  0.3297 -0.    ]
    #*     [ 0.3297  1.0989 -0.    ]
    #*     [-0.     -0.      0.7692]]

    """
    logger.debug("Start test_homogeneous_pantograph")
    start = time.time()
    if generate_mesh:
        geometry.init_geo_tools()
117
        geometry.set_gmsh_option("Mesh.MshFileVersion", 4.1)
118
        a = 1
119 120 121 122
        b, k, r = a, a / 3, 0.1 * a
        geo_model = mesh_generate.pantograph.pantograph_RVE(
            a, b, k, r, soft_mat=True, name="homogeneous_panto"
        )
123
        lc_ratio = 1
124 125
        lc_min_max = (lc_ratio * r, lc_ratio)
        d_min_max = (2 * r, a)
126 127 128 129 130 131 132 133 134
        geo_model.main_mesh_refinement(d_min_max, lc_min_max, False)
        geo_model.soft_mesh_refinement(d_min_max, lc_min_max, False)
        geo_model.mesh_generate()
        gmsh.model.mesh.renumberNodes()
        gmsh.model.mesh.renumberElements()
        gmsh.write("homogeneous_panto.msh")
        mesh = meshio.read("homogeneous_panto.msh")
        mesh.points = mesh.points[:, :2]
        geo_only = meshio.Mesh(
135 136
            points=mesh.points, cells={"triangle": mesh.cells["triangle"]}
        )
137 138 139
        meshio.write("homogeneous_panto.xdmf", geo_only)
        geometry.reset()

140 141 142 143
    E, nu = 1.0, 0.3
    material = materials.Material(E, nu, "cp")
    gen_vect = np.array([[4.0, 0.0], [0.0, 8.0]])
    rve = part.Fenics2DRVE.rve_from_file("homogeneous_panto.xdmf", gen_vect, material)
144
    hom_model = homog2d.Fenics2DHomogenization(rve)
145
    *localzt_dicts, constit_tensors = hom_model.homogenizationScheme("EGG")
146
    Chom_ref = np.array(
147 148 149 150 151 152
        [
            [1.09890110, 0.329670330, 0],
            [0.329670330, 1.09890110, 0],
            [0, 0, 0.769230769],
        ]
    )
153
    D_ref = np.zeros((6, 6))
154 155 156 157 158 159 160
    Chom = constit_tensors["E"]["E"]
    G = constit_tensors["E"]["EGGbis"]
    D = (
        constit_tensors["EG"]["EG"]
        - np.vstack((G[:, :6], G[:, 6:]))
        - np.vstack((G[:, :6], G[:, 6:])).T
    )
161
    print(Chom)
162 163 164 165
    if save_results:
        with fe.XDMFFile("homogeneous_panto_results.xdmf") as ofile:
            ofile.parameters["flush_output"] = False
            ofile.parameters["functions_share_mesh"] = True
166 167
            for i, E_ in enumerate(("E11", "E22", "E12")):
                loc_eps = fe.project(localzt_dicts[2]["E"][i], hom_model.W)
168
                loc_eps.rename(E_, E_)
169
                ofile.write(loc_eps, 0.0)
170 171 172
    assert Chom == approx(Chom_ref)
    assert D == approx(D_ref)
    logger.debug("End test_homogeneous_pantograph")
173 174
    logger.debug(f"Duration : {time.time() - start}")

175

176
# test_homog_EGG_pantograph_1x1(False)
177
# test_homogeneous_pantograph(False, True)