10
$\begingroup$

I'm doing FEM in Mathematica and want to visualize the results in Paraview (e.g., create a video of the displacement field). Now I have the element mesh, the displacement and stress fields. How can I convert the data into VTK format as readable by Paraview?

The minimum code is as follows.

ClearAll["Global`*"]

Needs["NDSolve`FEM`"]

op = {Inactive[
       Div][({{0, (Y ν)/((1 - 2 ν) (1 + ν))}, {Y/(
          2 (1 + ν)), 0}}.Inactive[Grad][v[x, y], {x, y}]), {x, 
       y}] + Inactive[
       Div][({{(Y (1 - ν))/((1 - 2 ν) (1 + ν)), 0}, {0, 
          Y/(2 (1 + ν))}}.Inactive[Grad][u[x, y], {x, y}]), {x, 
       y}], Inactive[
       Div][({{0, Y/(2 (1 + ν))}, {(
          Y ν)/((1 - 2 ν) (1 + ν)), 0}}.Inactive[Grad][
         u[x, y], {x, y}]), {x, y}] + 
     Inactive[
       Div][({{Y/(2 (1 + ν)), 0}, {0, (
          Y (1 - ν))/((1 - 2 ν) (1 + ν))}}.Inactive[Grad][
         v[x, y], {x, y}]), {x, y}]} /. {Y -> 2.5, ν -> 1/4};

Subscript[Γ, 
  D] = {DirichletCondition[{u[x, y] == 0.}, x == 0], 
   DirichletCondition[{v[x, y] == 0.}, y == 0]};

b2 = 1;
(*b2=Piecewise[{{1.5x y,y<1},{1.5,y≥1}}];*)

{ufun, vfun} = 
  NDSolveValue[{op == {0, b2}, Subscript[Γ, D]}, {u, 
    v}, {x, 0, 1}, {y, 0, 2}]; 
(*∇·σ+Overscript[b, ⇀]=0, given \
Overscript[b, ⇀]={0,-1}, compare the result with that of \
"LE_Example_Single_Step.nb"*)

mesh = ufun["ElementMesh"];
deformedMesh = ElementMeshDeformation[mesh, {ufun, vfun}];
Show[{mesh["Wireframe"], 
  deformedMesh[
   "Wireframe"[
    "ElementMeshDirective" -> 
     Directive[EdgeForm[{Thin, Red}], FaceForm[]]]]}]

ContourPlot[ufun[x, y], {x, y} ∈ mesh, 
 AspectRatio -> Automatic, PlotRange -> All, 
 ColorFunction -> ColorData["LightTemperatureMap"], Contours -> 50, 
 ContourLines -> False, ImageSize -> 200, PlotLegends -> Automatic]
ContourPlot[vfun[x, y], {x, y} ∈ mesh, 
 AspectRatio -> Automatic, PlotRange -> All, 
 ColorFunction -> ColorData["LightTemperatureMap"], Contours -> 50, 
 ContourLines -> False, ImageSize -> 200, PlotLegends -> Automatic]
$\endgroup$
  • 2
    $\begingroup$ What's the reason you do not want to use Mathematica for the visualization? It might be easier than to write an export for ElementMesh and interpolating functions - though not undoable. $\endgroup$ – user21 Nov 14 '17 at 1:10
  • $\begingroup$ Did you manage to export the data to paraview? I would like to do the same. $\endgroup$ – KratosMath Dec 17 '18 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.