0
$\begingroup$

How can we draw some multi-node (e.g. 5-node, 9-node, etc.) stencils that occur in the process of discretization of PDEs, such as the following ones?

enter image description here

or

enter image description here

As a matter of fact, I would like to know that how we can use Mathematica tools to draw such stencils in 3D! I mean, when there are discretizations along three dimensions x, y and z.

$\endgroup$
  • $\begingroup$ Do you simply need to generate the graphical representation, or do you need to carry out some operation on those grids? Would something along the lines of the following work? Graphics[{PointSize[0.05], Table[Point@{x, y}, {x, -2, 2, 1}, {y, -2, 1, 1}]}, GridLines -> {Range[-2, 2], Range[-2, 1]}, GridLinesStyle -> Thick] (output) $\endgroup$ – MarcoB Feb 2 '16 at 19:28
  • $\begingroup$ I would like to show the indices, i, j, k and the step-sizes along the three dimensions x,y,z. Furthermore, it would be of interest if e.g. the nine central nodes are colorized with black color and the other nodes are empty. $\endgroup$ – Fazlollah Feb 2 '16 at 19:45
  • 1
    $\begingroup$ Fazlollah, I noticed that you have asked a few questions on this site so far, some of which have received multiple excellent answers, but you have only ever accepted one answer. Accepting appropriate answers is considered good form on this site, and part of the reward for the effort people spend in helping you. I find it hard to believe that none of the answers to your other questions were good enough to be accepted. Perhaps you could go back and accept good answers to your previous questions (if there are any) before you ask for further help in this forum. $\endgroup$ – MarcoB Feb 2 '16 at 20:44
  • $\begingroup$ Thanks. I did as you suggested. $\endgroup$ – Fazlollah Feb 2 '16 at 21:16
  • 1
    $\begingroup$ All you have to do is look-up the following functions in the documentation: Graphics, Line, Point, Text, Arrow and the work is almost done. $\endgroup$ – Sjoerd C. de Vries Feb 2 '16 at 22:25
4
$\begingroup$

As Sjord C. de Vries says in the comments the required stencils can be made with the standard graphics functions. Probably it is better or easier to use some of the new Region and finite element functions.

2D grid

Here is code using Graphics primitives:

points = Table[{i, j}, {i, 1, 6}, {j, 1, 5}];

grid = Join[
   Map[Line /@ Partition[#, 2, 1] &, points],
   Map[Line /@ Partition[#, 2, 1] &, Transpose[points]]];

labelsCenter = {3, 3};
labelsSizes = {3, 2};
labelsVars = {i, j};

labels = Rationalize@
   Map[Thread[
      labelsVars + (# - points[[Sequence @@ labelsCenter]])] &, 
    points, {2}];

crossStencil = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

Graphics[{Green, PointSize[0.04], 
  Point@Map[points[[Sequence @@ (labelsCenter + #)]] &, crossStencil],
   Red, Point[points[[Sequence @@ labelsCenter]]], Black, grid, 
  PointSize[0.02], Map[Point, points, {-2}], 
  MapThread[Text[##, -{1.5, 2}] &, 
   Flatten[#, 1] & /@ {labels, points}]}]

enter image description here

Note that the code can take non-integer steps. (That is why Rationalize is used for the labels.) Here is a picture made with the points

points = Table[{i, j}, {i, 1, 6}, {j, 1, 5, 0.8}];

enter image description here

3D grid

Changing the 2D code to make 3D grids is not that trivial that is why the 3D code is given below. Since the node labels become too many and kind of clutter the graphics the labels are filtered to be put for the stencil points only.

points = Table[{i, j, k}, {i, 1, 3}, {j, 1, 3}, {k, 1, 3}];

grid = Join[
   Map[Line /@ Partition[#, 2, 1] &, points, {2}],
   Map[Line /@ Partition[#, 2, 1] &, 
    Transpose[points, {2, 3, 1}], {2}],
   Map[Line /@ Partition[#, 2, 1] &, 
    Transpose[points, {3, 1, 2}], {2}]];

labelsCenter = {2, 2, 2};
labelsSizes = {3, 2, 2};
labelsVars = {i, j, k};

labels = Rationalize@
   Map[Thread[
      labelsVars + (# - points[[Sequence @@ labelsCenter]])] &, 
    points, {3}];

crossStencil = 
  Join[Table[ReplacePart[{0, 0, 0}, i -> 1], {i, 3}], 
   Table[ReplacePart[{0, 0, 0}, i -> -1], {i, 3}]];

Graphics3D[{Green, PointSize[0.04], 
  Point@Map[points[[Sequence @@ (labelsCenter + #)]] &, crossStencil],
   Red, Point[points[[Sequence @@ labelsCenter]]], Black, grid, 
  PointSize[0.02], Map[Point, points, {-2}], 
  MapThread[
   Text[##, -{1.5, 2}] &, {Map[
     labels[[Sequence @@ (labelsCenter + #)]] &, 
     Append[crossStencil, {0, 0, 0}]], 
    Map[points[[Sequence @@ (labelsCenter + #)]] &, 
     Append[crossStencil, {0, 0, 0}]]}]}, Boxed -> False]

enter image description here

$\endgroup$
  • $\begingroup$ Wow. Thanks. The 3D plot is totally useful for showing the discretizations. Just one quick note. Usually, for the 2D case, the node left to (i,j) is (i-1,j) and the right one is (i+1,j). But here, it is vice versa. Although the plot is correct, I wished to know that how we can reverse this sorting of indices in the code. $\endgroup$ – Fazlollah Feb 3 '16 at 17:30
  • $\begingroup$ @FazlollahSoleymani Sorry, it was a (small) error in the labels command. (Using - twice...). I fixed it and updated the plots. $\endgroup$ – Anton Antonov Feb 3 '16 at 18:42

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.