Skip to content

Commit

Permalink
Regression test property flash (#373)
Browse files Browse the repository at this point in the history
* add regression test input
* test: disable failing test
  • Loading branch information
asmfstatoil authored May 10, 2023
1 parent 92d52ac commit 69eac26
Show file tree
Hide file tree
Showing 63 changed files with 25,941 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@
<version>53.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
6 changes: 6 additions & 0 deletions pomJava8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@
<version>48.4.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1977,7 +1977,6 @@ public CalculationResult propertyFlash(List<Double> Spec1, List<Double> Spec2, i
Boolean hasOnlineFractions = onlineFractions != null;
if (hasOnlineFractions) {
double range = 5;

for (int t = 0; t < sum.length; t++) {
sum[t] = 0.0;
for (int comp = 0; comp < onlineFractions.size(); comp++) {
Expand All @@ -1994,6 +1993,15 @@ public CalculationResult propertyFlash(List<Double> Spec1, List<Double> Spec2, i
this.system.setTotalNumberOfMoles(1);
}
} else {
/*
* // New attempt:
*
* // Have to init here to get correct MoleFractionsSum() this.system.init(0);
*
* // Annoying that MoleFractionsSum is not normalized. sum[0] =
* this.system.getMoleFractionsSum();
*/

double[] fraction = this.system.getMolarComposition();
sum[0] = 0.0;
for (int comp = 0; comp < fraction.length; comp++) {
Expand Down Expand Up @@ -2033,6 +2041,20 @@ public CalculationResult propertyFlash(List<Double> Spec1, List<Double> Spec2, i
}

if (hasOnlineFractions) {
/*
* // New attempt:
*
* this.system.setEmptyFluid();
*
* // Components in system with no corresponding value in onlineFractions will be zero.
* for (int componentNumber = 0; componentNumber < onlineFractions .size();
* componentNumber++) { this.system.addComponent(componentNumber,
* onlineFractions.get(componentNumber).get(t).doubleValue()); }
*
* if (this.system.getTotalNumberOfMoles() < 1e-5) { this.system.setTotalNumberOfMoles(1);
* }
*/

// Remaining fractions will be set to 0.0
double[] fraction = new double[this.system.getNumberOfComponents()];
for (int comp = 0; comp < onlineFractions.size(); comp++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package neqsim.thermodynamicOperations;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import neqsim.api.ioc.CalculationResult;
import neqsim.thermo.system.SystemInterface;
import neqsim.thermo.system.SystemProperties;
Expand Down Expand Up @@ -103,8 +113,7 @@ void testNeqSimPython2() {
Assertions.assertEquals(res.fluidProperties[0].length, propNames.length);

// Redo propertyFlash with online fractions, but still only one data point
List<List<Double>> onlineFractions =
createDummyRequest(thermoSystem.getMolarComposition(), 1);
List<List<Double>> onlineFractions = createDummyRequest(thermoSystem.getMolarComposition(), 1);
CalculationResult res1 = thermoOps.propertyFlash(jP, jT, 1, null, onlineFractions);
// Assert no calculation failed
for (String errorMessage : res1.calculationError) {
Expand Down Expand Up @@ -254,6 +263,66 @@ void testpropertyFlashOnlineSingle() {
Assertions.assertEquals(len, s.fluidProperties.length);
}

@Disabled
@Test
@SuppressWarnings("unchecked")
void testpropertyFlashRegressions() throws IOException {
// todo: make these tests work
// make output log of differences per failing test and see check if it is related to change in
// component input data
Collection<TestData> testData = getTestData();

for (TestData test : testData) {
HashMap<String, Object> inputData = test.getInput();

SystemInterface fluid = new SystemSrkEos(273.15 + 45.0, 22.0);

ArrayList<String> compNames = (ArrayList<String>) inputData.get("components");
ArrayList<Double> fractions = (ArrayList<Double>) inputData.get("fractions");

if (compNames == null) {
System.out.println("Skips test " + test.toString());
/*
* for (int k = 0; k < fractions.size(); k++) { fluid.addComponent(k, fractions.get(k)); }
*/
continue;
} else {
for (int k = 0; k < compNames.size(); k++) {
fluid.addComponent(compNames.get(k), fractions.get(k));
}
}

fluid.init(0);

ArrayList<Double> sp1 = (ArrayList<Double>) inputData.get("Sp1");
ArrayList<Double> sp2 = (ArrayList<Double>) inputData.get("Sp2");

ThermodynamicOperations ops = new ThermodynamicOperations(fluid);
int flashMode = (int) inputData.get("FlashMode");
CalculationResult s = ops.propertyFlash(sp1, sp2, flashMode, compNames, null);
for (String errorMessage : s.calculationError) {
Assertions.assertNull(errorMessage, "Calculation returned: " + errorMessage);
}

CalculationResult expected = test.getOutput();

for (int nSamp = 0; nSamp < s.fluidProperties.length; nSamp++) {
for (int nProp = 0; nProp < s.fluidProperties[nSamp].length; nProp++) {
if (Double.isNaN(expected.fluidProperties[nSamp][nProp])) {
Assertions.assertEquals(expected.fluidProperties[nSamp][nProp],
s.fluidProperties[nSamp][nProp],
"Test " + (nSamp + 1) + " Property " + SystemProperties.getPropertyNames()[nProp]);
} else {
Assertions.assertEquals(expected.fluidProperties[nSamp][nProp],
s.fluidProperties[nSamp][nProp], 1e-5,
"Test " + (nSamp + 1) + " Property " + SystemProperties.getPropertyNames()[nProp]);
}
}
}
// Assertions.assertEquals(expected, s);
}
}

private List<List<Double>> createDummyRequest(double[] fractions, int len) {
List<List<Double>> onlineFractions = new ArrayList<List<Double>>();

Expand All @@ -267,4 +336,111 @@ private List<List<Double>> createDummyRequest(double[] fractions, int len) {

return onlineFractions;
}

private Collection<TestData> getTestData() throws IOException {
ClassLoader classLoader = getClass().getClassLoader();
File folder =
new File(classLoader.getResource("neqsim/thermodynamicOperations/testcases").getFile());

HashMap<String, TestData> testData = new HashMap<>();

File[] directoryListing = folder.listFiles();
for (File child : directoryListing) {
String[] n = child.getName().split("_");

if (testData.get(n[0]) == null) {
testData.put(n[0], new TestData(n[0]));
}

if ("I.json".equals(n[1])) {
testData.get(n[0]).setInputFile(child.getAbsolutePath());
} else if ("O.json".equals(n[1])) {
testData.get(n[0]).setOutputFile(child.getAbsolutePath());
}
}

return testData.values();
}

private class TestData {

private final String name;
private String inputFile;
private String outputFile;
private HashMap<String, Object> input;

public TestData(String name) {
this.name = name;
}

public void setInputFile(String inputFile) throws IOException {
this.setInput(inputFile);
this.inputFile = inputFile;
}

public String getOutputFile() {
return outputFile;
}

public void setOutputFile(String outputFile) {
this.outputFile = outputFile;
}

@SuppressWarnings("unchecked")
private void setInput(String inputFile) throws IOException {
String fileContents = new String(Files.readAllBytes(Paths.get(inputFile)));
Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, Object>>() {}.getType();

input = gson.fromJson(fileContents, type);

input.replace("fn", Math.toIntExact(Math.round((double) input.get("fn"))));
input.replace("FlashMode", Math.toIntExact(Math.round((double) input.get("FlashMode"))));

ArrayList<Double> sp_in_pa = (ArrayList<Double>) input.get("Sp1");
ArrayList<Double> sp1 = new ArrayList<Double>();

for (int k = 0; k < sp_in_pa.size(); k++) {
sp1.add(sp_in_pa.get(k) / 1e5);
}
input.replace("Sp1", sp1);
}

private HashMap<String, Object> getInput() {
return input;
}

@SuppressWarnings("unchecked")
public CalculationResult getOutput() throws IOException {
String fileContents = new String(Files.readAllBytes(Paths.get(this.getOutputFile())));
Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, Object>>() {}.getType();

HashMap<String, Object> outputData = gson.fromJson(fileContents, type);
ArrayList<ArrayList<Double>> calcresult =
(ArrayList<ArrayList<Double>>) outputData.get("calcresult");

Double[][] calcResult = new Double[calcresult.size()][];
for (int kSample = 0; kSample < calcresult.size(); kSample++) {
calcResult[kSample] = new Double[calcresult.get(kSample).size()];
for (int kProp = 0; kProp < calcresult.get(kSample).size(); kProp++) {
try {
calcResult[kSample][kProp] = calcresult.get(kSample).get(kProp);

} catch (Exception e) {
calcResult[kSample][kProp] = Double.NaN;
}
}
}

ArrayList<String> calcerrors = (ArrayList<String>) outputData.get("calcerrors");

return new CalculationResult(calcResult, calcerrors.toArray(new String[0]));
}

@Override
public String toString() {
return "TestData{" + "name=" + name + ", input=" + inputFile + ", output=" + outputFile + '}';
}
}
}
Loading

0 comments on commit 69eac26

Please sign in to comment.