Beispiel #1
0
def create_fiber_fields(mesh,
                        markers,
                        ffun,
                        angles=None,
                        fiber_space='Quadrature_2'):

    if angles is None:
        angles = dict(
            alpha_endo_lv=60,  # Fiber angle on the endocardium
            alpha_epi_lv=-60,  # Fiber angle on the epicardium
            beta_endo_lv=0,  # Sheet angle on the endocardium
            beta_epi_lv=0)  # Sheet angle on the epicardium

    # Choose space for the fiber fields
    # This is a string on the form {family}_{degree}
    # fiber_space = 'Quadrature_2'
    # At all nodes
    # fiber_space = 'Lagrange_1'
    m = {
        'lv': markers.get('ENDO')[0],
        'epi': markers.get('EPI')[0],
        'base': markers.get('BASE')[0],
    }
    # Compute the microstructure
    fiber, sheet, sheet_normal = ldrb.dolfin_ldrb(mesh=mesh,
                                                  fiber_space=fiber_space,
                                                  ffun=ffun,
                                                  markers=m,
                                                  **angles)
    fiber.rename("fiber", "microstructure")
    sheet.rename("sheet", "microstructure")
    sheet_normal.rename("sheet_normal", "microstructure")
    ldrb.fiber_to_xdmf(fiber, 'lv_fiber')
    return [fiber, sheet, sheet_normal]
Beispiel #2
0
def test_save():

    mesh = df.UnitSquareMesh(3, 3)
    # exit()
    spaces = [
        "DG_0", "DG_1", "CG_1", "CG_2", "R_0", "Quadrature_2", "Quadrature_4"
    ]

    finite_elements = [
        df.FiniteElement(
            s.split("_")[0],
            mesh.ufl_cell(),
            int(s.split("_")[1]),
            quad_scheme="default",
        ) for s in spaces
    ]
    scalar_spaces = [df.FunctionSpace(mesh, el) for el in finite_elements]
    scalar_functions = [
        df.Function(V, name="Scalar_{}".format(s))
        for (V, s) in zip(scalar_spaces, spaces)
    ]

    vector_elements = [
        df.VectorElement(
            s.split("_")[0],
            mesh.ufl_cell(),
            int(s.split("_")[1]),
            quad_scheme="default",
        ) for s in spaces
    ]
    vector_spaces = [df.FunctionSpace(mesh, el) for el in vector_elements]
    vector_functions = [
        df.Function(V, name="Vector_{}".format(s))
        for (V, s) in zip(vector_spaces, spaces)
    ]

    for f, space in zip(scalar_functions, spaces):
        name = 'test_scalar_fun_{space}'.format(space=space)
        ldrb.fun_to_xdmf(f, name)
        os.remove(name + '.xdmf')
        os.remove(name + '.h5')

    for f, space in zip(vector_functions, spaces):
        name = 'test_vector_fun_{space}'.format(space=space)
        ldrb.fun_to_xdmf(f, name)
        os.remove(name + '.xdmf')
        os.remove(name + '.h5')
        name = 'test_vector_fiber_{space}'.format(space=space)
        ldrb.fiber_to_xdmf(f, name)
        os.remove(name + '.xdmf')
        os.remove(name + '.h5')
Beispiel #3
0
def save_geometry_vis(geometry, folder='geometry'):
    """
    Save the geometry as well as markers and fibers to files
    that can be visualized in paraview
    """
    if not os.path.isdir(folder):
        os.makedirs(folder)

    for attr in ['mesh', 'ffun', 'cfun']:
        print('Save {}'.format(attr))
        df.File('{}/{}.pvd'.format(folder, attr)) << getattr(geometry, attr)

    for attr in ['f0', 's0', 'n0']:
        ldrb.fiber_to_xdmf(getattr(geometry, attr),
                           '{}/{}'.format(folder, attr))
Beispiel #4
0
# df.File('rad.pvd') << rad
# rad.rename("radial", "local_basis_function")

# mapper = {'lv': 'ENDO', 'epi': 'EPI', 'rv': 'ENDO_RV', 'base': 'BASE'}
# m = {mapper[k]: (v, 2) for k, v in markers.items()}

# utils.save_geometry_to_h5(mesh, 'ellipsoid.h5', markers=m,
#                           fields=[fiber, sheet, sheet_normal],
#                           local_basis=[circ, rad, lon])

# from IPython import embed; embed()
# exit()
# Store the results
df.File("lv_fiber.xml") << fiber
df.File("lv_sheet.xml") << sheet
df.File("lv_sheet_normal.xml") << sheet_normal

# If you run in paralell you should skip the visualization step and do that in
# serial in stead. In that case you can read the the functions from the xml
# Using the following code
V = ldrb.space_from_string(fiber_space, mesh, dim=3)
fiber = df.Function(V, "lv_fiber.xml")
sheet = df.Function(V, "lv_sheet.xml")
sheet_normal = df.Function(V, "lv_sheet_normal.xml")

# Store files in XDMF to be visualized in Paraview
# (These function are not tested in paralell)
ldrb.fiber_to_xdmf(fiber, "lv_fiber")
ldrb.fiber_to_xdmf(sheet, "lv_sheet")
ldrb.fiber_to_xdmf(sheet_normal, "lv_sheet_normal")
Beispiel #5
0
# Select a space for the fibers (here linear lagrange element)

fiber_space = "P_2"

# Create a dictionary of fiber angles

angles = dict(
    alpha_endo_lv=60,  # Fiber angle on the endocardium
    alpha_epi_lv=-60,  # Fiber angle on the epicardium
    beta_endo_lv=0,  # Sheet angle on the endocardium
    beta_epi_lv=0,
)

# Run the LDRB algorithm

fiber, sheet, sheet_normal = ldrb.dolfin_ldrb(mesh=mesh,
                                              fiber_space=fiber_space,
                                              ffun=ffun,
                                              markers=ldrb_markers,
                                              **angles)

# Save to xdmf
# with dolfin.XDMFFile(mesh.mpi_comm(), "patient_fiber.xdmf") as xdmf:
#     xdmf.write(fiber)

# Use this function to save fibrer with angles as scalars

ldrb.fiber_to_xdmf(fiber, "patient_fiber")

# ![_](_static/figures/patient_fiber.png)
Beispiel #6
0
        mesh=mesh,
        fiber_space=space,
        ffun=ffun,
        markers=markers,
        alpha_endo_lv=-90,
        alpha_epi_lv=-90,
        beta_endo_lv=0,
        beta_epi_lv=0,
        save_markers=True,
    )

#
# Finally we save the vector fields to a file
#

ldrb.fiber_to_xdmf(long, "long")
ldrb.fiber_to_xdmf(circ, "circ")
ldrb.fiber_to_xdmf(rad, "ran")

#
#
# This resulting BiV and LV vector field are shown in {numref}`Figure {number} <lv_basis_function>` and {numref}`Figure {number} <biv_basis_function>` respectively.
#
# ```{figure} _static/figures/lv_basis_functions.png
# ---
# name: lv_basis_function
# ---
#
# LV longitudinal, circumferential and radial vector fields
# ```
#
Beispiel #7
0
# This is a string on the form {family}_{degree}
fiber_space = 'Quadrature_2'
# fiber_space = 'Lagrange_1'

# Compute the microstructure
fiber, sheet, sheet_normal = ldrb.dolfin_ldrb(mesh=mesh,
                                              fiber_space=fiber_space,
                                              ffun=ffun,
                                              markers=markers,
                                              log_level=df.debug,
                                              **angles)

# # Store the results
df.File('biv_fiber.xml') << fiber
df.File('biv_sheet.xml') << sheet
df.File('biv_sheet_normal.xml') << sheet_normal

# If you run in paralell you should skip the visualization step and do that in
# serial in stead. In that case you can read the the functions from the xml
# Using the following code
# V = ldrb.space_from_string(fiber_space, mesh, dim=3)
# fiber = df.Function(V, 'biv_fiber.xml')
# sheet = df.Function(V, 'biv_sheet.xml')
# sheet_normal = df.Function(V, 'biv_sheet_normal.xml')

# Store files in XDMF to be visualized in Paraview
# (These function are not tested in paralell)
ldrb.fiber_to_xdmf(fiber, 'biv_fiber')
ldrb.fiber_to_xdmf(sheet, 'biv_sheet')
ldrb.fiber_to_xdmf(sheet_normal, 'biv_sheet_normal')
Beispiel #8
0
    h5file.write(sheet, "/sheet")
    h5file.write(sheet_normal, "/sheet_normal")

# If you run in parallel you should skip the visualisation step and do that in
# serial in stead. In that case you can read the the functions from the xml
# Using the following code

# +
V = ldrb.space_from_string(fiber_space, mesh, dim=3)

fiber = df.Function(V)
sheet = df.Function(V)
sheet_normal = df.Function(V)

with df.HDF5File(mesh.mpi_comm(), "biv.h5", "r") as h5file:
    h5file.read(fiber, "/fiber")
    h5file.read(sheet, "/sheet")
    h5file.read(sheet_normal, "/sheet_normal")

# -

# You can also store files in XDMF which will also compute the fiber angle as scalars on the glyph to be visualised in Paraview. Note that these functions don't work (yet) using mpirun

# (These function are not tested in parallel)
ldrb.fiber_to_xdmf(fiber, "biv_fiber")
# ldrb.fiber_to_xdmf(sheet, "biv_sheet")
# ldrb.fiber_to_xdmf(sheet_normal, "biv_sheet_normal")

# ![_](_static/figures/biv_fiber.png)
# [Link to source code](https://github.com/finsberg/ldrb/blob/master/demos/demo_biv.py)