Bug fix issue 'No quality data generated'
HydraulicSim manages a QualitySim data member when Water quality analysis is needed in order to fill the water quality data in output.
It fixes the issue: https://github.com/Baseform/Baseform-Epanet-Java-Library/issues/4
Dear Alvaro,
Thanks a lot for contributing. The design idea here was to separate Hydraulic and Quality simulations at least at this level. The idea is that you may want to run them together or separately using the sample .inp layout and options by invoking different "simulators". Hybrid simulations are, of course desirable and can easily be achieved. Please find below a sample piece of code which does just that. Feel free to contact me directly to discuss possible ways of getting a simpler and better API.
public class Sample_HybridSimulation { public static void main(String[] args) throws ENException, IOException {
String filename = "files/Net3.inp";
Logger log = Logger.getLogger(Sample_RunSimulation.class.toString());
log.setUseParentHandlers(false);
// Parse INP file.
Network netINP = new Network();
InputParser parserINP = InputParser.create(Network.FileType.INP_FILE, log);
parserINP.parse(netINP, new File(filename));
File hydFile = File.createTempFile("outFile", "bin");
HydraulicSim hydSim = new HydraulicSim(netINP, log);
QualitySim qualSim = new QualitySim(netINP, log);
long rTime = 0;
BufferedOutputStream buffer = new BufferedOutputStream(new FileOutputStream(hydFile), 512 * 1024);
DataOutput out = new DataOutputStream(buffer);
PropertiesMap pMap = netINP.getPropertiesMap();
AwareStep.writeHeader(out, hydSim,pMap.getRstart(),pMap.getRstep(),pMap.getDuration());
while (true) {
long time = hydSim.getHtime();
long step = hydSim.simulateSingleStep();
if ( time >= rTime) {
rTime += netINP.getPropertiesMap().getRstep();
AwareStep.writeHydAndQual(out, hydSim, qualSim, step, time);
}
qualSim.simulateSingleStep(hydSim.getnNodes(), hydSim.getnLinks(), step);
if (step == 0)
break;
}
buffer.close();
FieldsMap fMap = netINP.getFieldsMap();
BufferedInputStream inBuffer = new BufferedInputStream(new FileInputStream(hydFile));
DataInput input = new DataInputStream(inBuffer);
AwareStep.HeaderInfo header = AwareStep.readHeader(input);
for (long current = 0; current <= netINP.getPropertiesMap().getDuration(); current += netINP.getPropertiesMap().getRstep()) {
AwareStep step = new AwareStep(input,header);
System.out.println("Report Time : " + step.getTime());
for(int id = 0;id<header.nodes;id++)
System.out.print(String.format("%.2f\t",fMap.revertUnit(FieldsMap.Type.QUALITY,step.getNodeQuality(id))));
System.out.print("\n");
}
}
}
Hi, thank you very much for your reply and clarifications.
I have rewritten the code creating a new "HydraulicAndQualitySim" class that explicitly mixes the hydraulic and water quality simulations. The developer then decides which uses as needed.
This change can be a better implementation which does not modify the current behavior. I hope this is most welcome.
Best Regards Alvaro