// STAR-CCM+ macro: Extract_1D_HeatFlux.java
// Written by STAR-CCM+ 10.06.010
package macro;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.*;
import star.common.*;
import star.base.neo.*;
import star.vis.*;
import star.base.report.*;
import star.meshing.*;
public class Extract_1D_Temperature_Bed extends StarMacro {
public void execute() {
execute0();
}
private void execute0() {
// User Defined Variables //
String Bound = "Bed";
String Reg = "Kiln";
String Direction = "Z";
int NumberOfDivisions = 50;
// get range of direction on surface of interest
double[] range = getRange(Direction, Bound, Reg);
// get 1D data based in range and number of divisions
double[] data = getData(range, NumberOfDivisions);
}
/* start of subroutine: getData */
private double[] getData(double[] range, int NumberOfDivisions) {
double[] data = new double[NumberOfDivisions];
Simulation sim = getActiveSimulation();
/* Get Threshold object */
ThresholdPart thresh = (ThresholdPart) sim.getPartManager().getObject("1D_Threshold");
/* Get Surface Average Report, associate threshold to it and set Boundary Heat Flux */
AreaAverageReport avReport = (AreaAverageReport) sim.getReportManager().getReport("1D_Segment_Average");
avReport.getParts().setObjects(thresh);
PrimitiveFieldFunction temp =
((PrimitiveFieldFunction) sim.getFieldFunctionManager().getFunction("Temperature"));
avReport.setScalar(temp);
/* Loop over the number of segments to compute the average */
double SegmentLength = (range[1] - range[0]) / NumberOfDivisions;
/* Open file to write data to */
try {
File f = new File(resolvePath("1D_Temperature_Bed.csv"));
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
String Header = "Segment start coord, Segment end coord, Segment mid coord, Temperature";
bw.write(Header);
bw.newLine();
bw.close();
} catch (Exception e) {
sim.println("Error unable to write data to file.");
}
for (int i = 0; i < NumberOfDivisions; i++) {
/* Set threshold values */
thresh.getRangeQuantities().setArray(new DoubleVector(new double[]{range[0] +
(i * SegmentLength), range[0] + ((i + 1) * SegmentLength)}));
/* get average value */
data[i] = avReport.getValue();
/* Pretty formatting for output */
String Coord1 = String.format(Locale.UK, "%6.3f", Math.abs(range[0] + (i * SegmentLength)));
String Coord2 = String.format(Locale.UK, "%6.3f", Math.abs(range[0] + ((i + 1) * SegmentLength)));
String Coordm = String.format(Locale.UK, "%6.3f", Math.abs(range[0] + ((i + 0.5 ) * SegmentLength)));
String q = String.format(Locale.UK, "%6.3f", data[i]);
if (i == 0) {
sim.println("Start Coord, End Coord, Mid Coord Segment Average Temperature");
}
sim.println(Coord1 + ", " + Coord2 + ", " + Coordm + "," + q);
/* Write data to file */
try {
File f = new File(resolvePath("1D_Temperature_Bed.csv"));
BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
String line = Coord1 + ", " + Coord2 + ", " + Coordm + "," + q;
bw.write(line);
bw.newLine();
bw.close();
} catch (Exception e) {
sim.println("Error unable to write data to file.");
}
}
/* Tidy up objects used */
/* sim.getReportManager().removeObjects(avReport);
sim.getPartManager().removeObjects(thresh);*/
return data;
}
/* End of subroutine getData */
/* Start of subroutine getRange */
private double[] getRange(String Direction, String Bound, String Reg) {
double[] range = {0, 0};
Simulation sim = getActiveSimulation();
/* Create min, max and surface average report */
MaxReport maxRep = sim.getReportManager().createReport(MaxReport.class);
MinReport minRep = sim.getReportManager().createReport(MinReport.class);
AreaAverageReport saRep = sim.getReportManager().createReport(AreaAverageReport.class);
saRep.setPresentationName("1D_Segment_Average");
/* Set Boundary Surface, i.e. Bed boundary as Parts */
Boundary surf = sim.getRegionManager().getRegion(Reg).getBoundaryManager().getBoundary(Bound);
maxRep.getParts().setObjects(surf);
minRep.getParts().setObjects(surf);
/* Units */
Units units_0 = sim.getUnitsManager().getPreferredUnits(new 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}));
/* Set Position as Sclar Field Function and set direction */
PrimitiveFieldFunction primitiveFieldFunction_1 =
((PrimitiveFieldFunction) sim.getFieldFunctionManager().getFunction("Position"));
if (Direction == "X") {
VectorComponentFieldFunction component =
((VectorComponentFieldFunction) primitiveFieldFunction_1.getComponentFunction(0));
ThresholdPart threshold = sim.getPartManager().createThresholdPart(new NeoObjectVector(new Object[]{surf}),
new DoubleVector(new double[]{0.0, 1.0}), units_0, component, 0);
threshold.setPresentationName("1D_Threshold");
maxRep.setScalar(component);
minRep.setScalar(component);
} else if (Direction == "Y") {
VectorComponentFieldFunction component =
((VectorComponentFieldFunction) primitiveFieldFunction_1.getComponentFunction(1));
ThresholdPart threshold = sim.getPartManager().createThresholdPart(new NeoObjectVector(new Object[]{surf}),
new DoubleVector(new double[]{0.0, 1.0}), units_0, component, 0);
threshold.setPresentationName("1D_Threshold");
maxRep.setScalar(component);
minRep.setScalar(component);
} else if (Direction == "Z") {
VectorComponentFieldFunction component =
((VectorComponentFieldFunction) primitiveFieldFunction_1.getComponentFunction(2));
ThresholdPart threshold = sim.getPartManager().createThresholdPart(new NeoObjectVector(new Object[]{surf}),
new DoubleVector(new double[]{0.0, 1.0}), units_0, component, 0);
threshold.setPresentationName("1D_Threshold");
maxRep.setScalar(component);
minRep.setScalar(component);
} else {
sim.println("Direction unknown - please check that you have specified X, Y or Z");
}
/* Get min and max values */
range[0] = minRep.getReportMonitorValue();
range[1] = maxRep.getReportMonitorValue();
sim.println("Range of Boundary " + surf.getPresentationName() +
" in " + Direction + " Direction is " + range[0] + "m to " + range[1] + "m.");
/* delete reports */
sim.getReportManager().removeObjects(minRep, maxRep);
return range;
}
// End of subroutine getRange
}
// End of Main Program