CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > Siemens > STAR-CCM+

Macro to generate CSV by reading report values

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 19, 2018, 03:38
Exclamation Macro to generate CSV by reading report values
  #1
New Member
 
Ashwin M
Join Date: Dec 2018
Posts: 3
Rep Power: 7
ashmrao is on a distinguished road
Hi,

I am trying to generate a macro which writes report values to a CSV for multiple sim files at a time.

This issue is, when this macro is called from a simple UI created in Star CCM+
It is showing an error as "Null point Exception" for the first sim file loaded.

Please guide how to resolve or any alternate suggestions

Thanks
Ashwin
ashmrao is offline   Reply With Quote

Old   December 19, 2018, 09:05
Default
  #2
Member
 
Join Date: Nov 2015
Posts: 57
Rep Power: 10
taillanm is on a distinguished road
From the steve portal : https://thesteveportal.plm.automatio...xt-output-file





Simulation sim = getActiveSimulation();

String outputfile = "output.txt";



try {

sim.println("Writing report values to file " + resolvePath(outputfile));

PrintWriter out = new PrintWriter(new FileWriter(new File(resolvePath(outputfile))));



for (Report report : sim.getReportManager().getObjects()) {

String reportName = report.getPresentationName();

double reportValue = report.getReportMonitorValue();

String reportUnits = report.getUnits().getPresentationName();

out.println(reportName + " = " + reportValue + " " + reportUnits);



}

out.close();

} catch (IOException ex) {

sim.println(ex);
}
taillanm is offline   Reply With Quote

Old   December 20, 2018, 01:28
Default
  #3
New Member
 
Ashwin M
Join Date: Dec 2018
Posts: 3
Rep Power: 7
ashmrao is on a distinguished road
Thanks, taillanm

Tried the below code, , here the first .sim file generated and stops at null exception.


Simulation simulation_0;
//BufferedWriter bwout = null;
JFrame theFrame = new JFrame();
JButton btnExport;
Logger logger = Logger.getLogger("MyLog");
FileHandler fh;
String fName;
public void execute() {
simulation_0 = getActiveSimulation();
new nFrame().newFrame();
simulation_0.println("frame!!");
try {
fh = new FileHandler("D:\\MyLogFile.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
logger.info("My first log");
}
catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
logger.info("***********");
}//execute end

public class nFrame extends JFrame {
BufferedWriter bwout = null;

public void newFrame() {

initComponents();
}
private void initComponents() {
logger.info("*****initcompoent method******");
//theFrame.setSize(new Dimension(540, 390));
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
theFrame.getContentPane().setFont(new Font("Calibri", Font.PLAIN, 15));
theFrame.getContentPane().setBackground(Color.WHIT E);
theFrame.setBackground(SystemColor.window);
theFrame.setSize(new Dimension(495, 328));
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
theFrame.setTitle("Coolant Jacket");
theFrame.getContentPane().setLayout(null);

btnExport = new JButton("Play");
btnExport.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
logger.info("*****Play button clicked******");
simulation_0.println("Files are getting generated....please wait..");
theFrame.dispose();
try{
bwout = new BufferedWriter(new FileWriter(resolvePath("D:\\report.csv")));
logger.info("*****bwout report.csv******");
bwout.write("sim, Report Name, Value, Unit, \n");
bwout.newLine();
bwout.close();
for(int i=0; i<2;i++){
generate_sim(0,-2.0+i,0,"200");
generate_report();

//new StarScript(getActiveSimulation(), new java.io.File("report.java")).play();
}
}catch(Exception e){
simulation_0.println("Error"+e);
}
}
});
btnExport.setFont(new Font("Calibri", Font.PLAIN, 15));
btnExport.setBounds(50, 257, 89, 23);
theFrame.getContentPane().add(btnExport);
theFrame.setVisible(true);
theFrame.repaint();
}//initcomponents end

public void generate_sim(double x,double y,double z,String flowrate) {
logger.info("******generating simmm*****");
simulation_0=getActiveSimulation();
fName="FR"+flowrate+"x"+String.valueOf((int)x)+"y" +String.valueOf((int)y)+"z"+String.valueOf((int)z) ;
Units units_0 = simulation_0.getUnitsManager().getPreferredUnits(n ew IntVector(new int[] {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}));
MeshPart meshPart_0 = ((MeshPart) simulation_0.get(SimulationPartManager.class).getP art("Cylinder"));
Units units_1 = ((Units) simulation_0.getUnitsManager().getObject("mm"));
LabCoordinateSystem labCoordinateSystem_0 = simulation_0.getCoordinateSystemManager().getLabCo ordinateSystem();
CartesianCoordinateSystem cartesianCoordinateSystem_0 = ((CartesianCoordinateSystem) labCoordinateSystem_0.getLocalCoordinateSystemMana ger().getObject("Cartesian 1"));
simulation_0.get(SimulationPartManager.class).tran slateParts(new NeoObjectVector(new Object[] {meshPart_0}), new DoubleVector(new double[] {x, y, z}), new NeoObjectVector(new Object[] {units_0, units_0, units_1}), labCoordinateSystem_0);
MeshPipelineController meshPipelineController_0 = simulation_0.get(MeshPipelineController.class);
UserFieldFunction userFieldFunction_0 = ((UserFieldFunction) simulation_0.getFieldFunctionManager().getFunction ("FlowRate_PumpOut_lpm"));
userFieldFunction_0.setDefinition(flowrate);
//simulation_0.saveState("D:"+fName+".sim");
simulation_0.getSimulationIterator().run();
simulation_0.saveState("D:"+fName+".sim");
generate_report();
}//generate_sim end
public void generate_report(){
logger.info("******write_to_csv*****");
//Simulation simulation_1 = new Simulation(txtLocation.getText().toString()+""+fNa me+"s1.sim");
Simulation simulation_1=getActiveSimulation();
Solution solution_0 = simulation_0.getSolution();
solution_0.clearSolution();
solution_0.initializeSolution();
String simulationName = simulation_1.getPresentationName();
logger.info("******write_to_csv*****"+simulationNa me);
simulation_0.println("Simulation Name:" + simulationName);
try{
BufferedWriter bwout = new BufferedWriter(new FileWriter("D:\\report.csv",true));
AreaAverageReport expressionReport_0 = ((AreaAverageReport) simulation_1.getReportManager().getReport("PEG42_K nowDbase_2.PressDistri_7.TstatOut_stat"));
logger.info("******avg report**");
simulation_1.println("value:"+expressionReport_0.g etValue());
simulation_1.println("value:"+expressionReport_0.g etUnits());
simulation_1.println("value:"+expressionReport_0.g etPresentationName());
String s=simulationName+", "+expressionReport_0.getPresentationName()+", "+expressionReport_0.getValue()+", "+expressionReport_0.getUnits()+" \n";
simulation_1.println(s);
bwout.write(s);
bwout.close();
}catch(Exception e){
simulation_1.println(e.getMessage());
}
}
}
}


the code i had tried, here the first .sim file generated will have null values in the report.
ashmrao is offline   Reply With Quote

Old   December 22, 2018, 19:33
Default
  #4
Senior Member
 
Join Date: Nov 2010
Location: USA
Posts: 1,232
Rep Power: 24
me3840 is on a distinguished road
Can you clean the code up? It's very hard to debug like this.

Why do you need a GUI for this? You're just writing data to a text file, this is easily done in batch, and that will get rid of probably half of this code.
me3840 is offline   Reply With Quote

Old   December 23, 2018, 23:37
Default
  #5
New Member
 
Ashwin M
Join Date: Dec 2018
Posts: 3
Rep Power: 7
ashmrao is on a distinguished road
Hi,

Using the GUI we are providing the parameters like flowrate, translation directions and on click of a button we are trying to generate the .sim files and also generate the report for the generated .sim file.

But here, we are unable to generate the report for the sim files generated, it is giving null values.

can you please suggest what might have gone wrong in the code?

private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
theFrame.getContentPane().setFont(new Font("Calibri", Font.PLAIN, 15));
theFrame.getContentPane().setBackground(Color.WHIT E);
theFrame.setBackground(SystemColor.window);
theFrame.setSize(new Dimension(100,100));


theFrame.getContentPane().setLayout(null);
JButton btnPlay = new JButton("Play");
btnPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{


simulation_0=getActiveSimulation();
Units units_0 = simulation_0.getUnitsManager().getPreferredUnits(n ew IntVector(new int[] {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}));
MeshPart meshPart_0 = ((MeshPart) simulation_0.get(SimulationPartManager.class).getP art("Cylinder"));
logger.info("*****Cylinder******");
Units units_1 = ((Units) simulation_0.getUnitsManager().getObject("mm"));

LabCoordinateSystem labCoordinateSystem_0 = simulation_0.getCoordinateSystemManager().getLabCo ordinateSystem();
CartesianCoordinateSystem cartesianCoordinateSystem_0 = ((CartesianCoordinateSystem) labCoordinateSystem_0.getLocalCoordinateSystemMana ger().getObject("Cartesian 1"));
simulation_0.get(SimulationPartManager.class).tran slateParts(new NeoObjectVector(new Object[] {meshPart_0}), new DoubleVector(new double[] {0.0,-2.0,0.0}), new NeoObjectVector(new Object[] {units_0, units_0, units_1}), labCoordinateSystem_0);

MeshPipelineController meshPipelineController_0 = simulation_0.get(MeshPipelineController.class);
UserFieldFunction userFieldFunction_0 = ((UserFieldFunction) simulation_0.getFieldFunctionManager().getFunction ("FlowRate_PumpOut_lpm"));
userFieldFunction_0.setDefinition("200");

simulation_0.getSolution().clearSolution();

StepStoppingCriterion stepStoppingCriterion_0 = ((StepStoppingCriterion) simulation_0.getSolverStoppingCriterionManager().g etSolverStoppingCriterion("Maximum Steps"));
stepStoppingCriterion_0.setMaximumNumberSteps(2000 );

simulation_0.getSimulationIterator().run();

simulation_0.saveState("D:\\s1.sim");

AreaAverageReport expressionReport_0 = ((AreaAverageReport) simulation_0.getReportManager().getReport("PEG42_K nowDbase_2.PressDistri_7.TstatOut_stat"));
simulation_0.println("value:"+expressionReport_0.g etValue());


theFrame.dispose();
}catch (Exception e){
simulation_0.println("ERROR!!!!!"+e.getMessage());
}
}
});
btnPlay.setBounds(10, 11, 89, 23);
theFrame.getContentPane().add(btnPlay);
theFrame.setAlwaysOnTop(false);
theFrame.setVisible(true);
theFrame.repaint();
}

Last edited by ashmrao; December 24, 2018 at 23:33.
ashmrao is offline   Reply With Quote

Old   October 3, 2019, 20:08
Default
  #6
lzw
New Member
 
Zhengwei Long
Join Date: Oct 2019
Location: Tianjin, China
Posts: 8
Rep Power: 6
lzw is on a distinguished road
Have you solve this problem? I met the same problem. The getReportMonitorValue is ok in the play macro model. But it is wrong when use it in the UI program. I checked all the things. But I still have not found the error.
lzw is offline   Reply With Quote

Old   October 7, 2019, 04:29
Default
  #7
Senior Member
 
Sebastian Engel
Join Date: Jun 2011
Location: Germany
Posts: 566
Rep Power: 20
bluebase will become famous soon enough
Hi lzw,

could you elaborate what is wrong when you use the GUI and your macro?

Such as: Does it not write the file in the intended location?


Edit: we may continue the discussion in your thread: Starccm+ Report Java macro error
bluebase is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
TimeVaryingMappedFixedValue irishdave OpenFOAM Running, Solving & CFD 32 June 16, 2021 06:55
OpenFoam output to CSV; correlate coordinates to internal values CoolKau OpenFOAM Post-Processing 0 October 29, 2014 08:03
Java Macro with a condition on a Report Mark_89 STAR-CCM+ 0 August 21, 2013 07:05
reading values from a text file into a java macro Jayson Martinez Siemens 1 May 23, 2008 17:58
Help: Reading values in derived types for VFortran Wee Main CFD Forum 0 October 15, 2006 09:39


All times are GMT -4. The time now is 11:17.