Understanding the Main Method in the Macro

The main method in the macro is the execute() method, and is executed when the macro is run within Simcenter STAR-CCM+.

Here you instantiate objects for each of the nested classes and use the methods in each, to carry out the required tasks.

Description Code
  • A try-catch operation surrounds the entire block of statements in the execute() method. The try-catch is useful in case any other errors are thrown when you run the macro.
public void execute() {
 
  try {
 
 
  } catch (Exception e) {
    jOptionPane.showMessageDialog(
      null, e.toString()
    );
  }
}

Within the try-catch operation, you can divide the statements into two main sections. The first section contains statements that are carried out once, for example retrieving the active simulation. The second section contains a loop in which you place statements that are repeated for each set of wind data.

Description Code
  • In the first section you retrieve the active simulation and instantiate the DataReader, DataWriter, SimRunner, and PostProcessor. In addition, use the DataReader object to call the readInput() method that reads the input file and create a collection of SimData objects. The getFlowDetails() method is called to obtain a reference to the SimData collection and is supplied to the loop.
Simulation theSim =
  getActiveSimulation();
 
DataReader reader =
  new DataReader();
reader.readInput(folder + “/trainInput.txt”);
 
List<SimData> listCases =
  reader.getFlowDetails();
 
 
PostProcessor postP =
  new PostProcessor(theSim);
  • Next, use a loop to run the simulation for each set of wind data. The syntax used is that of a for-each loop, and reads: for each SimData object in the collection, do the following. This functionality came about in Java 5, and offers a convenient way to iterate over arrays and other collections. You use the reference to the SimData collection, listCases , to define the for-each loop.
for (SimData sD : listCases) {
 
 
}
Description Code
  • Within the loop, the first process is to use the SimRunner object to carry out the tasks such as setting various conditions, clearing the previous solution, or running the case. Recall that these processes were defined in the method runCase(). You call this method, and pass it the current SimData object sD and an integer to define the number of iterations to carry out when each simulation is run, in this case 5.
runner.runCase(sD, 5);
  • Next, following the logical process of running a simulation, you extract the drag coefficient and save hardcopies of scenes and a plot. As before, call the methods that were defined in the relevant nested classes. The cross-wind angle was retrieved from the current SimData object to save each scene under a suitable name.
writer.writeDataLine(sD);
 
postP.saveVelMagScene(
  folder + “/velMag” + sD.getAngle() + “.png”
);
  • Finally, use another statement copied from the recorded macro to save the simulation after it has been run.
theSim.saveState(
  folder + “/train” + sD.getAngle() + “.sim”
);