# How can we draw a stencil for discretization of PDEs?

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?

or

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.

• 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) – MarcoB Feb 2 '16 at 19:28
• 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. – Fazlollah Feb 2 '16 at 19:45
• 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. – MarcoB Feb 2 '16 at 20:44
• Thanks. I did as you suggested. – Fazlollah Feb 2 '16 at 21:16
• 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. – Sjoerd C. de Vries Feb 2 '16 at 22:25

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@
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}],
Flatten[#, 1] & /@ {labels, points}]}]


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}];


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@
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}],

• @FazlollahSoleymani Sorry, it was a (small) error in the labels command. (Using - twice...). I fixed it and updated the plots. – Anton Antonov Feb 3 '16 at 18:42