|
[Sponsors] |
Macro to generate CSV by reading report values |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
December 19, 2018, 04:38 |
Macro to generate CSV by reading report values
|
#1 |
New Member
Ashwin M
Join Date: Dec 2018
Posts: 3
Rep Power: 7 |
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 |
|
December 19, 2018, 10:05 |
|
#2 |
Member
Join Date: Nov 2015
Posts: 57
Rep Power: 10 |
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); } |
|
December 20, 2018, 02:28 |
|
#3 |
New Member
Ashwin M
Join Date: Dec 2018
Posts: 3
Rep Power: 7 |
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. |
|
December 22, 2018, 20:33 |
|
#4 |
Senior Member
Join Date: Nov 2010
Location: USA
Posts: 1,232
Rep Power: 24 |
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. |
|
December 24, 2018, 00:37 |
|
#5 |
New Member
Ashwin M
Join Date: Dec 2018
Posts: 3
Rep Power: 7 |
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 25, 2018 at 00:33. |
|
October 3, 2019, 21:08 |
|
#6 |
New Member
Zhengwei Long
Join Date: Oct 2019
Location: Tianjin, China
Posts: 8
Rep Power: 7 |
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.
|
|
October 7, 2019, 05:29 |
|
#7 |
Senior Member
Sebastian Engel
Join Date: Jun 2011
Location: Germany
Posts: 567
Rep Power: 21 |
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 |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
TimeVaryingMappedFixedValue | irishdave | OpenFOAM Running, Solving & CFD | 32 | June 16, 2021 07:55 |
OpenFoam output to CSV; correlate coordinates to internal values | CoolKau | OpenFOAM Post-Processing | 0 | October 29, 2014 09:03 |
Java Macro with a condition on a Report | Mark_89 | STAR-CCM+ | 0 | August 21, 2013 08:05 |
reading values from a text file into a java macro | Jayson Martinez | Siemens | 1 | May 23, 2008 18:58 |
Help: Reading values in derived types for VFortran | Wee | Main CFD Forum | 0 | October 15, 2006 10:39 |