Skip to content

Commit a227ea2

Browse files
authored
large process models update (#1215)
* update * update combined model * update * add components and tests (#1216) * add components and tests * update database parameters * update * Add new co2 (#1217) * add components and tests * update database parameters * update * update * update * update database test (#1218) * update * update acid * update * add more to combined process * update * update model * update * further updates * update test * update * update * update * update * update * fixed hydrate test * fix * update * update * update * Test-large-combined-model2 (#1219) * try to solve error * dont run mixer calcs for one single input stream * test update * update * update * update
1 parent 9c6a5b2 commit a227ea2

File tree

16 files changed

+1345
-51
lines changed

16 files changed

+1345
-51
lines changed

src/main/java/neqsim/process/equipment/ProcessEquipmentBaseClass.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public abstract class ProcessEquipmentBaseClass extends SimulationBaseClass
3939
public HashMap<String, String> properties = new HashMap<String, String>();
4040
public EnergyStream energyStream = new EnergyStream();
4141
private boolean isSetEnergyStream = false;
42-
protected boolean isSolved = false;
42+
protected boolean isSolved = true;
4343

4444
/**
4545
* <p>

src/main/java/neqsim/process/equipment/mixer/Mixer.java

+22-18
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public void run(UUID id) {
207207
mixedStream.setThermoSystem(thermoSystem2);
208208
// thermoSystem2.display();
209209
ThermodynamicOperations testOps = new ThermodynamicOperations(thermoSystem2);
210-
if (streams.size() > 0) {
210+
if (streams.size() >= 2) {
211211
mixedStream.getThermoSystem().setNumberOfPhases(2);
212212
mixedStream.getThermoSystem().init(0);
213213

@@ -221,33 +221,37 @@ public void run(UUID id) {
221221
mixedStream.getThermoSystem().setTemperature(guessTemperature());
222222
}
223223
// System.out.println("filan temp " + mixedStream.getTemperature());
224-
}
225-
if (isSetOutTemperature) {
226-
if (!Double.isNaN(getOutTemperature())) {
227-
mixedStream.getThermoSystem().setTemperature(getOutTemperature());
228-
}
229-
testOps.TPflash();
230-
mixedStream.getThermoSystem().init(2);
231-
} else {
232-
try {
233-
testOps.PHflash(enthalpy, 0);
234-
} catch (Exception ex) {
235-
logger.error(ex.getMessage(), ex);
224+
225+
if (isSetOutTemperature) {
236226
if (!Double.isNaN(getOutTemperature())) {
237227
mixedStream.getThermoSystem().setTemperature(getOutTemperature());
238228
}
239229
testOps.TPflash();
230+
mixedStream.getThermoSystem().init(2);
231+
} else {
232+
try {
233+
testOps.PHflash(enthalpy, 0);
234+
} catch (Exception ex) {
235+
logger.error(ex.getMessage(), ex);
236+
if (!Double.isNaN(getOutTemperature())) {
237+
mixedStream.getThermoSystem().setTemperature(getOutTemperature());
238+
}
239+
testOps.TPflash();
240+
}
240241
}
242+
} else {
243+
testOps.TPflash();
244+
mixedStream.getThermoSystem().init(2);
241245
}
242-
mixedStream.setCalculationIdentifier(id);
246+
243247

244248
// System.out.println("enthalpy: " +
245-
// mixedStream.getThermoSystem().getEnthalpy());
246-
// System.out.println("enthalpy: " + enthalpy);
249+
// mixedStream.getThermoSystem().getEnthalpy())
250+
// System.out.println("enthalpy: " + en
247251
// System.out.println("temperature: " +
248-
// mixedStream.getThermoSystem().getTemperature());
249252

250-
// System.out.println("beta " + mixedStream.getThermoSystem().getBeta());
253+
254+
// System.out.println("beta " + mixedStream.getThermoSystem(
251255
// outStream.setThermoSystem(mixedStream.getThermoSystem());
252256
setCalculationIdentifier(id);
253257
}

src/main/java/neqsim/process/equipment/separator/Separator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class Separator extends ProcessEquipmentBaseClass implements SeparatorInt
8585
*/
8686
public Separator(String name) {
8787
super(name);
88-
setCalculateSteadyState(false);
88+
setCalculateSteadyState(true);
8989
}
9090

9191
/**

src/main/java/neqsim/process/equipment/splitter/Splitter.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ public Splitter(String name, StreamInterface inStream) {
6868
*
6969
* @param name a {@link java.lang.String} object
7070
* @param inletStream a {@link neqsim.process.equipment.stream.StreamInterface} object
71-
* @param i a int
71+
* @param number_of_splits an int
7272
*/
73-
public Splitter(String name, StreamInterface inletStream, int i) {
73+
public Splitter(String name, StreamInterface inletStream, int number_of_splits) {
7474
this(name);
75-
setSplitNumber(i);
75+
setSplitNumber(number_of_splits);
7676
this.setInletStream(inletStream);
7777
}
7878

7979
/** {@inheritDoc} */
8080
@Override
81-
public void setSplitNumber(int i) {
82-
splitNumber = i;
81+
public void setSplitNumber(int number_of_splits) {
82+
splitNumber = number_of_splits;
8383
splitFactor = new double[splitNumber];
8484
splitFactor[0] = 1.0;
8585
if (inletStream != null) {
@@ -192,10 +192,8 @@ public boolean needRecalculation() {
192192
&& Math.abs(inletStream.getFluid().getFlowRate("kg/hr") - lastFlowRate)
193193
/ inletStream.getFluid().getFlowRate("kg/hr") < 1e-6
194194
&& Arrays.equals(splitFactor, oldSplitFactor)) {
195-
isSolved = true;
196195
return false;
197196
} else {
198-
isSolved = false;
199197
return true;
200198
}
201199
}

src/main/java/neqsim/process/equipment/stream/Stream.java

-2
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,8 @@ public boolean needRecalculation() {
323323
&& Math.abs(getFluid().getFlowRate("kg/hr") - lastFlowRate)
324324
/ getFluid().getFlowRate("kg/hr") < 1e-6
325325
&& Arrays.equals(getFluid().getMolarComposition(), lastComposition)) {
326-
isSolved = true;
327326
return false;
328327
} else {
329-
isSolved = false;
330328
return true;
331329
}
332330
}

src/main/java/neqsim/process/equipment/tank/Tank.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class Tank extends ProcessEquipmentBaseClass {
5353
*/
5454
public Tank(String name) {
5555
super(name);
56-
setCalculateSteadyState(false);
56+
setCalculateSteadyState(true);
5757
}
5858

5959
/**

src/main/java/neqsim/process/equipment/util/Recycle.java

+5
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ public void run(UUID id) {
321321
if (numberOfInputStreams == 1 && thermoSystem2.getFlowRate("kg/hr") < 1e-100) {
322322
setErrorCompositon(0.0);
323323
setErrorFlow(0.0);
324+
setErrorTemperature(0.0);
325+
setErrorPressure(0.0);
324326
return;
325327
}
326328
mixedStream.setThermoSystem(thermoSystem2);
@@ -598,6 +600,9 @@ public void setPriority(int priority) {
598600
/** {@inheritDoc} */
599601
@Override
600602
public boolean solved() {
603+
if (getOutletStream().getFlowRate("kg/hr") < 1e-20 && iterations > 1)
604+
return true;
605+
601606
if (Math.abs(this.errorComposition) < compositionTolerance
602607
&& Math.abs(this.errorFlow) < flowTolerance
603608
&& Math.abs(this.errorTemperature) < temperatureTolerance

src/main/java/neqsim/process/equipment/util/RecycleController.java

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.ArrayList;
44
import java.util.Objects;
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
57

68
/**
79
* <p>
@@ -12,6 +14,7 @@
1214
* @version $Id: $Id
1315
*/
1416
public class RecycleController implements java.io.Serializable {
17+
static Logger logger = LogManager.getLogger(RecycleController.class);
1518
private static final long serialVersionUID = 1000;
1619

1720
ArrayList<Recycle> recycleArray = new ArrayList<Recycle>();
@@ -165,6 +168,7 @@ public boolean hasHigherPriorityLevel() {
165168
*/
166169
public boolean solvedAll() {
167170
for (Recycle recyc : recycleArray) {
171+
logger.info(recyc.getName() + " solved " + recyc.solved());
168172
if (!recyc.solved()) {
169173
return false;
170174
}

src/main/java/neqsim/process/equipment/valve/ThrottlingValve.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class ThrottlingValve extends TwoPortEquipment implements ValveInterface
5454
*/
5555
public ThrottlingValve(String name) {
5656
super(name);
57-
setCalculateSteadyState(false);
57+
setCalculateSteadyState(true);
5858
}
5959

6060
/**

src/main/java/neqsim/process/processmodel/ProcessModel.java

+42-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.LinkedHashMap;
44
import java.util.Map;
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
57

68
/**
79
* <p>
@@ -14,8 +16,12 @@
1416
* @version $Id: $Id
1517
*/
1618
public class ProcessModel implements Runnable {
17-
private final Map<String, ProcessSystem> processes = new LinkedHashMap<>();
19+
static Logger logger = LogManager.getLogger(ProcessModel.class);
20+
private Map<String, ProcessSystem> processes = new LinkedHashMap<>();
21+
1822
private boolean runStep = false;
23+
private int maxIterations = 50;
24+
private int iterations = 0;
1925

2026
/**
2127
* Checks if the model is running in step mode.
@@ -52,6 +58,7 @@ public boolean add(String name, ProcessSystem process) {
5258
if (processes.containsKey(name)) {
5359
throw new IllegalArgumentException("A process with the given name already exists");
5460
}
61+
process.setName(name);
5562
processes.put(name, process);
5663
return true;
5764
}
@@ -93,6 +100,28 @@ public void run() {
93100
e.printStackTrace();
94101
}
95102
}
103+
if (!runStep) {
104+
if (!isFinished() && iterations < maxIterations) {
105+
iterations += 1;
106+
run();
107+
} else {
108+
iterations = 0;
109+
}
110+
}
111+
}
112+
113+
/**
114+
* Checks if all processes are finished.
115+
*
116+
* @return true if all processes are solved, false otherwise.
117+
*/
118+
public boolean isFinished() {
119+
for (ProcessSystem process : processes.values()) {
120+
if (!process.solved()) {
121+
return false;
122+
}
123+
}
124+
return true;
96125
}
97126

98127
/**
@@ -111,13 +140,19 @@ public void runStep() {
111140

112141
/**
113142
* Runs the model as a separate thread.
114-
*
115-
* @return the thread running the model.
116143
*/
117-
public Thread runAsThread() {
118-
Thread processThread = new Thread(this);
119-
processThread.start();
120-
return processThread;
144+
public Map<String, Thread> getThreads() {
145+
Map<String, Thread> threads = new LinkedHashMap<>();
146+
try {
147+
for (ProcessSystem process : processes.values()) {
148+
Thread thread = new Thread(process);
149+
threads.put(process.getName(), thread);
150+
}
151+
152+
} catch (Exception ex) {
153+
logger.debug(ex.getMessage(), ex);
154+
}
155+
return threads;
121156
}
122157

123158
}

src/main/java/neqsim/process/processmodel/ProcessSystem.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,8 @@ public void run_step(UUID id) {
495495
// }
496496
} catch (Exception ex) {
497497
// String error = ex.getMessage();
498-
logger.error(ex.getMessage(), ex);
498+
logger.error(
499+
"equipment: " + unitOperations.get(i).getName() + " errror: " + ex.getMessage(), ex);
499500
}
500501
}
501502
for (int i = 0; i < unitOperations.size(); i++) {
@@ -556,6 +557,18 @@ public void runTransient(double dt, UUID id) {
556557
/** {@inheritDoc} */
557558
@Override
558559
public boolean solved() {
560+
/* */
561+
if (recycleController.solvedAll()) {
562+
for (int i = 0; i < unitOperations.size(); i++) {
563+
logger.info("unit " + unitOperations.get(i).getName() + " solved: "
564+
+ unitOperations.get(i).solved());
565+
if (!unitOperations.get(i).solved()) {
566+
return false;
567+
}
568+
}
569+
} else {
570+
return false;
571+
}
559572
return true;
560573
}
561574

src/main/java/neqsim/thermodynamicoperations/flashops/RachfordRice.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @author Even Solbraa
2020
*/
2121
public class RachfordRice implements Serializable {
22-
static Logger logger = LogManager.getLogger(PHsolidFlash.class);
22+
static Logger logger = LogManager.getLogger(RachfordRice.class);
2323
private static final long serialVersionUID = 1000;
2424
private double[] beta = new double[2];
2525
private static String method = "Michelsen2001"; // alternative use Nielsen2023 or Michelsen2001
@@ -103,6 +103,8 @@ public double calcBetaMichelsen2001(double[] K, double[] z)
103103
g1 += -z[i] / K[i];
104104
}
105105

106+
// logger.debug("Max beta " + maxBeta + " min beta " + minBeta);
107+
106108
if (g0 < 0) {
107109
return tolerance;
108110
}
@@ -186,7 +188,8 @@ public double calcBetaMichelsen2001(double[] K, double[] z)
186188
nybeta = 1.0 - betal;
187189
}
188190
step = gbeta / deriv;
189-
} while (Math.abs(step) >= 1.5e-10 && iterations < maxIterations);
191+
} while (Math.abs(step) >= 1.0e-11 && (Math.abs(step) >= 1e-9 && iterations < 50)
192+
&& iterations < maxIterations);
190193
if (nybeta <= tolerance) {
191194
nybeta = tolerance;
192195
} else if (nybeta >= 1.0 - tolerance) {
@@ -196,6 +199,8 @@ public double calcBetaMichelsen2001(double[] K, double[] z)
196199
beta[1] = 1.0 - nybeta;
197200

198201
if (iterations >= maxIterations) {
202+
logger.debug("error " + beta[1]);
203+
logger.debug("gbeta " + gbeta);
199204
logger.debug("K " + Arrays.toString(K));
200205
logger.debug("z " + Arrays.toString(z));
201206
throw new neqsim.util.exception.TooManyIterationsException(new RachfordRice(),
@@ -333,6 +338,10 @@ public double calcBetaNielsen2023(double[] K, double[] z)
333338
beta[1] = 1.0 - V;
334339

335340
if (iter >= maxIterations) {
341+
logger.error("Rachford rice did not coverge afer " + maxIterations + " iterations");
342+
logger.debug("K " + Arrays.toString(K));
343+
logger.debug("z " + Arrays.toString(z));
344+
336345
throw new neqsim.util.exception.TooManyIterationsException(new RachfordRice(),
337346
"calcBetaNielsen2023", maxIterations);
338347
}

src/test/java/neqsim/process/equipment/separator/SeparatorTest.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package neqsim.process.equipment.separator;
22

3-
import static org.junit.jupiter.api.DynamicTest.stream;
43
import org.junit.jupiter.api.Assertions;
54
import org.junit.jupiter.api.BeforeEach;
65
import org.junit.jupiter.api.Test;
7-
import neqsim.process.equipment.separator.Separator;
86
import neqsim.process.equipment.stream.Stream;
97
import neqsim.process.equipment.stream.StreamInterface;
108
import neqsim.process.measurementdevice.LevelTransmitter;
@@ -100,8 +98,13 @@ public void testSimpleSeparator() {
10098
processOps.add(separator1);
10199
processOps.run();
102100

103-
Assertions.assertEquals(0.1598175271755,
104-
separator1.getFluid().getPhase(PhaseType.OIL).getBeta(), 1e-5);
101+
102+
Assertions.assertEquals(0.06976026260, feedStream.getFluid().getPhase(PhaseType.OIL).getBeta(),
103+
1e-5);
104+
105+
Assertions.assertEquals(0.06976026260, separator1.getFluid().getPhase(PhaseType.OIL).getBeta(),
106+
1e-5);
107+
105108

106109
}
107110
}

0 commit comments

Comments
 (0)