Recursion and Recursively Defined Geometry Objects - COMSOL Blog
Recursion and Recursively Defined Geometry Objects - COMSOL Blog
Recursively defined geometrical structures may be useful in engineering applications such as broadband
antennas and metamaterials, to mention a few. For a brief overview of some applications, check out a
previous blog post on fractals (/blogs/fractals-beyond-eye-candy/). In this blog post, we will take a look at
how to quickly generate such geometry objects by algorithmic means.
Here, we will demonstrate recursion by two famous examples of recursive structures: a Sierpinski carpet
(https://en.wikipedia.org/wiki/Sierpinski_carpet) in 2D and a Menger sponge
(https://en.wikipedia.org/wiki/Menger_sponge) in 3D. The code for these examples are short enough so that
we can list them in full, and there are links to download these examples at the end of the post.
Sierpinski Carpet
The following method, create_carpet, runs the recursion to a certain recursive level according to an input
parameter level. It sets a limit at 5 levels in order to avoid creating an exceedingly large geometry. (You can
change this if you think your computer can handle it.)
if (level < 1)
if (level > 5)
counter = 0;
model.component("comp1").geom("geom1").feature().clear();
model.component("comp1").geom("geom1").autoRebuild("off");
Cookies Settings
By clicking “Accept All Cookies”, you agree to the storing of
double cx0 = 0, cy0 = 0;
The method create_carpet, in turn, calls the main recursive function carpet, listed below, using four input
arguments for the recursion level, the center x- and y-coordinates, and the current side length.
The input arguments are defined in the method’s Settings window, as shown below.
Cookies Settings
By clicking “Accept All Cookies”, you agree to the storing of
cookies on your device to enhance site navigation, analyze site
Accept All
usage, and assist in our marketing efforts. By continuing to use
Cookies
our site, you agree to our use of cookies.
This is the code for the method carpet:
Cookies Settings
By clicking “Accept All Cookies”, you agree to the storing of
cookies on your device to enhance site navigation, analyze site
Accept All
usage, and assist in our marketing efforts. By continuing to use
Cookies
our site, you agree to our use of cookies.
int l = level;
String strix;
int l1;
posx = cx+i*si-si;
posy = cy+j*si-si;
strix = toString(i)+toString(j);
if ((Math.abs((i-1))+Math.abs((j-1))) > 0) {
if (l == 1) {
counter = counter+1;
model.component("comp1").geom("geom1").create("sq"+strix+"C"+toString(count
with(model.component("comp1").geom("geom1").feature("sq"+strix+"C"+toString
set("base", "center");
endwith();
model.component("comp1").geom("geom1").feature("sq"+strix+"C"+toString(coun
} else {
l1 = l-1;
si1 = si/3;
Note that in order to get a consecutive numbering of the square objects, an integer variable counter is used
(under Declarations), as shown below.
Cookies Settings
By clicking “Accept All Cookies”, you agree to the storing of
cookies on your device to enhance site navigation, analyze site
Accept All
usage, and assist in our marketing efforts. By continuing to use
Cookies
our site, you agree to our use of cookies.
The code for the two functions are shown below, side by side, in the Method Editor:
Cookies Settings
By clicking “Accept All Cookies”, you agree to the storing of
cookies on your device to enhance site navigation, analyze site
Accept All
usage, and assist in our marketing efforts. By continuing to use
Cookies
our site, you agree to our use of cookies.
The figure below shows the resulting geometry for a level 3 carpet.
Note that the number of square geometry objects defined for the level 3 carpet is 512. The number of
geometry objects for a level N carpet is 8N. This means that, for the maximum level 5 set by the method
create_carpet, the resulting geometry has 32,768 geometry objects! If you try a level 5 carpet, make sure you
have a powerful enough computer!
Menger Sponge
We have gone over a recursively defined geometry object in 2D, but what about 3D objects? The 3D version
of a Sierpinski carpet is called a Menger sponge. The methods for the Sierpinski carpet readily generalize to
3D. However, in 3D, you need to be careful not to generate more objects than your computer can handle.
if (level < 1)
if (level > 3)
counter = 0;
model.component("comp1").geom("geom1").feature().clear();
model.component("comp1").geom("geom1").autoRebuild("off");
double si0 = 1;
int l1;
posx = cx+i*si-si;
posy = cy+j*si-si;
posz = cz+k*si-si;
strix = toString(i)+toString(j)+toString(k);
if ((Math.abs((i-1))+Math.abs((j-1))+Math.abs((k-1))) > 1) {
if (l == 1) {
counter = counter+1;
model.component("comp1").geom("geom1").create("blk"+strix+"C"+toString(co
with(model.component("comp1").geom("geom1").feature("blk"+strix+"C"+toStr
set("base", "center");
set("size", new String[]{toString(si), toString(si), toString(si)});
endwith();
model.component("comp1").geom("geom1").feature("blk"+strix+"C"+toString(c
} else {
l1 = l-1;
si1 = si/3;
The figure below shows the resulting geometry for a level 2 sponge.
Cookies Settings
By clicking “Accept All Cookies”, you agree to the storing of
cookies on your device to enhance site navigation, analyze site
Accept All
usage, and assist in our marketing efforts. By continuing to use
Cookies
our site, you agree to our use of cookies.
In this case, the number of geometry objects grows with the level N as 20N, and the level 2 sponge shown
above has 400 block geometry objects.
You don’t have to use recursion to create this type of structure, as demonstrated in a previous blog post on
an iterative approach to creating a Koch snowflake (/blogs/using-the-application-builder-to-create-a-koch-
snowflake/).
If you have any of the add-on products for additional CAD functionality, you can export these geometry
objects on standard CAD formats. Without an add-on product, you can still export the meshed geometry to
any of the supported mesh formats, such as STL.
Next Step
Try the models featured in this blog post by clicking the button below, which will take you to the Application
Gallery.
Cookies Settings
By clicking “Accept All Cookies”, you agree to the storing of
cookies on your device to enhance site navigation, analyze site
Accept All
usage, and assist in our marketing efforts. By continuing to use
Cookies
our site, you agree to our use of cookies.
Cookies Settings
By clicking “Accept All Cookies”, you agree to the storing of
cookies on your device to enhance site navigation, analyze site
Accept All
usage, and assist in our marketing efforts. By continuing to use
Cookies
our site, you agree to our use of cookies.