3
$\begingroup$

I am trying to make animations of results of a FEM solver from an external model. Basically, I am pre- and post-processing in MMA. As a simple example, let's say I have a mesh such as:

Needs["NDSolve`FEM`"]
mesh = ToElementMesh[
   "Coordinates" -> {{0., 0.}, {1., 0.}, {2., 0.}, {2., 1.}, {1., 
      1.}, {0., 1.}}, 
   "MeshElements" -> {TriangleElement[{{1, 2, 5}, {5, 6, 1}, {2, 3, 
        4}, {4, 5, 2}}]}];
Show[mesh[
  "Wireframe"[
   "MeshElementStyle" -> {Directive[FaceForm[Green]], 
     Directive[FaceForm[Red]]}]], 
 mesh["Wireframe"["MeshElementIDStyle" -> Red]], 
 mesh["Wireframe"["MeshElement" -> "PointElements", 
   "MeshElementIDStyle" -> Blue]]]

enter image description here

And let's say the solution for each element would be f[i_,t_]:=Sin[ i t] where i is the element id and t is time. Now I need to make the FaceForm[] work with color data. I can create a list of colors for element 1 for example with Table[ColorData["TemperatureMap"][Sin[t]], {ii, Length[First@First@mesh["MeshElements"]]}, {t, 0, 10, 0.1}]. But how do I bring it all together and make each element read the color from its solution function f[i] to be able to animate later?

$\endgroup$
4
  • $\begingroup$ What equations are you solving that the current FEM solver can not solve? $\endgroup$
    – user21
    Feb 17 at 6:45
  • $\begingroup$ @user21 It's not that it cannot be solved, I've been assigned a task to make simulations with a model that another research group we are collaborating with have created (very large model which took them more than a decade to develop). The pre and post processing has been patchy based on who has been working on it and is a combination of Fortran, C, Bash, Perl, Python and some commercial software. I realized I can do almost all those in MMA very quickly. But I won't be able to reproduce their whole model easily. $\endgroup$
    – MathX
    Feb 17 at 10:52
  • $\begingroup$ @user21 also, you've been so great with your help all across the website (and I assume for Wolfram), I would definitely tell you what model/equations I am working on. I just don't want to mention it in searchable forums just to be safe. $\endgroup$
    – MathX
    Feb 17 at 11:13
  • $\begingroup$ Sure I understand, I was just curious of what the usage scenario is. $\endgroup$
    – user21
    Feb 17 at 11:31

1 Answer 1

3
$\begingroup$

There is an example of this on the ElementMeshInterpolation ref page.

Here is how I'd do it. I'd use ElementMeshInterpolation for this:

Needs["NDSolve`FEM`"]
mesh = ToElementMesh[
   "Coordinates" -> {{0., 0.}, {1., 0.}, {2., 0.}, {2., 1.}, {1., 
      1.}, {0., 1.}}, 
   "MeshElements" -> {TriangleElement[{{1, 2, 5}, {5, 6, 1}, {2, 3, 
        4}, {4, 5, 2}}]}];

(* create example data  *)
f[X_, t_] := Sin[Total[X^2, {2}] t]
coords = mesh["Coordinates"];
exampleTimes = Range[0, 1, 0.1];
exampleValues = {f[coords, #]} & /@ exampleTimes;

Note the dimensions of the values:

Dimensions[exampleValues]
(*{11, 1, 6}*)

Create the interpolating function:

tif = ElementMeshInterpolation[{exampleTimes, mesh}, exampleValues]

Visualize:

Manipulate[Plot3D[tif[t, x, y], {x, y} \[Element] mesh], {t, 0, 1}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks a lot! I had done something similar by creating interpolation of tables of coordinates and the solution values for each node, didn't know this existed. I wish the FEM package functions would show up in the Wolfram website search easier... $\endgroup$
    – MathX
    Feb 17 at 10:55
  • 1
    $\begingroup$ @MathX, I share that sentiment about the FEM functionality not being findable - if you want to do me a favor, report this to the support. It sometimes helps if this is requested by customers. $\endgroup$
    – user21
    Feb 17 at 11:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.