Anda di halaman 1dari 3

How to write a Java macro to change boundary condition types based on global

parameters
by Chao Zhang
on 10/18/2017
131 views
Categories: Boundary Conditions, Java   •  Products: HEEDS, STAR-CCM+   •  Version Applicable: 12.04   •  Article Number: 7978

Attachments: changeBCType.java (2 KB) ChangeBC_testCase.sim (12 MB)

In some design exploration studies, we may need to explore different boundary condition types (for example,. wall vs. inlet). This
can be accomplished by writing a Java macro. The fundamental approach is outlined in the following points:

Create a global parameter for each boundary


The global parameters representing the boundaries should be defined as design variables in Optimate or Design Manager to
be used to explore the design space
In Optimate or Design Manager, the global parameters should be defined using lists of discrete values;
The purpose of having discrete values is that each value represents a certain boundary condition type; linking the
discrete value to a certain boundary condition type is done through a Java macro (see next step)
Write a Java macro to accomplish these two things: (1) associating certain values of the global parameter to certain
boundary types (e.g.: 0 means wall; 1 means inlet), and (2) setting up the boundary condition according to the corresponding
global parameter value.
Insert the Java macro in the optimization study . 

An example sim file and Java macro are attached to this article. To see how the example macro works, change to parameter values
under Tools > Parameters. 0 represents wall boundary, and 1 represents an in-flow boundary condition. Play the Java macro, and
the 10 jet faces will be changed according to the parameter values. . 


The following explains the workflow in the Java macro:

1. Create a collection that gathers all global parameters from the sim file
2. Use a global parameter object to loop through the collection
a. In each iteration within the loop, obtain the presentation name of the global parameter and the value of the parameter

b.
Page 1 of 3
2.

b. Use “if… else� statement to check the global parameter value, and set the boundary condition accordingly.
c. Optionally check that if the global parameter value does not correspond to any desired boundary condition type then
the simulation exits, to prevent error


Next, we explain some of the important commands used in the Java macro:
1.. To create a collection of global parameters you need to use the “GlobalParameterBaseâ€� class. To obtain all global
parameter objects from the sim file, you need to use the “getObjects()� method, which is available under the
GlobalParameterManager class. Therefore, first create an object of global parameter manager, and then create a global parameter
collection to get the parameter objects from the sim file:

GlobalParameterManager globalPMN = simulation_0.get(GlobalParameterManager.class);


Collection<GlobalParameterBase> all_globalP = globalPMN.getObjects();. .

Note “simulation_0� was defined as the current open simulation, an object in the “Simulation� class.

2.. Once you define the global parameter collection, you can loop through it and set the boundary conditions accordingly. . The basic
looping structure of the code is as follows:

for(GlobalParameterBase i_BC : all_globalP){


[Get the global parameter name. Then get the value of the global parameter]
if (p_value == 0.0){
[Set boundary condition corresponding to the value 0.0]
. . . . }
else if (p_value ==1.0){
. . . . . . . [Set boundary condition corresponding to the value 1.0]
}
else {
. . . . . . . . . . . . . [error, exit simulation]
}

3.. Here is some detail on how to manipulate global parameters in Java. To get the name of a global parameter in a string use
“getPresentationName� which is a method available to the GlobalParameterBase class:

BC_id = i_BC.getPresentationName();

where BC_id is a string.


Once the parameter name is obtained, you can get a scalar global parameter (defined using class ScalarGlobalParameter) using its
name:

globalScalarP_i = ((ScalarGlobalParameter) globalPMN.getObject(BC_id));

Page 2 of 3
Then you can obtain the value of the parameter using the following method:

p_value = globalScalarP_i.getQuantity().getInternalValue();

where p_value should be a double type



4.. Here is some detail on manipulating boundary conditions in Java. To get boundaries you can use the following methods under
the Simulation class:

boundary_i = simulation_0.getRegionManager().getRegion("Region").getBoundaryManager().getBoundary(BC_id);

where boundary_i is an object defined by the Boundary class


To set the boundary condition, you first get a boundary condition type object, then use the “setBoundary� method to apply it
to a boundary object. For example, to set a wall boundary condition:

WallBoundary wallBoundary_0 = ((WallBoundary) simulation_0.get(ConditionTypeManager.class).get(WallBoundar


boundary_i.setBoundaryType(wallBoundary_0);

The example sim file attached is a demonstration and not an endorsement of a best practice. It is provided to showcase the use of
simulation features in a given application. Open the file in a software version equal to or newer than the one specified in the Version
Applicable field.

See also:
Getting Started: Editing a Java Macro

Page 3 of 3

Anda mungkin juga menyukai