CFD Online Discussion Forums

CFD Online Discussion Forums (
-   STAR-CCM+ (
-   -   Automatically export all reports in batch (

f-w July 21, 2011 20:03

Automatically export all reports in batch

I typically run many simulations in batch mode with accompanying java macros that set up the conditions; these simulations have many reports. I'm looking for a way to export all reports after simulation convergence (in batch mode) into a csv file.

I have found a java macro which exports all reports to a csv file by running it manually on a simulation, but I need to run it automatically and only after convergence. Currently, the last command in my setup macros (which I use with each simulation in batch mode) is to RUN the simulation.

How do I go about running the report export macro before the simulation closes (and moves on to the next simulation) in batch mode?


Attached is the report export macro I got from cd-adapco's java hut (author:Sangamesh)


// STAR-CCM+ macro:
package macro;

import java.util.*;
import java.nio.*;
import star.common.*;
import star.base.neo.*;
import star.flow.*;

public class Report_to_csv extends StarMacro {

BufferedWriter bwout = null;

public void execute() {

try {

Simulation simulation_0 = getActiveSimulation();

// Collecting the simualtion file name
String simulationName = simulation_0.getPresentationName();
simulation_0.println("Simulation Name:" + simulationName);

// Open Buffered Input and Output Readers
// Creating file with name "<sim_file_name>+report.csv"
bwout = new BufferedWriter(new FileWriter(resolvePath(simulationName +"report.csv")));
bwout.write("Report Name, Value, Unit, \n");

Collection<Report> reportCollection = simulation_0.getReportManager().getObjects();

for (Report thisReport : reportCollection){

String fieldLocationName = thisReport.getPresentationName();
Double fieldValue = thisReport.getReportMonitorValue();
String fieldUnits = thisReport.getUnits().toString();

// Printing to chek in output window
simulation_0.println("Field Location :" + fieldLocationName);
simulation_0.println(" Field Value :" + fieldValue);
simulation_0.println(" Field Units :" + fieldUnits);

// Write Output file as "sim file name"+report.csv
bwout.write( fieldLocationName + ", " +fieldValue + ", " + fieldUnits +"\n");


} catch (IOException iOException) {


f-w July 25, 2011 19:51

I forgot that I can play a macro from within a macro. All I had to do was add the following line to the end of my conditioning macro:

new StarScript(getActiveSimulation(),new""))).p lay();

cbisw August 3, 2011 09:14

Not 100% sure if I understand what you are doing. Just run a macro once the simulation run finishes? I wrote a couple of macros a few months ago to do stuff after each iteration or timestep, maybe they are helpful to you.

First I toyed around with the observer pattern:


class ObserverTest extends StarMacro {

publicvoid execute() {

Simulation sim = getActiveSimulation();

SimulationIterator simIt = sim.getSimulationIterator();
NeoObservable simObs = simIt.getIterationObservable();
MyObs myObs =
new MyObs(sim, simIt);


class MyObs extends IterationNotifier.Receiver {

private Simulation sim;
private SimulationIterator simIt;

MyObs(Simulation sim, SimulationIterator simIt) {
this.sim = sim;
this.simIt = simIt;

publicvoid update(Observable o, Object msg) {

sim.println("Observer has been notified");
// do whatever you want here

// e.g. check whether convergence has been reached

// and do stuff accordingly



Then I used another approach by making my macro run the simulation itself timestep by timestep. You would probably have to adjust your stopping criteria since you obviously have your own ones based on convergence. I simplified it and put comments where you can do things a) after every step and b) after last step:


// execute() is called by Star-CCM+ when macro is run
publicvoid execute() {

System.out.println("MACRO> Macro XYZ has been started.");


// references to current simulation and solution
sim = getActiveSimulation();
out.println("Active simulation is '"+sim.getPresentationName()+"'.");
sol = sim.getSolution();

// initialize solution if necessary for starting values
// WARNING: not initializing the solution will cause the macro to fail because
// the number of points cannot be extracted the way it is done here
if( sol.isInitialized() == false ) {
out.println("Solution not initialized, initializing...");

// status
int startTimelevel = sim.getSimulationIterator().getCurrentTimeLevel();
int startIteration = sim.getSimulationIterator().getCurrentIteration();
double timestep = ((ImplicitUnsteadySolver)sim.getSolverManager().getSolver(ImplicitUnsteadySolv er.class)).getTimeStep().getValue();

// set the current iteration
iteration = startIteration;
timelevel = startTimelevel;



do {

// run a single time step and wait until it has completed.
long timeBeforeStep = System.nanoTime();

// output some information
iteration = sim.getSimulationIterator().getCurrentIteration();
out.println("Iteration: "+iteration);
out.println("Timelevel: "+sim.getSimulationIterator().getCurrentTimeLevel());

// do whatever you want to do each step here

long timeAfterStep = System.nanoTime();
long timeRequirement = timeAfterStep - timeBeforeStep;
out.println("Total time for this iteration: "+timeRequirement + " ns.");

} while (IsCriteriaSatisfied(sim) == false);

// at this point, any of the stopping criteria has been satisfied, execute() returns
// do whatever you want to do after simulation has ended here
out.println("MACRO> Stopping criterion met, quitting macro.");


// returns true if any stopping criteria has been met, taking into account the three default stopping criteria
privateboolean IsCriteriaSatisfied(Simulation Sim) {

PhysicalTimeStoppingCriterion physTimeCrit = ((PhysicalTimeStoppingCriterion) Sim.getSolverStoppingCriterionManager().getSolverS toppingCriterion("Maximum Physical Time"));
StepStoppingCriterion stepCrit = ((StepStoppingCriterion) Sim.getSolverStoppingCriterionManager().getSolverS toppingCriterion("Maximum Steps"));
AbortFileStoppingCriterion abortFileCrit = (AbortFileStoppingCriterion) Sim.getSolverStoppingCriterionManager().getSolverS toppingCriterion("Stop File");

if( physTimeCrit.getIsUsed() ) {
if( physTimeCrit.getIsSatisfied() ) {
Sim.println("MACRO> PhysicalTimeStoppingCriterion met");

if( stepCrit.getIsUsed() ) {
if( stepCrit.getIsSatisfied() ) {
Sim.println("MACRO> StepStoppingCriterion met");

if( abortFileCrit.getIsUsed() ) {
if( abortFileCrit.getIsSatisfied() ) {
Sim.println("MACRO> AbortFileStoppingCriterion met");



f-w August 3, 2011 19:05

Thanks for your contribution Sebastian, but as you can read from my 2nd post, I figured it out ...

cbisw August 4, 2011 01:02

Oh alright, that came across like some kind of extra information. Guess I shouldn't try to answer threads in a hurry shortly before calling it a day ;)

All times are GMT -4. The time now is 02:01.