예제 #1
0
파일: pyfenics.py 프로젝트: xshi/raser
 def fenics_space(self, my_d):
     """
     @description: 
         Define the fenics solver space 
     @param:
         None
     @Returns:
         Fenics Box structure
     @Modify:
         2021/08/31
     """
     if "plugin3D" in self.det_model:
         self.sensor_range_confirm(my_d)
         m_sensor = mshr.Box(fenics.Point(self.sx_l, self.sy_l, 0),
                             fenics.Point(self.sx_r, self.sy_r, self.fl_z))
         for i in range(len(my_d.e_tr)):
             e_t_i = my_d.e_tr[i]
             elec_n = mshr.Cylinder(
                 fenics.Point(e_t_i[0], e_t_i[1], e_t_i[3]),
                 fenics.Point(e_t_i[0], e_t_i[1], e_t_i[4]), e_t_i[2],
                 e_t_i[2])
             m_sensor = m_sensor - elec_n
     elif "planar3D" in self.det_model:
         m_sensor = mshr.Box(fenics.Point(0, 0, 0),
                             fenics.Point(self.fl_x, self.fl_y, self.fl_z))
     else:
         print("sensor model is wrong")
         sys.exit()
     return m_sensor
예제 #2
0
    def create_cocontinuous_mesh(self):

        if self.use_previously_stored_mesh:

            self.mesh = fe.Mesh(self.mesh_fname)
            print('Mesh has been loaded from file')
            return

        p = self.L * np.ones(3)
        geo     = mesher.Box(fe.Point(0,0,0), fe.Point(p))
        net     = self.extrude_base_net()
        columns = self.create_cylindrical_columns()
        net    += columns

        if self.keep_only_network: 
            geo  = net
        else:
            geo -= net

        print('Finished creating the geometry')
        self.mesh = mesher.generate_mesh(geo, self.mesh_density);

        print('Writing mesh to file')
        mesh_file = fe.File(self.mesh_fname)
        mesh_file << self.mesh
        print('Finished writing mesh to file')
예제 #3
0
def box_mesh_with_hole(point1=Point(0, 0, 0),
                       point2=Point(2, 1, 1),
                       cyl_cent1=Point(1, -10, 0.5),
                       cyl_cent2=Point(1, 10, 0.5),
                       cyl_rad=0.25,
                       numpts=15):
    """
    generates a 3D box mesh with tetrahedral elements with a cylindrical hole in it

    input
    ------
        point1: Point coordinates in 3D corner_min of box
        point2: Point coordinates in 3D corner_max of box
        cyl_cent1: Point coordinates in 3D of center1 of Cylinder
        cyl_cent2: Point coordinates in 3D of center1 of Cylinder
        cyl_rad:   Radius of cylinder
        npts: Number of discretization points
    output:
    ------
        mesh: 3D FEniCS mesh with a cylindrical hole

    """
    Router = mshr.Box(point1, point2)
    Rinner = mshr.Cylinder(cyl_cent1, cyl_cent2, cyl_rad, cyl_rad)
    domain = Router - Rinner

    mesh = mshr.generate_mesh(domain, numpts)
    print_mesh_stats(mesh)

    return mesh
예제 #4
0
def mesh_Cube(n=10):
    dom = mshr.Box(Point(0., 0., 0.), Point(1., 1., 1.))
    gen = mshr.CSGCGALMeshGenerator3D()
    #gen.parameters["facet_angle"] = 30.0
    #gen.parameters["facet_size"] = 0.5
    #gen.parameters["edge_size"] = 0.5
    gen.parameters["mesh_resolution"] = 0.01
    gen.parameters["odt_optimize"] = True
    mesh = gen.generate(mshr.CSGCGALDomain3D(dom))
    return mesh
예제 #5
0
def create_toy_mesh():

    box = mshr.Box(dolfin.Point(-3, -1, -0.5), dolfin.Point(3, 1, 0.5))

    c1 = mshr.Cylinder(dolfin.Point(0, 0, -2), dolfin.Point(0, 0, 2), 0.6, 0.6)
    b1 = mshr.Box(dolfin.Point(-2.5, -0.5, -2), dolfin.Point(-1.5, 0.5, 2))

    # "triangle"
    t1 = mshr.Polygon([
        dolfin.Point(2.5, -0.5, 0),
        dolfin.Point(2.5, 0.5, 0),
        dolfin.Point(1.5, -0.5, 0),
    ])
    g3d = mshr.Extrude2D(t1, -2)
    g3d = mshr.CSGTranslation(g3d, dolfin.Point(0, 0, 1))

    m = mshr.generate_mesh(box - c1 - b1 - g3d, 40, "cgal")

    return m.coordinates(), m.cells()
예제 #6
0
    def initiate_fem(self):
        #define mesh
        #self.mesh = BoxMesh(Point(0, 0, 0), Point(self.L, self.W, self.W), 10, 3, 3)

        # Parameters
        R = self.W / 4
        r = 0.08
        t = self.W
        x = self.W / 2 + R * cos(float(t) / 180 * pi)
        y = self.W / 2
        z = R * sin(t)

        # Create geometry
        s1 = mshr.Sphere(Point(x + self.L - 3 / 2 * self.W, y, z), r)
        s2 = mshr.Sphere(Point(x, y, z), r)

        b1 = mshr.Box(Point(0, 0, 0), Point(self.L, self.W, self.W))
        b2 = mshr.Box(Point(self.L / 2 - self.w, 0, self.W / 2),
                      Point(self.L / 2 + self.w, self.W, self.W))
        geometry1 = b1 - s1 - s2
        geometry2 = b1 - b2

        # Create and store mesh
        self.mesh = mshr.generate_mesh(geometry1,
                                       10)  # use geometry1 or geometry2
        #cell_markers = CellFunction("bool", mesh)
        #cell_markers.set_all(False)
        #for cell in cells(mesh):
        #p = cell.midpoint()
        #if p.x() > (self.L/2-2*self.w) and p.x() < (self.L/2-2*self.w):
        #cell_markers[cell] = True

        ## refining mesh
        #mesh = LocalMeshCoarsening(mesh, cell_markers)
        #self.mesh = mesh

        File('results/cracked_beam.pvd') << self.mesh

        #define function space
        self.V = VectorFunctionSpace(self.mesh, 'P', 1)

        #define dirichlet boundary
        self.bc = DirichletBC(self.V, Constant((0, 0, 0)), clamped_boundary)

        #define right hand side function
        self.f = Constant((0, 0, -self.rho * self.g))
        self.T = Constant((0, 0, 0))

        #define functions
        q = TrialFunction(self.V)
        #		p = TrialFunction(self.V)
        self.d = q.geometric_dimension()
        v = TestFunction(self.V)

        aq = inner(q, v) * dx
        kq = -inner(sigma(q, self.lambda_, self.mu, self.d), epsilon(v)) * dx
        Lq = inner(self.f, v) * dx

        Kq, bq = assemble_system(kq, Lq, self.bc)
        self.Aq, dummy = assemble_system(aq, Lq, self.bc)

        #define the mass and stiffness matrices
        M = np.matrix(self.Aq.array())
        print(M.shape)
        self.M_inv = np.linalg.inv(M)
        self.K = np.matrix(Kq.array())

        print(self.K.shape)

        #define the force term
        c = np.matrix(bq.array())
        self.cp = np.transpose(c)
예제 #7
0
 def box(corner1=None, corner2=None):
     """A 3d rectangular prism from `corner1` to `corner2` ((0, 0, 0) to (1, 1, 1) by default)"""
     corner1 = corner1 or (0, 0, 0)
     corner2 = corner2 or (1, 1, 1)
     return mshr.Box(FEN.Point(corner1), FEN.Point(corner2))
예제 #8
0
def create_lv_mesh(
    N=13,
    a_endo=1.5,
    b_endo=0.5,
    c_endo=0.5,
    a_epi=2.0,
    b_epi=1.0,
    c_epi=1.0,
    center=(0.0, 0.0, 0.0),
    base_x=0.0,
    markers=None,
):
    r"""
    Create an lv-ellipsoidal mesh.

    An ellipsoid is given by the equation

    .. math::

        \frac{x^2}{a} + \frac{y^2}{b} + \frac{z^2}{c} = 1

    We create two ellipsoids, one for the endocardium and one
    for the epicardium and subtract them and then cut the base.
    For simplicity we assume that the longitudinal axis is in
    in :math:`x`-direction and as default the base is located
    at the :math:`x=0` plane.
    """
    df.info("Creating LV mesh. This could take some time...")
    # LV
    # The center of the LV ellipsoid
    center = df.Point(*center)

    # Markers
    if markers is None:
        markers = default_markers()

    class Endo(df.SubDomain):
        def inside(self, x, on_boundary):
            return (
                (x[0] - center.x()) ** 2 / a_endo ** 2
                + (x[1] - center.y()) ** 2 / b_endo ** 2
                + (x[2] - center.z()) ** 2 / c_endo ** 2
                - 1.1
                < df.DOLFIN_EPS
                and on_boundary
            )

    class Base(df.SubDomain):
        def inside(self, x, on_boundary):
            return x[0] - base_x < df.DOLFIN_EPS and on_boundary

    class Epi(df.SubDomain):
        def inside(self, x, on_boundary):
            return (
                (x[0] - center.x()) ** 2 / a_epi ** 2
                + (x[1] - center.y()) ** 2 / b_epi ** 2
                + (x[2] - center.z()) ** 2 / c_epi ** 2
                - 0.9
                > df.DOLFIN_EPS
                and on_boundary
            )

    # The plane cutting the base
    diam = -2 * a_epi
    box = mshr.Box(df.Point(base_x, -diam, -diam), df.Point(diam, diam, diam))

    # LV epicardium
    el_lv = mshr.Ellipsoid(center, a_epi, b_epi, c_epi)
    # LV endocardium
    el_lv_endo = mshr.Ellipsoid(center, a_endo, b_endo, c_endo)

    # LV geometry (subtract the smallest ellipsoid)
    lv = el_lv - el_lv_endo

    # LV geometry
    m = lv - box

    # Create mesh
    print("Generate mesh. This can take some time...")
    mesh = mshr.generate_mesh(m, N)

    ffun = df.MeshFunction("size_t", mesh, 2)
    ffun.set_all(0)

    endo = Endo()
    endo.mark(ffun, markers["lv"])

    base = Base()
    base.mark(ffun, markers["base"])

    epi = Epi()
    epi.mark(ffun, markers["epi"])

    mark_facets(mesh, ffun)
    return geometry(mesh=mesh, ffun=ffun, markers=markers)
예제 #9
0
def create_biv_mesh(
    N=13,
    a_endo_lv=1.5,
    b_endo_lv=0.5,
    c_endo_lv=0.5,
    a_epi_lv=2.0,
    b_epi_lv=1.0,
    c_epi_lv=1.0,
    center_lv=(0.0, 0.0, 0.0),
    a_endo_rv=1.45,
    b_endo_rv=1.25,
    c_endo_rv=0.75,
    a_epi_rv=1.75,
    b_epi_rv=1.5,
    c_epi_rv=1.0,
    center_rv=(0.0, 0.5, 0.0),
    base_x=0.0,
    markers=None,
):
    r"""
    Create an biv-ellipsoidal mesh.

    An ellipsoid is given by the equation

    .. math::

        \frac{x^2}{a} + \frac{y^2}{b} + \frac{z^2}{c} = 1

    We create three ellipsoids, one for the LV and RV endocardium
    and one for the epicardium and subtract them and then cut the base.
    For simplicity we assume that the longitudinal axis is in
    in :math:`x`-direction and as default the base is located
    at the :math:`x=0` plane.

    """
    df.info("Creating BiV mesh. This could take some time...")

    # The center of the LV ellipsoid
    center_lv = df.Point(*center_lv)
    # The center of the RV ellipsoid (slightly translated)
    center_rv = df.Point(*center_rv)

    # Markers
    if markers is None:
        markers = default_markers()

    class EndoLV(df.SubDomain):
        def inside(self, x, on_boundary):
            return (
                (x[0] - center_lv.x()) ** 2 / a_endo_lv ** 2
                + (x[1] - center_lv.y()) ** 2 / b_endo_lv ** 2
                + (x[2] - center_lv.z()) ** 2 / c_endo_lv ** 2
                - 1
                < df.DOLFIN_EPS
                and on_boundary
            )

    class Base(df.SubDomain):
        def inside(self, x, on_boundary):
            return x[0] - base_x < df.DOLFIN_EPS and on_boundary

    class EndoRV(df.SubDomain):
        def inside(self, x, on_boundary):
            return (
                (x[0] - center_rv.x()) ** 2 / a_endo_rv ** 2
                + (x[1] - center_rv.y()) ** 2 / b_endo_rv ** 2
                + (x[2] - center_rv.z()) ** 2 / c_endo_rv ** 2
                - 1
                < df.DOLFIN_EPS
                and (x[0] - center_lv.x()) ** 2 / a_epi_lv ** 2
                + (x[1] - center_lv.y()) ** 2 / b_epi_lv ** 2
                + (x[2] - center_lv.z()) ** 2 / c_epi_lv ** 2
                - 0.9
                > df.DOLFIN_EPS
            ) and on_boundary

    class Epi(df.SubDomain):
        def inside(self, x, on_boundary):
            return (
                (x[0] - center_rv.x()) ** 2 / a_epi_rv ** 2
                + (x[1] - center_rv.y()) ** 2 / b_epi_rv ** 2
                + (x[2] - center_rv.z()) ** 2 / c_epi_rv ** 2
                - 0.9
                > df.DOLFIN_EPS
                and (x[0] - center_lv.x()) ** 2 / a_epi_lv ** 2
                + (x[1] - center_lv.y()) ** 2 / b_epi_lv ** 2
                + (x[2] - center_lv.z()) ** 2 / c_epi_lv ** 2
                - 0.9
                > df.DOLFIN_EPS
                and on_boundary
            )

    # The plane cutting the base
    a_epi = max(a_epi_lv, a_epi_rv)
    diam = -5 * a_epi
    box = mshr.Box(df.Point(base_x, a_epi, a_epi), df.Point(diam, diam, diam))
    # Generate mesh

    # LV epicardium
    el_lv = mshr.Ellipsoid(center_lv, a_epi_lv, b_epi_lv, c_epi_lv)
    # LV endocardium
    el_lv_endo = mshr.Ellipsoid(center_lv, a_endo_lv, b_endo_lv, c_endo_lv)

    # LV geometry (subtract the smallest ellipsoid)
    lv = el_lv - el_lv_endo

    # LV epicardium
    el_rv = mshr.Ellipsoid(center_rv, a_epi_rv, b_epi_rv, c_epi_rv)
    # LV endocardium
    el_rv_endo = mshr.Ellipsoid(center_rv, a_endo_rv, b_endo_rv, c_endo_rv)

    # RV geometry (subtract the smallest ellipsoid)
    rv = el_rv - el_rv_endo - el_lv

    # BiV geometry
    m = lv + rv - box

    # Create mesh
    mesh = mshr.generate_mesh(m, N)

    ffun = df.MeshFunction("size_t", mesh, 2)
    ffun.set_all(0)

    endolv = EndoLV()
    endolv.mark(ffun, markers["lv"])
    base = Base()
    base.mark(ffun, markers["base"])
    endorv = EndoRV()
    endorv.mark(ffun, markers["rv"])
    epi = Epi()
    epi.mark(ffun, markers["epi"])

    mark_facets(mesh, ffun)
    return geometry(mesh=mesh, ffun=ffun, markers=markers)
# # m = mshr.generate_mesh(dom, 5)
# m = BoxMesh(Point(0., 0., 0.), Point(1., 1., 1.), n, n, n)
# K = 4
# for k in range(K):
#     V = FunctionSpace(m, 'Lagrange', p)
#     u = TrialFunction(V)
#     v = TestFunction(V)

#     M = assemble(u * v * dx)
#     M_diag = as_backend_type(M).mat().getDiagonal().array
#     print('Min of diag of M: {}'.format(M_diag.min()))
#     print('Dim V: {}'.format(V.dim()))
#     if k < K:
#         m = refine(m)

dom = mshr.Box(Point(0., 0., 0.), Point(1., 1., 1.))
# m_orig = mshr.generate_mesh(dom,0.01)

gen = mshr.CSGCGALMeshGenerator3D()

#gen.parameters["facet_angle"] = 30.0
#gen.parameters["facet_size"] = 0.5
#gen.parameters["edge_size"] = 0.5
gen.parameters["mesh_resolution"] = 0.01
gen.parameters["exude_optimize"] = True

m_orig = gen.generate(mshr.CSGCGALDomain3D(dom))

xf = File('mesh_orig.pvd')
xf << m_orig
예제 #11
0
class Base(df.SubDomain):
    def inside(self, x, on_boundary):
        return x[0] - base_x < df.DOLFIN_EPS and on_boundary


class Epi(df.SubDomain):
    def inside(self, x, on_boundary):
        return (x[0]-center.x())**2/a_epi**2 \
            + (x[1]-center.y())**2/b_epi**2 \
            + (x[2]-center.z())**2/c_epi**2 - 0.9 > df.DOLFIN_EPS \
            and on_boundary


# The plane cutting the base
diam = -10.0
box = mshr.Box(df.Point(base_x, 2, 2), df.Point(diam, diam, diam))
# Generate mesh

# LV epicardium
el_lv = mshr.Ellipsoid(center, a_epi, b_epi, c_epi)
# LV endocardium
el_lv_endo = mshr.Ellipsoid(center, a_endo, b_endo, c_endo)

# LV geometry (subtract the smallest ellipsoid)
lv = el_lv - el_lv_endo

# LV geometry
m = lv - box

# Create mesh
mesh = mshr.generate_mesh(m, N)
예제 #12
0
from block import block_mat, block_vec, block_bc
from dolfin import *
from xii import *

import mshr

N = 10
EPS = 1E-3
R = 0.25
box_domain = mshr.Box(dolfin.Point(0, 0, 0), dolfin.Point(1, 1, 1))
_mesh = mshr.generate_mesh(box_domain, N)

stokes_subdomain = dolfin.CompiledSubDomain(
    "sqrt((x[0]-0.5) * (x[0]-0.5) + (x[1]-0.5) * (x[1]-0.5)) < R", R=R)

subdomains = MeshFunction('size_t', _mesh, _mesh.topology().dim(), 0)

# Awkward marking
for cell in cells(_mesh):
    x = cell.midpoint().array()
    if stokes_subdomain.inside(x, False):
        subdomains[cell] = 1
    else:
        subdomains[cell] = 0

submeshes, interface, _ = mortar_meshes(subdomains,
                                        list(range(2)),
                                        strict=True,
                                        tol=EPS)

stokes_domain = submeshes[0]
예제 #13
0
                    help="Number of loading steps to use.")
parser.add_argument("--polynomial-degree", "-pd",
                    type=int, default=2, dest="pd", choices=[1, 2, 3],
                    help="Polynomial degree to be used for displacement.")
args = parser.parse_args()

# Region IDs
HNBC  = 0  # homogeneous Neumann BC
HDBC  = 10  # homogeneous Dirichlet BC
INBC  = 20  # inhomogeneous Neumann BC

KAPPA = 1e100 if args.incompressible else args.kappa

if args.generate_mesh:
    import mshr
    domain = mshr.Box(dlf.Point(), dlf.Point(10, 1, 1))
    mesh_file = mshr.generate_mesh(domain, args.resolution)

    boundaries = dlf.MeshFunction("size_t", mesh_file, 2)
    boundaries.set_all(HNBC)

    hdbc = dlf.CompiledSubDomain("near(x[0], 0.0) && on_boundary")
    hdbc.mark(boundaries, HDBC)

    inbc = dlf.CompiledSubDomain("near(x[2], 0.0) && on_boundary")
    inbc.mark(boundaries, INBC)
else:
    mesh_file = boundaries = args.mesh_file

mesh = {
    'mesh_file': mesh_file,