The motivation for this is to render the Finite Element simulation results from FEniCS in yt where both projects are NumFocus projects; thereby removing the need to store and recall the FEniCS output in vtk
thanks to Matthew Turk (@powersoffour) in helping to this working via: Re: [yt-project/yt] load fenics data into yt instead of relaying on vtk (#1780)
To install I had to run
conda create -n fenicsproject -c conda-forge fenics
source activate fenicsproject
from https://fenicsproject.org/download/
this was followed by having to install mshr
for the following ex to work wich downgrades the version of fenics
conda install -c conda-forge mshr
https://anaconda.org/conda-forge/mshr this was then followed up with install installing matplotlib, yt, xarray, jupyter in the VirEnv
The FEniCS example I am using is the first obvious 3d example from the * Solving PDEs in Python – The FEniCS Tutorial I by Langtangen & Logg* (https://fenicsproject.org/tutorial/) is the elastic beam problem in section 3.3 where the source code is here (https://github.com/hplgit/fenics-tutorial/blob/master/pub/python/vol1/ft06_elasticity.py)
from fenics import *
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import yt
from yt.visualization.volume_rendering.transfer_function_helper import TransferFunctionHelper
from yt.visualization.volume_rendering.render_source import VolumeSource
/home/iridium/anaconda3/envs/fenicsproject/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`. from ._conv import register_converters as _register_converters
# Scaled variables
L = 1; W = 0.2
mu = 1
rho = 1
delta = W/L
gamma = 0.4*delta**2
beta = 1.25
lambda_ = beta
g = gamma
# Create mesh and define function space
mesh = BoxMesh(Point(0, 0, 0), Point(L, W, W), 10, 3, 3)
V = VectorFunctionSpace(mesh, 'P', 1)
# Define boundary condition
tol = 1E-14
def clamped_boundary(x, on_boundary):
return on_boundary and x[0] < tol
bc = DirichletBC(V, Constant((0, 0, 0)), clamped_boundary)
#strain
def epsilon(u):
return 0.5*(nabla_grad(u) + nabla_grad(u).T)
#return sym(nabla_grad(u))
#stress
def sigma(u):
return lambda_*nabla_div(u)*Identity(d) + 2*mu*epsilon(u)
# Define variational problem
u = TrialFunction(V)
d = u.geometric_dimension() # space dimension
v = TestFunction(V)
f = Constant((0, 0, -rho*g))
T = Constant((0, 0, 0))
a = inner(sigma(u), epsilon(v))*dx
L = dot(f, v)*dx + dot(T, v)*ds
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Plot solution
plot(u, title='Displacement', mode='displacement')
# Plot stress
s = sigma(u) - (1./3)*tr(sigma(u))*Identity(d) # deviatoric stress
von_Mises = sqrt(3./2*inner(s, s))
V = FunctionSpace(mesh, 'P', 1)
von_Mises = project(von_Mises, V)
plot(von_Mises, title='Stress intensity')
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7f2dcfb3f550>
# Compute magnitude of displacement
u_magnitude = sqrt(dot(u, u))
u_magnitude = project(u_magnitude, V)
plot(u_magnitude, 'Displacement magnitude')
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7f2dcf8646a0>
The results are stored in u
where I have not been able to tell if u
has the mesh coordinates stored within it. putting that aside for a moment lets examine the mesh
generated by
mesh = BoxMesh(Point(0, 0, 0), Point(L, W, W), 10, 3, 3)
where the documents for BoxMesh
are (https://fenicsproject.org/olddocs/dolfin/1.3.0/python/programmers-reference/cpp/mesh/BoxMesh.html) where it states the resulting mesh is a Tetrahedral mesh. The coordinates of the mech vertices should then be callable and stored in Mcoords
via:
Mcoords=mesh.coordinates()
print(Mcoords.shape)
Mcoords
(176, 3)
array([[0. , 0. , 0. ], [0.1 , 0. , 0. ], [0.2 , 0. , 0. ], [0.3 , 0. , 0. ], [0.4 , 0. , 0. ], [0.5 , 0. , 0. ], [0.6 , 0. , 0. ], [0.7 , 0. , 0. ], [0.8 , 0. , 0. ], [0.9 , 0. , 0. ], [1. , 0. , 0. ], [0. , 0.06666667, 0. ], [0.1 , 0.06666667, 0. ], [0.2 , 0.06666667, 0. ], [0.3 , 0.06666667, 0. ], [0.4 , 0.06666667, 0. ], [0.5 , 0.06666667, 0. ], [0.6 , 0.06666667, 0. ], [0.7 , 0.06666667, 0. ], [0.8 , 0.06666667, 0. ], [0.9 , 0.06666667, 0. ], [1. , 0.06666667, 0. ], [0. , 0.13333333, 0. ], [0.1 , 0.13333333, 0. ], [0.2 , 0.13333333, 0. ], [0.3 , 0.13333333, 0. ], [0.4 , 0.13333333, 0. ], [0.5 , 0.13333333, 0. ], [0.6 , 0.13333333, 0. ], [0.7 , 0.13333333, 0. ], [0.8 , 0.13333333, 0. ], [0.9 , 0.13333333, 0. ], [1. , 0.13333333, 0. ], [0. , 0.2 , 0. ], [0.1 , 0.2 , 0. ], [0.2 , 0.2 , 0. ], [0.3 , 0.2 , 0. ], [0.4 , 0.2 , 0. ], [0.5 , 0.2 , 0. ], [0.6 , 0.2 , 0. ], [0.7 , 0.2 , 0. ], [0.8 , 0.2 , 0. ], [0.9 , 0.2 , 0. ], [1. , 0.2 , 0. ], [0. , 0. , 0.06666667], [0.1 , 0. , 0.06666667], [0.2 , 0. , 0.06666667], [0.3 , 0. , 0.06666667], [0.4 , 0. , 0.06666667], [0.5 , 0. , 0.06666667], [0.6 , 0. , 0.06666667], [0.7 , 0. , 0.06666667], [0.8 , 0. , 0.06666667], [0.9 , 0. , 0.06666667], [1. , 0. , 0.06666667], [0. , 0.06666667, 0.06666667], [0.1 , 0.06666667, 0.06666667], [0.2 , 0.06666667, 0.06666667], [0.3 , 0.06666667, 0.06666667], [0.4 , 0.06666667, 0.06666667], [0.5 , 0.06666667, 0.06666667], [0.6 , 0.06666667, 0.06666667], [0.7 , 0.06666667, 0.06666667], [0.8 , 0.06666667, 0.06666667], [0.9 , 0.06666667, 0.06666667], [1. , 0.06666667, 0.06666667], [0. , 0.13333333, 0.06666667], [0.1 , 0.13333333, 0.06666667], [0.2 , 0.13333333, 0.06666667], [0.3 , 0.13333333, 0.06666667], [0.4 , 0.13333333, 0.06666667], [0.5 , 0.13333333, 0.06666667], [0.6 , 0.13333333, 0.06666667], [0.7 , 0.13333333, 0.06666667], [0.8 , 0.13333333, 0.06666667], [0.9 , 0.13333333, 0.06666667], [1. , 0.13333333, 0.06666667], [0. , 0.2 , 0.06666667], [0.1 , 0.2 , 0.06666667], [0.2 , 0.2 , 0.06666667], [0.3 , 0.2 , 0.06666667], [0.4 , 0.2 , 0.06666667], [0.5 , 0.2 , 0.06666667], [0.6 , 0.2 , 0.06666667], [0.7 , 0.2 , 0.06666667], [0.8 , 0.2 , 0.06666667], [0.9 , 0.2 , 0.06666667], [1. , 0.2 , 0.06666667], [0. , 0. , 0.13333333], [0.1 , 0. , 0.13333333], [0.2 , 0. , 0.13333333], [0.3 , 0. , 0.13333333], [0.4 , 0. , 0.13333333], [0.5 , 0. , 0.13333333], [0.6 , 0. , 0.13333333], [0.7 , 0. , 0.13333333], [0.8 , 0. , 0.13333333], [0.9 , 0. , 0.13333333], [1. , 0. , 0.13333333], [0. , 0.06666667, 0.13333333], [0.1 , 0.06666667, 0.13333333], [0.2 , 0.06666667, 0.13333333], [0.3 , 0.06666667, 0.13333333], [0.4 , 0.06666667, 0.13333333], [0.5 , 0.06666667, 0.13333333], [0.6 , 0.06666667, 0.13333333], [0.7 , 0.06666667, 0.13333333], [0.8 , 0.06666667, 0.13333333], [0.9 , 0.06666667, 0.13333333], [1. , 0.06666667, 0.13333333], [0. , 0.13333333, 0.13333333], [0.1 , 0.13333333, 0.13333333], [0.2 , 0.13333333, 0.13333333], [0.3 , 0.13333333, 0.13333333], [0.4 , 0.13333333, 0.13333333], [0.5 , 0.13333333, 0.13333333], [0.6 , 0.13333333, 0.13333333], [0.7 , 0.13333333, 0.13333333], [0.8 , 0.13333333, 0.13333333], [0.9 , 0.13333333, 0.13333333], [1. , 0.13333333, 0.13333333], [0. , 0.2 , 0.13333333], [0.1 , 0.2 , 0.13333333], [0.2 , 0.2 , 0.13333333], [0.3 , 0.2 , 0.13333333], [0.4 , 0.2 , 0.13333333], [0.5 , 0.2 , 0.13333333], [0.6 , 0.2 , 0.13333333], [0.7 , 0.2 , 0.13333333], [0.8 , 0.2 , 0.13333333], [0.9 , 0.2 , 0.13333333], [1. , 0.2 , 0.13333333], [0. , 0. , 0.2 ], [0.1 , 0. , 0.2 ], [0.2 , 0. , 0.2 ], [0.3 , 0. , 0.2 ], [0.4 , 0. , 0.2 ], [0.5 , 0. , 0.2 ], [0.6 , 0. , 0.2 ], [0.7 , 0. , 0.2 ], [0.8 , 0. , 0.2 ], [0.9 , 0. , 0.2 ], [1. , 0. , 0.2 ], [0. , 0.06666667, 0.2 ], [0.1 , 0.06666667, 0.2 ], [0.2 , 0.06666667, 0.2 ], [0.3 , 0.06666667, 0.2 ], [0.4 , 0.06666667, 0.2 ], [0.5 , 0.06666667, 0.2 ], [0.6 , 0.06666667, 0.2 ], [0.7 , 0.06666667, 0.2 ], [0.8 , 0.06666667, 0.2 ], [0.9 , 0.06666667, 0.2 ], [1. , 0.06666667, 0.2 ], [0. , 0.13333333, 0.2 ], [0.1 , 0.13333333, 0.2 ], [0.2 , 0.13333333, 0.2 ], [0.3 , 0.13333333, 0.2 ], [0.4 , 0.13333333, 0.2 ], [0.5 , 0.13333333, 0.2 ], [0.6 , 0.13333333, 0.2 ], [0.7 , 0.13333333, 0.2 ], [0.8 , 0.13333333, 0.2 ], [0.9 , 0.13333333, 0.2 ], [1. , 0.13333333, 0.2 ], [0. , 0.2 , 0.2 ], [0.1 , 0.2 , 0.2 ], [0.2 , 0.2 , 0.2 ], [0.3 , 0.2 , 0.2 ], [0.4 , 0.2 , 0.2 ], [0.5 , 0.2 , 0.2 ], [0.6 , 0.2 , 0.2 ], [0.7 , 0.2 , 0.2 ], [0.8 , 0.2 , 0.2 ], [0.9 , 0.2 , 0.2 ], [1. , 0.2 , 0.2 ]])
The data we wish to see is stored in u
which will be extracted to U
thusly
U=u.vector().get_local()
print(U.shape)
U
(528,)
array([ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 5.08008829e-03, 2.91100131e-04, -4.53526583e-03, 5.79034485e-03, 1.05831925e-03, -4.40190290e-03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.74298392e-03, 2.81428971e-04, -3.95206961e-03, 2.10122330e-03, 5.91689712e-04, -3.83491393e-03, 9.55946557e-03, 8.01949147e-04, -1.30091504e-02, 1.03267096e-02, 1.38395092e-03, -1.29236194e-02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.69275096e-03, -2.46781203e-04, -4.35755105e-03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 8.88828238e-03, 2.72478960e-04, -1.27282845e-02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -1.34213137e-03, 2.17430325e-04, -3.96323060e-03, -1.16993421e-03, -1.92394843e-06, -3.87141136e-03, 3.24597852e-03, 8.63286339e-04, -1.25698031e-02, 3.81028898e-03, 1.12875941e-03, -1.24842089e-02, 1.29586814e-02, 1.61357330e-03, -2.51093280e-02, 1.38527120e-02, 2.06616845e-03, -2.51441989e-02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.45209386e-03, 7.17831865e-05, -3.81146618e-03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.20371306e-03, -7.75095826e-04, -3.94609564e-03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 2.69432886e-03, 6.66704677e-04, -1.23115617e-02, 1.21153684e-02, 1.19181880e-03, -2.47743597e-02, 1.11705059e-02, 8.05236443e-04, -2.41868888e-02, 8.11361998e-03, -2.10541241e-04, -1.21593514e-02, -4.93021127e-03, -3.07058991e-04, -4.51235528e-03, -5.05746880e-03, 1.97115959e-04, -4.70093781e-03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -1.56689759e-03, 4.22742131e-04, -3.85349708e-03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.15537093e-03, -1.01259193e-04, -3.39857134e-03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -2.77083404e-03, 8.86899471e-04, -1.26423253e-02, -2.25378930e-03, 7.37800828e-04, -1.25744296e-02, 4.41033266e-03, 1.77644318e-03, -2.47845695e-02, 5.15906595e-03, 1.98553137e-03, -2.48167464e-02, 1.54889087e-02, 2.71947128e-03, -3.99588546e-02, 1.64902367e-02, 3.05690956e-03, -4.01029333e-02, -3.25135440e-03, 1.07339180e-03, -1.24275576e-02, 3.65590610e-03, 1.61909702e-03, -2.44657553e-02, 2.87714656e-03, 1.48850575e-03, -2.38879913e-02, 2.12508384e-03, 5.05731614e-04, -1.17516135e-02, 1.45199380e-02, 2.40365636e-03, -3.95857571e-02, 1.34647828e-02, 2.11314152e-03, -3.90191073e-02, -8.71615991e-03, 5.16334537e-04, -1.31165399e-02, -9.14938687e-03, 9.40904052e-04, -1.32136958e-02, -3.84378904e-03, 1.90501691e-03, -2.48434146e-02, -3.10643877e-03, 1.79234595e-03, -2.48911379e-02, 5.29049551e-03, 2.96292169e-03, -3.97255208e-02, 6.18545386e-03, 3.11882374e-03, -3.98703907e-02, 1.72941962e-02, 4.05381806e-03, -5.67952941e-02, 1.83719791e-02, 4.29285878e-03, -5.70278620e-02, -5.25111678e-03, 6.86931337e-04, -4.62987504e-03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -1.79183005e-03, 7.27888163e-04, -3.42882235e-03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -9.56613706e-03, 1.41584140e-03, -1.30346463e-02, -4.54132965e-03, 2.04879473e-03, -2.45523551e-02, -5.24889310e-03, 2.24243460e-03, -2.39874269e-02, -3.75018811e-03, 1.32165229e-03, -1.18855411e-02, 4.38723233e-03, 2.84561591e-03, -3.93629832e-02, 3.45982603e-03, 2.74825826e-03, -3.88014915e-02, 1.51129521e-02, 3.62302269e-03, -5.58600065e-02, 1.62379879e-02, 3.82980447e-03, -5.63970274e-02, -1.16946339e-02, 1.72610856e-03, -2.53183199e-02, -1.23726712e-02, 2.05575694e-03, -2.52893942e-02, -4.64843298e-03, 3.18148810e-03, -3.97731699e-02, -3.75101190e-03, 3.09866045e-03, -3.99311747e-02, 5.92243899e-03, 4.35858560e-03, -5.66392602e-02, 6.92523319e-03, 4.46872937e-03, -5.68757104e-02, 1.84992693e-02, 5.55491362e-03, -7.49763323e-02, 1.96246632e-02, 5.71280683e-03, -7.52759692e-02, -1.30152868e-02, 2.43515905e-03, -2.50259144e-02, -5.51145442e-03, 3.28766719e-03, -3.94301804e-02, -6.38174770e-03, 3.43108652e-03, -3.88793056e-02, -1.36437828e-02, 2.84228945e-03, -2.45188937e-02, -9.95792172e-03, 1.92789116e-03, -1.25558095e-02, -5.41282668e-03, 1.25008525e-03, -4.30443344e-03, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.91255111e-03, 4.27584555e-03, -5.62476284e-02, 3.88107775e-03, 4.20708797e-03, -5.57129891e-02, 1.62228610e-02, 5.26886047e-03, -7.40589310e-02, 1.73872055e-02, 5.40649483e-03, -7.45640984e-02, -1.39482622e-02, 3.17360387e-03, -4.02546006e-02, -1.48108446e-02, 3.41611277e-03, -4.01104987e-02, -5.23086975e-03, 4.64667378e-03, -5.66788185e-02, -4.21918034e-03, 4.58926092e-03, -5.69256164e-02, 6.34669315e-03, 5.90364158e-03, -7.48824065e-02, 7.42319488e-03, 5.97608764e-03, -7.51877737e-02, 1.92259315e-02, 7.17021809e-03, -9.39741894e-02, 2.03758824e-02, 7.26432593e-03, -9.43211487e-02, -1.56354517e-02, 3.69878744e-03, -3.97880493e-02, -6.21450588e-03, 4.72075841e-03, -5.63009843e-02, -7.20348577e-03, 4.82092036e-03, -5.57755962e-02, -1.64541870e-02, 4.00083071e-03, -3.92842385e-02, 5.26479500e-03, 5.84917321e-03, -7.44738659e-02, 4.16468661e-03, 5.80374732e-03, -7.39693168e-02, 1.69020827e-02, 6.99795987e-03, -9.30827179e-02, 1.80825777e-02, 7.08107509e-03, -9.35569112e-02, -1.55691546e-02, 4.77843891e-03, -5.71593850e-02, -1.65651328e-02, 4.94615255e-03, -5.69226465e-02, -5.62899321e-03, 6.24207407e-03, -7.49145908e-02, -4.54051494e-03, 6.20546420e-03, -7.52275114e-02, 6.60554211e-03, 7.54795842e-03, -9.39265013e-02, 7.72788163e-03, 7.59085873e-03, -9.42797439e-02, 1.95957795e-02, 8.85777015e-03, -1.13377758e-01, 2.07539191e-02, 8.90549218e-03, -1.13754642e-01, -1.75251766e-02, 5.14386473e-03, -5.65596035e-02, -6.69575068e-03, 6.28980879e-03, -7.45153648e-02, -7.76634048e-03, 6.35439870e-03, -7.40188823e-02, -1.84848013e-02, 5.35429797e-03, -5.60703114e-02, 5.47926816e-03, 7.51556984e-03, -9.35107931e-02, 4.33851032e-03, 7.48841118e-03, -9.30361062e-02, 1.72580493e-02, 8.76848151e-03, -1.12515240e-01, 1.84387555e-02, 8.81160047e-03, -1.12961956e-01, -1.66590026e-02, 6.48145855e-03, -7.53861909e-02, -1.77456700e-02, 6.58817712e-03, -7.50801195e-02, -5.87895559e-03, 7.92011481e-03, -9.39511842e-02, -4.74358947e-03, 7.89952787e-03, -9.43093676e-02, 6.74154577e-03, 9.25214568e-03, -1.13360205e-01, 7.88864244e-03, 9.27364626e-03, -1.13742628e-01, 1.97309001e-02, 1.05858576e-02, -1.32893699e-01, 2.08875112e-02, 1.06044309e-02, -1.33285604e-01, -1.88005559e-02, 6.71592366e-03, -7.46910228e-02, -6.99867888e-03, 7.94745765e-03, -9.35413243e-02, -8.12114655e-03, 7.98449830e-03, -9.30734194e-02, -1.98588341e-02, 6.85113362e-03, -7.42207919e-02, 5.59154550e-03, 9.23572313e-03, -1.12944654e-01, 4.43093028e-03, 9.22191258e-03, -1.12496924e-01, 1.73980243e-02, 1.05490655e-02, -1.32058577e-01, 1.85707892e-02, 1.05664872e-02, -1.32483703e-01, -1.73230584e-02, 8.23688412e-03, -9.44076013e-02, -1.84656434e-02, 8.29689372e-03, -9.40536411e-02, -6.01640213e-03, 9.64418770e-03, -1.13377144e-01, -4.85657507e-03, 9.63477407e-03, -1.13762220e-01, 6.79666921e-03, 1.09873200e-02, -1.32889842e-01, 7.95403893e-03, 1.09952652e-02, -1.33284988e-01, 1.97546630e-02, 1.23294089e-02, -1.52348388e-01, 2.09096159e-02, 1.23341559e-02, -1.52739847e-01, -1.95821067e-02, 8.37060475e-03, -9.36501855e-02, -7.16621515e-03, 9.65715428e-03, -1.12964900e-01, -8.31813590e-03, 9.67477285e-03, -1.12522424e-01, -2.07044069e-02, 8.44780382e-03, -9.31999890e-02, 5.63665153e-03, 1.09809325e-02, -1.32479811e-01, 4.46975249e-03, 1.09758163e-02, -1.32053876e-01, 1.74274845e-02, 1.23174882e-02, -1.51536894e-01, 1.85948113e-02, 1.23227240e-02, -1.51947229e-01, -1.76674771e-02, 1.00104241e-02, -1.13814489e-01, -1.88391070e-02, 1.00382157e-02, -1.13431622e-01, -6.07647462e-03, 1.13889754e-02, -1.32898994e-01, -4.90709091e-03, 1.13857608e-02, -1.33295107e-01, 6.81022825e-03, 1.27337220e-02, -1.52340154e-01, 7.96956931e-03, 1.27352914e-02, -1.52731354e-01, -1.99914304e-02, 1.00740707e-02, -1.13022933e-01, -7.24047553e-03, 1.13936232e-02, -1.32490643e-01, -8.40675965e-03, 1.13997573e-02, -1.32068043e-01, -2.11505930e-02, 1.01107631e-02, -1.12590940e-01, 5.64752969e-03, 1.27324031e-02, -1.51939798e-01, 4.48007697e-03, 1.27315894e-02, -1.51530584e-01, -1.77978311e-02, 1.17792867e-02, -1.33314887e-01, -1.89803417e-02, 1.17895121e-02, -1.32919854e-01, -6.09203214e-03, 1.31413487e-02, -1.52340119e-01, -4.91838817e-03, 1.31401012e-02, -1.52728812e-01, -2.01502602e-02, 1.18036901e-02, -1.32513161e-01, -7.26094114e-03, 1.31439238e-02, -1.51941933e-01, -8.43306553e-03, 1.31470828e-02, -1.51535707e-01, -2.13271914e-02, 1.18171179e-02, -1.32095247e-01, -1.78219878e-02, 1.35388438e-02, -1.52734445e-01, -1.90083249e-02, 1.35433130e-02, -1.52345227e-01, -2.01831243e-02, 1.35520431e-02, -1.51947351e-01, -2.13654020e-02, 1.35603213e-02, -1.51541675e-01])
According to http://yt-project.org/doc/examining/loading_data.html in the Unstructured Grid Data section and from standard convection of volumetric meshes in OpenGL extra there should be a connectivity (vertex) array which seems to be given by
Mconn=mesh.cells()
print(Mconn.shape)
#recast from unit32 to int64
Mconn=Mconn.astype(np.int64)
Mconn
(540, 4)
array([[ 0, 1, 12, 56], [ 0, 1, 45, 56], [ 0, 44, 45, 56], ..., [119, 130, 131, 175], [119, 163, 174, 175], [119, 130, 174, 175]])
data = dict({('connect1', 'displacment') : U[Mconn]})
ds = yt.load_unstructured_mesh(Mconn, Mcoords, node_data=data)
yt : [INFO ] 2018-05-17 11:55:13,143 Parameters: current_time = 0.0 yt : [INFO ] 2018-05-17 11:55:13,144 Parameters: domain_dimensions = [2 2 2] yt : [INFO ] 2018-05-17 11:55:13,145 Parameters: domain_left_edge = [0. 0. 0.] yt : [INFO ] 2018-05-17 11:55:13,146 Parameters: domain_right_edge = [1.1 0.22 0.22] yt : [INFO ] 2018-05-17 11:55:13,148 Parameters: cosmological_simulation = 0.0
sl = yt.SlicePlot(ds, 'x', 'displacment')
sl.annotate_mesh_lines()
yt : [INFO ] 2018-05-17 11:55:13,302 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:13,303 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:13,305 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:13,305 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:13,307 Making a fixed resolution buffer of (('all', 'displacment')) 800 by 800 yt : [WARNING ] 2018-05-17 11:55:13,446 Plot image for field ('all', 'displacment') has both positive and negative values. Min = -0.030955, Max = 0.009722. yt : [WARNING ] 2018-05-17 11:55:13,446 Switching to symlog colorbar scaling unless linear scaling is specified later
sl = yt.SlicePlot(ds, 'y', 'displacment')
sl.annotate_mesh_lines()
yt : [INFO ] 2018-05-17 11:55:14,611 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:14,613 ylim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:14,617 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:14,618 ylim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:14,623 Making a fixed resolution buffer of (('all', 'displacment')) 800 by 800 yt : [WARNING ] 2018-05-17 11:55:14,774 Plot image for field ('all', 'displacment') has both positive and negative values. Min = -0.030411, Max = 0.007552. yt : [WARNING ] 2018-05-17 11:55:14,775 Switching to symlog colorbar scaling unless linear scaling is specified later
sl = yt.SlicePlot(ds, 'z', 'displacment')
sl.annotate_mesh_lines()
yt : [INFO ] 2018-05-17 11:55:16,246 xlim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:16,247 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:16,249 xlim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:16,250 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:16,255 Making a fixed resolution buffer of (('all', 'displacment')) 800 by 800 yt : [WARNING ] 2018-05-17 11:55:16,419 Plot image for field ('all', 'displacment') has both positive and negative values. Min = -0.034565, Max = 0.014873. yt : [WARNING ] 2018-05-17 11:55:16,420 Switching to symlog colorbar scaling unless linear scaling is specified later
sc = yt.create_scene(ds)
#transfer function
scTF = TransferFunctionHelper(ds)
scTF.set_field('displacment')
scTF.set_bounds((abs(U.mean()-U.std()), U.mean()+U.std()))
scTF.set_log(True)
sc.transfer_function=scTF.tf
#camera controls
cam = sc.add_camera()
cam.position = np.array([0.5, 0.5, -1.0])
cam.north_vector = np.array([0.0, 1.0, 1.0])
sc.annotate_axes()
sc.annotate_domain(ds)
sc.camera.set_width(ds.quan(.009, 'm'))
sc.show(sigma_clip=2)
scTF.plot()
yt : [INFO ] 2018-05-17 11:55:17,127 Setting default field to ('connect1', 'displacment') yt : [INFO ] 2018-05-17 11:55:17,163 Rendering scene (Can take a while).
S=von_Mises.vector().get_local(); S.shape
(176,)
data = dict({('connect1', 'stress') : S[Mconn]})
ds = yt.load_unstructured_mesh(Mconn, Mcoords, node_data=data)
yt : [INFO ] 2018-05-17 11:55:18,814 Parameters: current_time = 0.0 yt : [INFO ] 2018-05-17 11:55:18,815 Parameters: domain_dimensions = [2 2 2] yt : [INFO ] 2018-05-17 11:55:18,816 Parameters: domain_left_edge = [0. 0. 0.] yt : [INFO ] 2018-05-17 11:55:18,818 Parameters: domain_right_edge = [1.1 0.22 0.22] yt : [INFO ] 2018-05-17 11:55:18,822 Parameters: cosmological_simulation = 0.0
sl = yt.SlicePlot(ds, 'x', 'stress')
sl.annotate_mesh_lines()
yt : [INFO ] 2018-05-17 11:55:18,976 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:18,977 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:18,978 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:18,979 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:18,982 Making a fixed resolution buffer of (('all', 'stress')) 800 by 800
sl = yt.SlicePlot(ds, 'y', 'stress')
sl.annotate_mesh_lines()
yt : [INFO ] 2018-05-17 11:55:20,141 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:20,142 ylim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:20,144 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:20,148 ylim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:20,150 Making a fixed resolution buffer of (('all', 'stress')) 800 by 800
sl = yt.SlicePlot(ds, 'z', 'stress')
sl.annotate_mesh_lines()
yt : [INFO ] 2018-05-17 11:55:21,786 xlim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:21,789 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:21,792 xlim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:21,794 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:21,799 Making a fixed resolution buffer of (('all', 'stress')) 800 by 800
sc = yt.create_scene(ds)
#transfer function
scTF = TransferFunctionHelper(ds)
scTF.set_field('stress')
scTF.set_bounds((abs(S.mean()-S.std()), S.mean()+S.std()))
scTF.set_log(True)
sc.transfer_function=scTF.tf
#camera controls
cam = sc.add_camera()
cam.position = np.array([0.5, 0.5, -1.0])
cam.north_vector = np.array([0.0, 1.0, 1.0])
sc.annotate_axes()
sc.annotate_domain(ds)
sc.camera.set_width(ds.quan(.009, 'm'))
sc.show(sigma_clip=2)
scTF.plot()
yt : [INFO ] 2018-05-17 11:55:22,733 Setting default field to ('connect1', 'stress') yt : [INFO ] 2018-05-17 11:55:22,783 Rendering scene (Can take a while).
u_magnitude
UM=u_magnitude.vector().get_local(); UM.shape
(176,)
data = dict({('connect1', 'disMag') : UM[Mconn]})
ds = yt.load_unstructured_mesh(Mconn, Mcoords, node_data=data)
yt : [INFO ] 2018-05-17 11:55:24,653 Parameters: current_time = 0.0 yt : [INFO ] 2018-05-17 11:55:24,654 Parameters: domain_dimensions = [2 2 2] yt : [INFO ] 2018-05-17 11:55:24,658 Parameters: domain_left_edge = [0. 0. 0.] yt : [INFO ] 2018-05-17 11:55:24,660 Parameters: domain_right_edge = [1.1 0.22 0.22] yt : [INFO ] 2018-05-17 11:55:24,662 Parameters: cosmological_simulation = 0.0
sl = yt.SlicePlot(ds, 'x', 'disMag')
sl.annotate_mesh_lines()
yt : [INFO ] 2018-05-17 11:55:25,002 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:25,004 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:25,008 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:25,012 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:25,017 Making a fixed resolution buffer of (('all', 'disMag')) 800 by 800
sl = yt.SlicePlot(ds, 'y', 'disMag')
sl.annotate_mesh_lines()
yt : [INFO ] 2018-05-17 11:55:26,041 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:26,043 ylim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:26,045 xlim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:26,047 ylim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:26,056 Making a fixed resolution buffer of (('all', 'disMag')) 800 by 800
sl = yt.SlicePlot(ds, 'z', 'disMag')
sl.annotate_mesh_lines()
yt : [INFO ] 2018-05-17 11:55:27,708 xlim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:27,709 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:27,711 xlim = 0.000000 1.100000 yt : [INFO ] 2018-05-17 11:55:27,713 ylim = 0.000000 0.220000 yt : [INFO ] 2018-05-17 11:55:27,715 Making a fixed resolution buffer of (('all', 'disMag')) 800 by 800
sc = yt.create_scene(ds)
#transfer function
scTF = TransferFunctionHelper(ds)
scTF.set_field('disMag')
scTF.set_bounds((abs(UM.mean()-UM.std()), UM.mean()+UM.std()))
scTF.set_log(True)
sc.transfer_function=scTF.tf
#camera controls
cam = sc.add_camera()
cam.position = np.array([0.5, 0.5, -1.0])
cam.north_vector = np.array([0.0, 1.0, 1.0])
sc.annotate_axes()
sc.annotate_domain(ds)
sc.camera.set_width(ds.quan(.009, 'm'))
sc.show(sigma_clip=2)
scTF.plot()
yt : [INFO ] 2018-05-17 11:55:28,513 Setting default field to ('connect1', 'disMag') yt : [INFO ] 2018-05-17 11:55:28,538 Rendering scene (Can take a while).
As can be seen above yt is now a viable alternative to VTK and similar 3d visualization tools to visualize the outputs of FEniCS. And while the above leaves much to be desired in terms of normal features that VTK especially in terms of the volumetric rendering it is a good start that will hopefully lead to more duel use of yt and FEniCS and contributions to yt to give it all the features it needs to move beyond the narrow use Astro-cosmological visualizations to becoming python defacto volumetric rendering library.