an open source framework for integrated additive manufacturing and level...

11
1 AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL-SET BASED TOPOLOGY OPTIMIZATION Panagiotis Vogiatzis Computational Modeling, Analysis and Design Optimization Research Laboratory Department of Mechanical Engineering State University of New York at Stony Brook Stony Brook, NY, 11794 Email: [email protected] Shikui Chen 1 Computational Modeling, Analysis and Design Optimization Research Laboratory Department of Mechanical Engineering State University of New York at Stony Brook Stony Brook, NY, 11794 Email: [email protected] Chi Zhou Department of Industrial and Systems Engineering State University of New York at Buffalo Buffalo, New York, 14260 1 Address all correspondence to this author. ABSTRACT Topology optimization has been considered as a promising tool for conceptual design due to its capability of generating innovative design candidates without depending on the designer’s intuition and experience. Various optimization methods have been developed through the years and one of the promising options is the level-set-based topology optimization method. The benefit of this alternative method is that the design is characterized by its clear boundaries. This advantage can avoid post-processing work in conventional topology optimization process to a large extent and realize direct integration between topology optimization and additive manufacturing. In this paper, practical algorithms and a MATLAB-based open source framework are developed to seamlessly integrate the level-set-based topology optimization procedure with additive manufacturing process by converting the design to STL (STereoLithography) files, which is de facto standard format for 3D printing. The proposed algorithm and code are evaluated by a proof-of-concept demonstration with 3D printing of both single and multimaterial topology optimization results. The algorithm and the open source framework proposed in this paper will be beneficial to the areas of computational design and additive manufacturing. INTRODUCTION Topology optimization can be considered as seeking the optimal distribution of one or more materials in a prescribed design domain according to a performance function. Since the pioneering work of Bendsøe and Kikuchi in 1988 [1], several methods of topology optimization have been developed, including homogenization [1-3], evolutionary method [4], Solid Isotropic Material with Penalization (SIMP) method [5, 6], and level-set methods [7-10]. Even though the eye catching advantage of the level-set methods is that no post-processing work is needed, however such method is not 100% accurate. Since dealing with computer-driven analysis, that includes optimization algorithms combined with Finite Element Analysis, computational time and resources have to be accounted. That leads to common techniques such as downgrading 3D problems to 2D and applying symmetries where possible. Metamaterial examples are also worth mentioning, since they are a combination of periodically assembled unit cells. In these cases, some post- processing work is practically needed: adding thickness, mirroring, repeating the design periodically or even a combination of these. Scaling and rotating the design are also useful tools for achieving the desired product. Additive Manufacturing (AM) such as Stereolithography (SLA), Fused Deposition Modeling (FDM) and Selective Laser Sintering (SLS) can fabricate parts directly from Computer- Aided design (CAD) models without specific tooling and fixtures. The main distinguishing feature of AM technologies is their layer-wise fabrication approach. The ability of selectively place (multi) materials in 3D spaces provides unique design opportunities and capabilities to create any complex geometric shape and compositional material distribution. Conventional design practice relies heavily on designers’ intuition and restricted by the manufacturing technologies. Topology optimization is a powerful computational tool which recasts the design problem as an optimal material distribution problem so that the system will find an efficient geometry which fulfills the requirements quantified by the objective and constraint functions in the optimization scheme. However, topology optimization results exist in complicated freeform geometries, which pose great challenges for conventional manufacturing technologies. The direct rationale implies that the topology optimization would be one of the best technologies for design

Upload: others

Post on 25-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL …me.eng.stonybrook.edu/~chen/Downloads/TO_and_AM_preprint.pdf · 2016-09-22 · AN OPEN SOURCE FRAMEWORK

1

AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE

MANUFACTURING AND LEVEL-SET BASED TOPOLOGY OPTIMIZATION

Panagiotis Vogiatzis Computational Modeling, Analysis and Design

Optimization Research Laboratory Department of Mechanical Engineering

State University of New York at Stony Brook Stony Brook, NY, 11794

Email: [email protected]

Shikui Chen1 Computational Modeling, Analysis and Design

Optimization Research Laboratory Department of Mechanical Engineering

State University of New York at Stony Brook Stony Brook, NY, 11794

Email: [email protected]

Chi Zhou Department of Industrial and Systems Engineering

State University of New York at Buffalo Buffalo, New York, 14260

1 Address all correspondence to this author.

ABSTRACT Topology optimization has been considered as a promising tool

for conceptual design due to its capability of generating

innovative design candidates without depending on the

designer’s intuition and experience. Various optimization

methods have been developed through the years and one of the

promising options is the level-set-based topology optimization

method. The benefit of this alternative method is that the design

is characterized by its clear boundaries. This advantage can

avoid post-processing work in conventional topology

optimization process to a large extent and realize direct

integration between topology optimization and additive

manufacturing. In this paper, practical algorithms and a

MATLAB-based open source framework are developed to

seamlessly integrate the level-set-based topology optimization

procedure with additive manufacturing process by converting

the design to STL (STereoLithography) files, which is de facto

standard format for 3D printing. The proposed algorithm and

code are evaluated by a proof-of-concept demonstration with

3D printing of both single and multimaterial topology

optimization results. The algorithm and the open source

framework proposed in this paper will be beneficial to the areas

of computational design and additive manufacturing.

INTRODUCTION Topology optimization can be considered as seeking the

optimal distribution of one or more materials in a prescribed

design domain according to a performance function. Since the

pioneering work of Bendsøe and Kikuchi in 1988 [1], several

methods of topology optimization have been developed,

including homogenization [1-3], evolutionary method [4], Solid

Isotropic Material with Penalization (SIMP) method [5, 6], and

level-set methods [7-10].

Even though the eye catching advantage of the level-set

methods is that no post-processing work is needed, however

such method is not 100% accurate. Since dealing with

computer-driven analysis, that includes optimization algorithms

combined with Finite Element Analysis, computational time and

resources have to be accounted. That leads to common

techniques such as downgrading 3D problems to 2D and

applying symmetries where possible. Metamaterial examples

are also worth mentioning, since they are a combination of

periodically assembled unit cells. In these cases, some post-

processing work is practically needed: adding thickness,

mirroring, repeating the design periodically or even a

combination of these. Scaling and rotating the design are also

useful tools for achieving the desired product.

Additive Manufacturing (AM) such as Stereolithography

(SLA), Fused Deposition Modeling (FDM) and Selective Laser

Sintering (SLS) can fabricate parts directly from Computer-

Aided design (CAD) models without specific tooling and

fixtures. The main distinguishing feature of AM technologies is

their layer-wise fabrication approach. The ability of selectively

place (multi) materials in 3D spaces provides unique design

opportunities and capabilities to create any complex geometric

shape and compositional material distribution. Conventional

design practice relies heavily on designers’ intuition and

restricted by the manufacturing technologies. Topology

optimization is a powerful computational tool which recasts the

design problem as an optimal material distribution problem so

that the system will find an efficient geometry which fulfills the

requirements quantified by the objective and constraint

functions in the optimization scheme. However, topology

optimization results exist in complicated freeform geometries,

which pose great challenges for conventional manufacturing

technologies. The direct rationale implies that the topology

optimization would be one of the best technologies for design

Page 2: AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL …me.eng.stonybrook.edu/~chen/Downloads/TO_and_AM_preprint.pdf · 2016-09-22 · AN OPEN SOURCE FRAMEWORK

2

for additive manufacturing. Despite the importance of the

topology optimization in additive manufacturing, the open

source platform that bridges these two systems are not yet

available.

This paper provides a code that can be integrated with the

optimization algorithms, offering the designer a variety of post-

processing tools and generating the STL file, which can be

directly processed by 3D printing equipment. In the following

sections, an overview of the STL format is provided, the main

parts of the code are explained and several demonstration

examples are presented.

3D PRINTING FILE FORMAT STereoLithography (STL) is a standard file widely used by

commercial 3D printers and Computer-Aided Design software.

It contains information only about the boundary surface of the

3D object in a specific way which will be explained in this

section.

The result of a level-set based topology optimization

method is a continuous function Φ, implicitly defining the

boundaries by its zero level. These boundaries form an

isosurface which has to be included in the STL file. To achieve

that, the isosurface has to be discretized into triangular facets

which can be obtained by using a triangulation technique such

as Delaunay [11]. Each triangular facet is now described by

three vertices (corners), and the unit normal vector of the facet

can be calculated:

2 1 2 1 2 1

3 1 3 1 3 1

x 2 1 3 1 2 1 3 1

y 2 1 3 1 2 1 3 1

z 2 1 3 1 2 1 3 1

i j k

n det x x y y z z

x x y y z z

n (y y )(z z ) (z z )(y y )

n [(x x )(z z ) (z z )(x x )]

n (x x )(y y ) (y y )(x x )

(1)

x x

2 2 2

x y z y y

z z

n̂ n /

ˆn n n n n /

n̂ n /

(2)

Since the surface of the design can describe only the

boundaries of the object, the normal vectors play an important

role on defining on which side of the facet there will be material

and which not. The normal vector should always point outwards

to the void. Hence, on the other direction, the existence of

material is implied.

A normal vector along with three vertices contains all the

information for each facet with 12 numbers. These data can be

used in two different ways in order to generate an STL file:

ASCII and Binary mode [12]. The main advantage of the ASCII

mode generated STL files is that the data can be readable by the

designer and a typical example is shown as follows:

facet normal 0.257496774 -0.25384599 0.932339907

outer loop

vertex 100.810745 51.1502495 16.9487667

vertex 100.845886 50.9629555 16.8880672

vertex 99.8509369 52.9121056 17.6935463

endloop

endfacet

Figure 1 ALGORITHM TRANSFERRING ISOSURFACE TO

STL FILE.

But the size of these files is large, making their use

sometimes impractical. On the other hand, if the Binary mode is

selected, the size of the file is reduced, but the data can only be

read by software - a drawback that is not considered important.

Moreover, since the designs obtained by topology optimization

can be complicated, a high resolution may be needed.

Following the Binary mode, the size of the generated files is

kept relatively small.

As in every electronic file, generating an STL file should

follow a standard procedure. The target is to have a column of

25 numbers for each facet. At the beginning, the data should be

converted to single precision. Then, each of the 12 numbers has

been converted to 16-bit unsigned integer making every number

to a two-number equivalent. That will sum up the data of each

facet to 24 values, followed by an attribute byte count which is

generally equal to 0.

x x y y z z 1 1 1 1 1 1

T

2 2 2 2 2 2 3 3 3 3 3 3

ˆ ˆ ˆ ˆ ˆ ˆn n n n n n x x y y z z

x x y y z z x x y y z z 0 (3)

At the beginning of the STL file the user can insert an 80-

character title (80 bytes), which should be followed by the total

number of the facets (n). The file ends with the prepared data in

a 25 x n structure. Even though the dimensions in the STL file

are dimensionless, the metric system (mm) will be used here for

reference.

IMPLEMENTATION In this section, the main parts of the Matlab code

(Appendix) are explained. The code, apart from creating the

STL file, also provides some useful tools for preparing the

design before printing. There are two main categories of inputs:

the ones that are given while calling the function (direct inputs)

and those that can be changed inside the code (built-in

parameters). The numbers of the lines that each tools is

referring to, are given in brackets.

Page 3: AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL …me.eng.stonybrook.edu/~chen/Downloads/TO_and_AM_preprint.pdf · 2016-09-22 · AN OPEN SOURCE FRAMEWORK

3

Figure 2 2D GEOMETRY DEFINED BY A 3D LEVEL-SET

FUNCTION Φ.

Direct Inputs [1-48] The direct inputs are split into two categories:

Primary Input (phi) [1-19] phi is the main user input that must be given. It is the level-

set function Φ, which can be either a 3D or 4D function. As

already mentioned, Φ is a function one dimension higher than

the design that it describes. That is, a 4D function Φ represents

a 3D design by implicitly defining its boundaries on the zero

level. The sign of Φ can further imply the presence of material

or not. Since a 4D function cannot be intuitively illustrated, the

following basic example of a 3D Φ and the resulted 2D design

is presented:

x 0, x , material

x 0, x , boundary

x 0, x D \ , void

(4)

Α 3D Φ consists of a discretized 2D domain and a value on

each node, and hence, the input phi will be a 2D matrix (or 3D

matrix for a 3D design). For a 3D matrix, the initial grid will be

i x j x k. In a general case, if one dimension has been discretized

by n, the grid will consist of n+1 nodes. Thus, the original

resolution will be calculated from the largest dimension as

1/(max([i, j, k])-1). Moreover the code, by default, will scale the

largest dimension to 1mm - a dimension that can be changed

later.

Secondary Inputs (r, res, vis, t) [20-48] In contrast with phi, the following inputs are not vital. Even

though they are not defined, the default settings will be enabled.

In case of an input phi ready to be printed without any post-

processing, the designer still has to change the scaling factor r

in order to get the desired size. That will drive the largest

dimension of the initial phi to be r mm, and the rest of the

dimensions will be changed accordingly.

Parameter res can change the accuracy of the design [320-

327]. Larger res means higher accuracy and by result larger-in

size-STL file. For speed improvement of the code, the

resolution change happens before [115-122] or after [171-176]

the modifications of Φ, based on the value of res (less or greater

than 1).

Table 1 DECREASING THE QUALITY OF A

MICROGRIPPER DESIGN [13] Resolution

(res) 0.2 0.3 0.5

Design

STL size 21.3KB 71.3KB 327KB

Table 2 INCREASING THE QUALITY OF A

MICROGRIPPER DESIGN (Chen, Chen et al. 2010) Resolution

(res) 1 2 4

Design

STL size 1.44MB 5.84MB 23.6MB

Throughout the code, several figures are generated to

visualize the design before and after the changes. It can help the

designer decide the changes that need to be done, but it

increases significantly the computational time. When a high

resolution is preferred, it is recommended that the visualization

is turned off (vis=0). A resolution should be selected wisely: low

resolution may reduce the quality of the design (Table 1) and

high resolution will severely slow down the code and may lead

to huge STL files (Table 2), especially for designs with a lot of

complicated features. An ideal resolution has to be found for

every design separately, in order to balance the size of the STL

file, the speed of the code and the quality of the design.

Unlike 3D designs that may be ready-to-print, 2D designs

need, at least, the necessary step of giving thickness [189-192].

Since the code changes the scale of the desired twice:

automatically to 1mm and by designer input to desired r mm,

the thickness should be provided in terms of a reference

dimension. In this implementation, the initial x dimension has

been selected. If this dimension was 0.8mm and t=20%, the

result will be a thickness of 0.16mm. If r=100, then x=80mm

and the thickness will be resized to 16mm. On the other hand,

periodicity or other processes, that have been applied to x

direction, will not affect the thickness. In the latter example, if

per_x=2, then x=160mm but the thickness will remain 16mm.

The thickness is used to convert a 2D design to 3D, which

actually can be described as 2.5D since it only extrudes the

initial 2D design. This conversion is done by the

convert_2d_to_3d function [297-318] which is included at the

end of the code.

In summary, Table 3 presents the 5 different modes, in

which the input can be given, with the corresponding default

settings.

Page 4: AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL …me.eng.stonybrook.edu/~chen/Downloads/TO_and_AM_preprint.pdf · 2016-09-22 · AN OPEN SOURCE FRAMEWORK

4

Table 3 INPUTS AND DEFAULT SETTINGS.

Inputs Default settings

phi_to_stl(phi) r=1, res=1, vis=0, t=20

phi_to_stl(phi, r) res=1, vis=0, t=20

phi_to_stl(phi, r, res) vis=0, t=20

phi_to_stl(phi, r, res, vis) t=20

phi_to_stl(phi, r, res, vis, t) none

Built-In Parameters [50-77] Apart from the direct inputs, there are several parameters,

each of which is responsible for a specific action that is needed

for some designs. Since they are not as necessary as the basic

inputs, they can only be changed inside the code. By default,

these parameters are set to values, keeping these tools disabled.

The designer can always modify the code to alter these

parameters and can further include them as direct inputs. That

will be useful in case of optimizations that need symmetry,

periodicity or change on their placement orientation. By

including them as direct inputs, the communication between the

code and the optimization algorithm becomes automated. The

reader is also referred to Figs 6-15 of the examples for a

demonstration of these tools.

Flip Parameters [51-56] [124-133] Flipping the design can be useful and is usually selected in

order to correctly mirror the design (symmetry). It can also

change the desired orientation along with the orientation

parameters (which will be discussed in 3.2.4). fl_x, fl_y and fl_z

can be either 0, allowing the design unaffected, or 1 (flip in

specific direction). (Fig. 10a, b)

Figure 3 FLIPPING IN X DIRECTION FOR A DESIGN

WITH N+1 NODES.

Symmetry [58-61] [135-144] If the optimization result was obtained by having

considered symmetries, symx, symy and symz can mirror the

design in the corresponding directions. In 2D designs, by

default, the initial 2 dimensions are set to be x and y, hence,

symmetry in z direction has no physical meaning and is being

ignored. Value 1 will keep the initial design and 2 will apply the

symmetry in the specific direction. (Fig. 10b, c)

Figure 4 SYMMETRY IN X DIRECTION FOR A DESIGN

WITH N+1 NODES.

Periodicity [63-68] [146-169] Some designs, including metamaterials, consist of

periodically assembled unit cells. If a unit cell is generated by

the optimization procedure, the result should get repeated

accordingly, in order to build the desired structure. For

computational time economy, providing thickness to a 2D

design comes after applying the periodicity. So, as in the

symmetry, per_z has no effect on a 2D design. Instead,

increasing the thickness parameter t, from the direct inputs, is

an option. If all per_x, per_y and per_z are equal to 1, no

periodicity is applied in any direction. (Fig. 10a, b)

All the changes, up to this point, will be saved as a new

data file containing the updated Φ (phi.mat) [269-272]

Figure 5 PERIODICITY IN X DIRECTION FOR A DESIGN

WITH N+1 NODES.

Orientation [70-77] [199-205] This last tool can be used if the orientation of the printed

product has to be altered. Given the variety of 3D printers in the

market, the capabilities vary. More economic 3D printers or

high tech large scale production printer may require the product

to be placed in a specific orientation for better result or

manufacturing convenience. Parameters x_new, y_new and

z_new are, by default, set to 1, 2 and 3 accordingly. Setting

x_new=2 will move the initial y direction to x direction and so

on. As a result, the values 1, 2 and 3 have to be used once, in

order to get a correctly rotated 3D design. If more rotation is

required, these parameters can be combined with the flip

parameters. (Fig. 10c, d)

STL Generation [222-267]

Page 5: AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL …me.eng.stonybrook.edu/~chen/Downloads/TO_and_AM_preprint.pdf · 2016-09-22 · AN OPEN SOURCE FRAMEWORK

5

The preparation of the data for generating the STL file can

commence after Φ has been modified. phi data correspond to an

isosurface from which the facets and the vertices can be

extracted. The triangulation will create triangles, and for each

facet, 3 vertices and one unit normal are calculated. These data

are converted to single precision and rearranged at the

following convenient-for illustration-way:

x 1 2 3

y 1 2 3

z 1 2 3

n̂ x x x

n̂ y y y

n̂ z z z

(5)

After editing them, each number is now transformed into a

two-number equivalent (16-bit unsigned integer):

x x y y z z 1 1 1 1 1 1

T

2 2 2 2 2 2 3 3 3 3 3 3

ˆ ˆ ˆ ˆ ˆ ˆn n n n n n x x y y z z

x x y y z z x x y y z z (6)

At the end of this sequence of numbers, an attribute byte

count has to be specified which is equal to 0:

x x y y z z 1 1 1 1 1 1

T

2 2 2 2 2 2 3 3 3 3 3 3

ˆ ˆ ˆ ˆ ˆ ˆn n n n n n x x y y z z

x x y y z z x x y y z z 0 (7)

Writing the STL file has to follow a specific way. At the

beginning, the user can insert an 80-character title, which

should be followed by the total number n of the facets. Then the

prepared data can be written in a 25 x n structure.

Multi-Material Designs

The code offers a framework of exporting an STL file from

a specified Φ. In topology optimization, multi-material designs

are of great interest. They contain more than one level-set

functions, depending on the method followed: Color level-set

method, Piecewise constant level-set method or Reconciled

level-set method. If the Reconciled method [14-16] is followed,

the optimization will include one level-set function for each

material. In case of a design with m materials, the current code

should be utilized m times for Φ1 to Φm. If other methods are

followed, the code can still fit in the needs of each framework.

Each STL file will represent a single material and be a part of

the full multi-material assembly. Having more than one level-set

functions Φ implies that each STL file should be saved under a

separate name. [258]

Main Errors

While the designer is trying to tune up the parameters for

getting the desired output, the visualization should be on and

the resolution relatively low, which will accelerate the code.

Designs with high resolution may lead to calculations difficult

to be handled by the computer. If high resolution is required, a

wise selection of res is recommended in accordance to the

computer’s capabilities. Unlike res, scale factor r does not

affect the computational time.

Figure 6 (a) INITIAL Φ AND DESIGN, AND (b) FINAL Φ

AND DESIGN.

The main errors that can occur are the following:

Phi structure input is not the expected or a parameter

has an illegal value.

The computer is unable to handle complicated post-

processing work on phi and simultaneously high

resolution res. This can make the STL generation time-

consuming and may use the entire computer’s power.

EXAMPLES [80-89] The code completes the level-set based topology

optimization and creates a design framework integrated with

additive manufacturing. In this section, the code is

demonstrated in action with several optimization examples. The

first one is a benchmark optimization known as Michell beam.

The design is obtained through an in-house level-set based

topology optimization algorithm. The second one is a

microgipper; a design which is the result of a robust topology

optimization process [13]. In these examples, the functionality

of the code is presented in terms of post-processing operations

and generation of STL. Both examples need data that can be

downloaded online in order to function as input. For convenient

testing, the code contains a third example with a simple 3D Φ

representing a random design as well. At the end of this section,

a metamaterial with negative Poisson’s ratio is also presented to

demonstrate the code’s capability of handling more than one

material.

Michell Beam [85-86] For the Michell beam, the design domain consists of an 11

x 8 plate with a circle filled with material. It is loaded at the

center of the right edge with F=30N in the vertical

direction[17]. The mean compliance problem is solved using

level-set based topology optimization method, and the initial

and final design can be seen in Fig. 6.

Page 6: AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL …me.eng.stonybrook.edu/~chen/Downloads/TO_and_AM_preprint.pdf · 2016-09-22 · AN OPEN SOURCE FRAMEWORK

6

Since an integrated framework is used, at the end of the

optimization the resulted phi is send directly to phi_to_stl(phi)

and the STL file is generated. By using the basic input only

(phi), the design will follow the default settings, transforming

the 11mm dimension to 1mm and using 0.22mm as thickness

(20%) (Fig. 7). No extra tools will be enabled.

Figure 7 2D DESIGN WITH DEFAULT SETTINGS.

A more complete use of the code is to include more direct

inputs phi_to_stl(phi, 110, 2, 0, 10). That will produce a final

design with the x dimension scaled to 110mm, the resolution

doubled, the visualization disabled and a thickness of 11mm

(10%) (Fig. 8).

Figure 8 2D DESIGN WITH USER-DEFINED SETTINGS.

The STL generated by the code is, then, ready to be

printed. Assuming that the part has to be printed just for

realizing the design-result, an economic and fast way can be

selected. In this example, the object was built with PLA

material using a Flashforge Dreamer 3D printer, with low

accuracy settings for fast printing. The design procedure, from

the initial Φ to the printed model, is shown in Fig. 9.

Figure 9 (a) 2D DESIGN, (b) 3D DESIGN, (c) MESHED

SURFACE, AND (d) 3D PRINTED.

Figure 10 (a)-(d) POST-PROCESSING IN STEPS, (e)

MESHED SURFACE, AND (f) MESH DETAIL.

3D Microgripper [88-89] In this example, some parameters from inside the code will

be changed, as well, for a better demonstration of the capability

of the code to adapt to the problem, designer’s intent and

manufacturing needs. The example contains a half

microgripper, which is the result of an optimization[13]. For

computational reasons, symmetry conditions were considered

during the problem setting.

Since phi contains information only for half of the desired

design, the data have to be modified before printing. By calling

phi_to_stl(phi), where phi is the only input, the result would be

a 1mm long half microgripper (Fig. 10a). In order to get a stl

file with the desired result, direct inputs and built-in parameters

have to be set. In this case, the code is called as phi_to_stl(phi,

50, 1, 1) in order to request a half design that is 50mm long. At

the same time, symmetry has to be enabled in z direction, after

the initial design has been flipped. To do so, built-in parameters

fl_z=1 (Fig. 10b) and symz=2 (Fig. 10c) have to be set.

For manufacturing purposes, the design might be expected

to have a different orientation, such as having the original yz

plane attached to the ground. Again, built-in parameters have to

be set to: x_new=1, y_new=3 and z_new=2 (Fig. 10d). The

modifications of the design along with the creation of the STL

file are shown in Fig. 10.

The design was further printed using a high quality 3D

printer (Objet260 Connex, Stratasys) with a soft material

(TangoBlackPlus) Fig. 11.

Figure 11 (a) ISOSURFACE, (b) MESHED SURFACE, AND

(c) 3D PRINTED.

Page 7: AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL …me.eng.stonybrook.edu/~chen/Downloads/TO_and_AM_preprint.pdf · 2016-09-22 · AN OPEN SOURCE FRAMEWORK

7

Other Operations Included

Boundary [329-340]

The generation of the STL can be done only on 3D designs

that do not have any openings on the external surface. It is not

uncommon that some optimization results have open faces at

the locations where the original design meets the boundaries.

That is why a boundary function is used at the end of all the

modifications [194-196]. That ensures that no open faces exist

and the creation of the STL file will not fail (Fig. 12).

Limit [206-210]

By setting a small offset (±0.0001) on Φ instead of the zero

level, it ensures that the boundaries will be included in the final

design. In the current implementation, positive values of Φ

correspond to material and negative values to void. If the

optimization follows the opposite way (positive values of Φ

corresponds to void), the signs have to be reversed for keeping

the normal vectors in the correct directions, pointing outwards

(phi_stl=±phi) (Fig. 12).

Figure 12 BEFORE AND AFTER APPLYING BOUNDARIES.

Smoothing Φ [212-213]

Smoothing Φ is a technique of getting good quality surface

with a relatively small resolution. That may lead to differences

between the original design and the printed object, and

therefore, it has to be used with caution (Fig. 13).

Figure 13 BEFORE AND AFTER SMOOTHING Φ.

Simple 2D example [81-83] Since the previous examples require downloading data, one

more example is presented using a simple Φ created inside

Matlab. Assuming that the current design is a unit cell, it has to

be repeated periodically. Here, it will be repeated 3 times in the

x direction and twice in y direction resulting to a 2 x 3 structure

(Fig. 14). As in the previous example, apart from having phi as

an input, built-in parameters per_x and per_z have to be set to 3

and 2, accordingly. Since, having the design repeated is not

common, these parameters are not set as direct inputs. It is up to

designer’s option to modify the code, in order to make them

direct inputs, if that is necessary.

Figure 14 (a) INITIAL DESIGN, (b) MODIFIED DESIGN, (c)

3D DESIGN, AND (d) MESH GENERATION.

Multi-Material Negative Poisson’s Ratio In contrast to the single material designs, a multi-material

design, consisting of m materials, needs m different stl files to

be printed. Each stl file will be assigned with a material during

the printing procedure. In this last subsection, a multi-material

design is used to demonstrate the use of the code with more

than one material. The current example is a negative Poisson’s

ratio metastructure. A 2D reconciled level-set based topology

optimization method is followed with two functions Φ1 and Φ2

associated with one hard and one soft material. The result of the

optimization is a 2D unit cell which has to be periodically

assembled to get the desired structure. In this example, the code

has to be called twice: once for each Φ. The structure is

periodic (3x3) and the dimension of each unit cell is chosen to

be 10mmx10mm and the thickness 100% (10mm).

The design was printed using a high quality 3D printer

(Objet260 Connex, Stratasys) with a hard (VeroWhite) and a

soft material (TangoPlus) Fig. 15.

Figure 15 (a) UNIT CELL DESIGN, (b) 3X3 STRUCTURE

DESIGN, (c) SEPARATE DESIGN FOR EACH MATERIAL,

(d) MESHED SURFACE, AND (d) 3D PRINTED.

Page 8: AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL …me.eng.stonybrook.edu/~chen/Downloads/TO_and_AM_preprint.pdf · 2016-09-22 · AN OPEN SOURCE FRAMEWORK

8

CONCLUSION This paper presented a method of connecting the topology

optimization with the additive manufacturing. By using the

existing code, an STL file is getting created based on the Binary

mode. That makes the STL file smaller-in size-than following

the alternative way of ASCII mode. The gap between these two

fields was bridged by a Matlab code, which apart from

generating an STL file, provides also some useful post-

processing tools: scaling, changing resolution, adding thickness

to 2D designs, flipping, symmetry, periodicity and changing the

orientation. It can work with the existing optimization

algorithms based on level-set method in a straight-forward way.

Inputting direct inputs for the basic use of the code and

changing built-in parameters were explained in detail. The only

necessary input for generating the STL was the level-set

function Φ that corresponds to a 2D or 3D design, and the rest

of the inputs are used to create the desired design. The built-in

parameters are generally less frequently utilized, but the

designer still has the freedom to change them or even include

them as direct inputs. The speed aspect of the code has been

analyzed in terms of the design’s resolution and visualization of

the results. The paper concluded with the demonstration of

three examples-designs that have been obtained using a level-

set based topology optimization method.

ACKNOWLEDGMENTS

The authors acknowledges the support from the National

Science Foundation (CMMI1462270), Ford Motor Company,

the Region 2 University Transportation Research Center

(UTRC) and the start-up funds from the State University of

New York at Stony Brook.

REFERENCES 1. Bendsoe, M.P. and N. Kikuchi, Generating optimal

topologies in structural design using a

homogenization method. Comput. Methods Appl.

Mech. Eng., 1988. 71(2): p. 197-224.

2. Suzuki, K. and N. Kikuchi, A homogenization method

for shape and topology optimization. Computer

methods in applied mechanics and engineering, 1991.

93(3): p. 291-318.

3. Allaire, G., Shape optimization by the homogenization

method. Vol. 146. 2002: Springer Science & Business

Media.

4. Xie, Y. and G.P. Steven, A simple evolutionary

procedure for structural optimization. Computers &

structures, 1993. 49(5): p. 885-896.

5. Bendsøe, M.P., Optimal shape design as a material

distribution problem. Structural optimization, 1989.

1(4): p. 193-202.

6. Zhou, M. and G. Rozvany, The COC algorithm, Part

II: topological, geometrical and generalized shape

optimization. Computer Methods in Applied

Mechanics and Engineering, 1991. 89(1): p. 309-336.

7. Osher, S. and J.A. Sethian, Fronts propagating with

curvature-dependent speed: Algorithms based on

Hamilton-Jacobi formulations. Journal of

Computational Physics, 1988. 79(1): p. 12-49.

8. Sethian, J.A. and A. Wiegmann, Structural Boundary

Design via Level Set and Immersed Interface Methods.

Journal of Computational Physics, 2000. 163(2): p.

489-528.

9. Wang, M.Y., X. Wang, and D. Guo, A level set method

for structural topology optimization. Computer

Methods in Applied Mechanics and Engineering, 2003.

192(1–2): p. 227-246.

10. Allaire, G., F. Jouve, and A.-M. Toader, Structural

optimization using sensitivity analysis and a level-set

method. Journal of Computational Physics, 2004.

194(1): p. 363-393.

11. Shewchuk, J.R., Triangle: Engineering a 2D quality

mesh generator and Delaunay triangulator, in Applied

computational geometry towards geometric

engineering. 1996, Springer. p. 203-222.

12. Burns, M., The StL format: standard data format for

fabbers. 1999.

13. Chen, S., W. Chen, and S. Lee, Level set based robust

shape and topology optimization under random field

uncertainties. Structural and Multidisciplinary

Optimization, 2010. 41(4): p. 507-524.

14. Merriman, B., J.K. Bence, and S.J. Osher, Motion of

multiple junctions: A level set approach. Journal of

Computational Physics, 1994. 112(2): p. 334-363.

15. Zhang, X., J.-S. Chen, and S. Osher, A multiple level

set method for modeling grain boundary evolution of

polycrystalline materials. Interaction and Multiscale

Mechanics, 2008. 1(2): p. 178-191.

16. Chen, S., et al., A level set approach for optimal

design of smart energy harvesters. Computer Methods

in Applied Mechanics and Engineering, 2010. 199(37):

p. 2532-2543.

17. Wang, X., M. Wang, and D. Guo, Structural shape and

topology optimization in a level-set-based framework

of region representation. Structural and

Multidisciplinary Optimization, 2004. 27(1-2): p. 1-19.

Page 9: AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL …me.eng.stonybrook.edu/~chen/Downloads/TO_and_AM_preprint.pdf · 2016-09-22 · AN OPEN SOURCE FRAMEWORK

9

APPENDIX

MATLAB CODE

1 function phi_to_stl(phi, varargin)

2 % Developed by Panagiotis Vogiatzis

([email protected])

3 % Advisor: Prof. Shikui Chen

([email protected])

4 % Computational Modeling Analysis and Design

Optimization Lab (CMADO)

5 % Department of Mechanical Engineering

6 % State University of New York at Stony Brook

7 % First version: November 06, 2015

8 %%% Disclaimer: This code is provided for educational

purposes and is not

9 %%% guaranteed to be free of errors. The authors are

not liable for any

10 %%% problems caused by the use of this code.

11

12 % phi_to_stl(phi) takes the level-set function Phi and

converts it into an

13 % stl file using binary mode. The code contains also

post-processing tools.

14 % After the post-processing, Phi is saved as 'phi.mat'

15 % -phi: a matrix with the values of the 3D or 4D

level set function phi

16 % -2D (3D) Design

17 % A 2D (3D) design is described by a 3D (4D)

level-set function phi.

18 % phi is a i x j (i x j x k) matrix, where i and

j (i, j and k)

19 % define the grid of the 2D (3D) design.

20 % -i x j x k is the initial grid.

21 % -If the design has been discretized in n elements

in one direction, the

22 % corresponding grid will have n+1 nodes.

23 % -The original resolution is set to be 1/(max([i, j,

k])-1). (res = 1)

24 % -The code transforms, initially, the larger side of

the design

25 % automatically to 1mm. (r = 1)

26 % -Visualization is off (vis = 0)

27 % -The default thickness for 2D designs is set to be

20%.

28

29 % phi_to_stl(phi, r) includes the scale factor r as an

input

30 % r will scale the dimension of the larger side to r

mm. The dimension of

31 % the other 1 or 2 direction/s will be resized

accordingly.

32 % i.e. r = 10 will make the largest dimension equal

to 10mm.

33

34 % phi_to_stl(phi, r, res) includes resolution res. With

default r = 1, if

35 % the initial grid is 100x100x100, the resolution is

set to be 1/100mm.

36 % If res = 0.5, the resolution will be half of the

initial (1/50mm).

37 % This parameter increases/decreases the accuracy of

the design. High

38 % resolution should be carefully selected since it

will significantly

39 % increase the computational cost. res = 1 keeps the

initial resolution.

40

41 % phi_to_stl(phi, r, res, vis) manages also the

visualization. vis = 0/1

42 % disables/enables the visualization. Disabling the

visualization will

43 % accelerate the code.

44 %

45 % phi_to_stl(phi, r, res, vis, t) Sets thickness for 2D

designs.

46 % Percentage of the thickness compared to the initial

x dimension.

47 % i.e. t = 50 makes the thickness 50% of the initial

x dimension.

48 [r, res, vis, t] = inputs(varargin{:}); % Investigate

which mode is used.

49

50 % % % % %PARAMETERS TO BE CHANGED% % % % %

51 % Flip parameters

52 % The design can be flipped in one or more

directions. These parameters

53 % can be used in order to correctly mirror the design

later.

54 fl_x = 0; % fl_x = 0 will not flip the design in the x

direction.

55 fl_y = 0;

56 fl_z = 0; % fl_z = 1 will flip the design in the z

direction.

57

58 % Symmetry

59 symx = 1; % symx = 1 will not apply symmetry in x

direction.

60 symy = 1; % symy = 2 will apply symmetry in y

direction.

61 symz = 1; % In 2D designs, symz will not have any

effect on the design.

62

63 % Periodicity

64 % Defining the number that the design is repeated in

each direction.

65 % If a 4x5x1 structure is needed: per_x = 4, per_y =

5 and per_z = 1.

66 per_x = 1;

67 per_y = 1;

68 per_z = 1; % In 2D designs, instead of using per_z,

increase t parameter.

69

70 % Orientation

71 % Orientation and flip parameters will define the

final design placement.

72 % 1, 2 and 3 refers to initial x, y and z directions.

73 % Each of 1, 2 and 3 have to be used once.

74 % The orientation of the design can be changed with

the following:

75 x_new = 1; % x_new=2 will move the initial y direction

to x direction.

76 y_new = 2; % Default: x_new = 1, y_new = 2 and z_new =

3.

77 z_new = 3;

78

79

80 % % % % %EXAMPLES% % % % %

81 % % Michell beam (online download is required)

82 % load('michell_beam.mat'); r = 110; vis = 1; t = 10;

83

84 % % 3D Microgripper (online download is required)

85 % load('microgripper.mat'); r=50; vis = 1; fl_z = 1;

symz = 2;

86

87 % % 2D Example

88 % [g{1}, g{2}] = ndgrid(0:0.02:1, 0:0.02:1); vis = 1;

89 % phi = sqrt((g{1}-0.5).^2 + (g{2}-0.5).^2) - 0.25;

per_x = 3; per_y = 2;

90

91 % Dimensions of initial phi

92 [i, j, k] = size(phi);

93 mx = max([i, j, k]);

94 d(1) = (i-1)/(mx-1);

95 di=d(1)*r;

96 d(2) = (j-1)/(mx-1);

97 d(3) = (k-1)/(mx-1);

98 dx = max([d(1), d(2), d(3)])/(mx-1);

99 [g{1}, g{2}, g{3}] = ndgrid(0:dx:d(1), 0:dx:d(2),

0:dx:d(3));

100 % since the largest dimension is scaled to 1, set

max(phi) equal to 0.2.

101 phi = 0.2*phi/max(phi(:));

Page 10: AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL …me.eng.stonybrook.edu/~chen/Downloads/TO_and_AM_preprint.pdf · 2016-09-22 · AN OPEN SOURCE FRAMEWORK

10

102 if vis==1

103 figure;

104 if ndims(phi)==2

105 surf(g{1}, g{2}, phi); title('Initial Phi');

axis equal;

106 figure;

107 contourf(g{1}, g{2}, phi, [0 0]); axis equal;

108 else

109 show(g{1}, g{2}, g{3}, phi, d(1), d(2), d(3))

110 end

111 title('Initial Design')

112 saveas(gcf,'Initial Design.jpeg');

113 end

114

115 % Reshaping Phi if res<1

116 if res<1

117 dx = dx/res;

118 [g, phi] = resolution(dx, g, phi, d(1), d(2),

d(3));

119 [i, j, k] = size(phi);

120 mx = max([i, j, k]);

121 phi = r*phi;

122 end

123

124 % Flip the design in x, y and z direction

125 if fl_x==1

126 phi = flip(phi,1);

127 end

128 if fl_y==1

129 phi = flip(phi,2);

130 end

131 if fl_z==1

132 phi = flip(phi,3);

133 end

134

135 % Symmetry in x, y and z direction

136 phiy = flip(phi(:,1:j-1,:),2);

137 phixy = [phi, (symy-1)*phiy;

138 (symx-1)*flip(phi(1:i-1,:,:),1),(symx-1)*(symy-

1)*flip(phiy(1:i-1,:,:),1)];

139 phixyz = flip(phixy,3);

140 phisym(:,:,:) = phixy;

141 if ndims(phi)==3

142 phisym(:,:,k:2*k-1) = (symz-1)*phixyz;

143 end

144 phi=phisym(1:(mx-1)*d(1)*symx+1,1:(mx-

1)*d(2)*symy+1,1:(mx-1)*d(3)*symz+1);

145

146 % Periodicity in x, y and z direction

147 d(1) = per_x*r*d(1)*symx;

148 d(2) = per_y*r*d(2)*symy;

149 d(3) = per_z*r*d(3)*symz;

150 [m, n, o] = size(phi);

151 dx = max([d(1), d(2), d(3)])/(max([(symx*per_x*(i-1)),

...

152 (symy*per_y*(j-1)), (symz*per_z*(k-1))]));

153 [g{1}, g{2}, g{3}] = ndgrid(0:dx:d(1), 0:dx:d(2),

0:dx:d(3));

154 phi_new = zeros(per_x*(m-1)+1, per_y*(n-1)+1, per_z*(o-

1)+1);

155 for i = 0:per_x-1

156 for j = 0:per_y-1

157 for k = 0:per_z-1

158 for ii = 1:m

159 for jj = 1:n

160 for kk = 1:o

161 phi_new((i*(m-1)+ii), ((j*(n-

1)+jj)), ...

162 ((k*(o-1)+kk)))= phi(ii,

jj, kk);

163 end

164 end

165 end

166 end

167 end

168 end

169 phi = phi_new;

170

171 % Reshaping Phi if res>=1

172 if res>=1

173 dx = dx/res;

174 [g, phi] = resolution(dx, g, phi, d(1), d(2),

d(3));

175 phi = r*phi;

176 end

177

178 if vis==1

179 if ndims(phi)==2

180 figure;

181 surf(g{1}, g{2}, phi); title('Modified Phi');

axis equal;

182 figure;

183 title('Modified Design')

184 contourf(g{1}, g{2}, phi, [0 0]); axis equal;

185 saveas(gcf,'Modified Design.jpeg');

186 end

187 end

188

189 % Convert 2D to 3D (2.5d)

190 if ndims(phi)==2

191 [phi, d(3), g] = convert_2d_to_3d(phi, d(1), d(2),

t, di);

192 end

193

194 % Adding boundaries

195 phi_boundary = boundary(g, d(1), d(2), d(3));

196 phi = min(phi, phi_boundary);

197

198 disp('Preparing stl file...');

199 % Rotate the design for stl output only

200 gf{1} = g{x_new};

201 gf{2} = g{y_new};

202 gf{3} = g{z_new};

203 df(1)=d(x_new);

204 df(2)=d(y_new);

205 df(3)=d(z_new);

206 % limit. In the examples, negative phi defines void, so

-phi and -0.00001.

207 phi_stl = -phi; % if negative/positive defines void (-

/+phi).

208 limit = -0.00001; % the sign of the limit should match

the sign of phi.

209 phi_stl(phi_stl<=limit) = -1;

210 phi_stl(phi_stl>limit) = 1;

211

212 % Smoothing the design

213 phi_stl = smooth3(phi_stl);

214

215 if vis==1

216 figure;

217 title('Design')

218 show(gf{1}, gf{2}, gf{3}, phi_stl, df(1), df(2),

df(3))

219 saveas(gcf,'Design.jpeg');

220 end

221

222 % Getting the faces and vertices

223 [faces, vertices] = isosurface(gf{1}, gf{2}, gf{3},

phi_stl, 0);

224 triangles = triangulation(faces, vertices);

225

226 if vis==1

227 figure

228 title('Mesh')

229 h=trimesh(triangles);

230 set(h,'EdgeColor', [0 0 0.5]);

231 axis equal;

232 grid on

233 axis([0 df(1) 0 df(2) 0 df(3)])

234 xlabel('x'); ylabel('y'); zlabel('z');

235 saveas(gcf,'Mesh.jpeg');

236 end

237

238 normals = faceNormal(triangles);

239 vertices=vertices'; faces=faces'; normals=normals';

240 % Transform vertices into single precision.

241 datav = single(vertices);

242 % Put all vertices of the faces in a single 2D matrix

(3 x m).

243 datavxyz = datav(:,faces);

244 % Transform data to [V1, V2, V3] for each face in a (3

x 3 x m/3) matrix.

245 dataxyz = reshape(datavxyz,3,3,numel(datavxyz)/9);

246 % Reshape normals from (3 x m) to (3 x 1 x m) matrix.

247 normals = reshape(normals,3,1,numel(normals)/3);

248 % Include normal vectors: [n, V1, V2, V3] in a (3 x 4 x

m/3) matrix.

249 datanxyz = [normals(:,:,:), dataxyz(:,:,:)];

250 % Transform datanxyz to 16-bit unsigned integer.

251 datanxyz = typecast(datanxyz(:), 'uint16');

252 % Reshape data. Each column will contain the

information of one face.

253 data = reshape(datanxyz(:), 24, numel(datanxyz)/24);

254 % The 25th is the attribute byte count.

Page 11: AN OPEN SOURCE FRAMEWORK FOR INTEGRATED ADDITIVE MANUFACTURING AND LEVEL …me.eng.stonybrook.edu/~chen/Downloads/TO_and_AM_preprint.pdf · 2016-09-22 · AN OPEN SOURCE FRAMEWORK

11

255 data(25,:) = 0;

256

257 % Generating stl file in Binary mode

258 fileID = fopen('.\stl.stl', 'w'); % name of stl file

can be changed.

259 % Write a title up to 80 characters. (80 bytes)

260 fprintf(fileID, '%-80s',...

261 'exported using phi_to_stl created by P. Vogiatzis

(Advisor: S. Chen)');

262 % Write the number of faces in 32-bit unsigned integer.

263 fwrite(fileID, numel(data)/25, 'uint32');

264 % Write data.

265 fwrite(fileID, data, 'uint16');

266 fclose(fileID);

267 disp('...stl file ready');

268

269 % Saving new phi data

270 assignin('base', 'phi', phi);

271 clearvars -except phi

272 save('phi.mat')

273

274 function [r, res, vis, t] = inputs(varargin)

275 r = 1; % Default size 1mm, unless otherwise specified

276 res = 1; % Default resolution is the initial resolution

277 vis = 0; % By default, visualization is off

278 t = 20; % Default thickness for 2D designs is set to

20%

279 if nargin==1 % phi_to_stl(phi, r)

280 r = varargin{1};

281 elseif nargin==2 % phi_to_stl(phi, r, res)

282 r = varargin{1};

283 res = varargin{2};

284 elseif nargin==3 % phi_to_stl(phi, r, res, vis)

285 r = varargin{1};

286 res = varargin{2};

287 vis = varargin{3};

288 elseif nargin==4 % phi_to_stl(phi, r, res, vis)

289 r = varargin{1};

290 res = varargin{2};

291 vis = varargin{3};

292 t = varargin{4};

293 elseif nargin>0

294 disp('too many inputs!')

295 end

296

297 function [phi, ti, g] = convert_2d_to_3d(phi, dimx,

dimy, ti, di)

298 [m, n] = size(phi);

299 ti = di*ti/100;

300 dx = max([dimx, dimy])/((max([m, n])-1));

301 [gb{1}, gb{2}] = ndgrid(0:dx:dimx, 0:dx:dimy);

302 [g{1}, g{2}, g{3}] = ndgrid(0:dx:dimx, 0:dx:dimy,

0:dx:ti);

303

304 phi = interpn(gb{1}, gb{2}, phi, g{1}(:), g{2}(:));

305 phi = reshape(phi, size(g{1}));

306 phi1 = g{1};

307 phi2 = max(g{1}(:))-g{1};

308 phi3 = g{2};

309 phi4 = max(g{2}(:))-g{2};

310 phi5 = g{3};

311 phi6 = max(g{3}(:))-g{3};

312

313 phi12 = min(phi1, phi2);

314 phi123 = min(phi12, phi3);

315 phi1234 = min(phi123, phi4);

316 phi12345 = min(phi1234, phi5);

317 phi_boundary = min(phi12345, phi6);

318 phi = min(phi, phi_boundary);

319

320 function [g, phi] = resolution(dx_new, g2, phi, dimx,

dimy, dimz)

321 [g{1}, g{2}, g{3}] = ndgrid(0:dx_new:dimx,

0:dx_new:dimy, 0:dx_new:dimz);

322 if ndims(phi)==2

323 phic = interpn(g2{1}, g2{2}, phi, g{1}(:),

g{2}(:));

324 else

325 phic = interpn(g2{1}, g2{2}, g2{3}, phi, g{1}(:),

g{2}(:), g{3}(:));

326 end

327 phi = reshape(phic, size(g{1}));

328

329 function [phi] = boundary(g, dimx, dimy, dimz)

330 phi1 = g{1};

331 phi2 = dimx-g{1};

332 phi3 = min(phi1, phi2);

333 phi4 = g{2};

334 phi5 = dimy-g{2};

335 phi6 = min(phi4, phi5);

336 phi7 = g{3};

337 phi8 = dimz-g{3};

338 phi9 = min(phi7, phi8);

339 phi10 = min(phi3, phi6);

340 phi = min(phi10, phi9);

341

342 function show(x, y, z, Phi, dimx, dimy, dimz)

343 h = patch(isosurface(x, y, z, Phi, 0));

344 set(h, 'FaceColor', [0 0.7 0], 'EdgeColor', 'none');

345 camlight left;

346 grid on

347 axis equal

348 axis([0 dimx 0 dimy 0 dimz])

349 xlabel('x'); ylabel('y'); zlabel('z');

350 alpha (h, 0.7);