Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

large process models update #1215

Merged
merged 26 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class ProcessEquipmentBaseClass extends SimulationBaseClass
public HashMap<String, String> properties = new HashMap<String, String>();
public EnergyStream energyStream = new EnergyStream();
private boolean isSetEnergyStream = false;
protected boolean isSolved = false;
protected boolean isSolved = true;

/**
* <p>
Expand Down
40 changes: 22 additions & 18 deletions src/main/java/neqsim/process/equipment/mixer/Mixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void run(UUID id) {
mixedStream.setThermoSystem(thermoSystem2);
// thermoSystem2.display();
ThermodynamicOperations testOps = new ThermodynamicOperations(thermoSystem2);
if (streams.size() > 0) {
if (streams.size() >= 2) {
mixedStream.getThermoSystem().setNumberOfPhases(2);
mixedStream.getThermoSystem().init(0);

Expand All @@ -221,33 +221,37 @@ public void run(UUID id) {
mixedStream.getThermoSystem().setTemperature(guessTemperature());
}
// System.out.println("filan temp " + mixedStream.getTemperature());
}
if (isSetOutTemperature) {
if (!Double.isNaN(getOutTemperature())) {
mixedStream.getThermoSystem().setTemperature(getOutTemperature());
}
testOps.TPflash();
mixedStream.getThermoSystem().init(2);
} else {
try {
testOps.PHflash(enthalpy, 0);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);

if (isSetOutTemperature) {
if (!Double.isNaN(getOutTemperature())) {
mixedStream.getThermoSystem().setTemperature(getOutTemperature());
}
testOps.TPflash();
mixedStream.getThermoSystem().init(2);
} else {
try {
testOps.PHflash(enthalpy, 0);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
if (!Double.isNaN(getOutTemperature())) {
mixedStream.getThermoSystem().setTemperature(getOutTemperature());
}
testOps.TPflash();
}
}
} else {
testOps.TPflash();
mixedStream.getThermoSystem().init(2);
}
mixedStream.setCalculationIdentifier(id);


// System.out.println("enthalpy: " +
// mixedStream.getThermoSystem().getEnthalpy());
// System.out.println("enthalpy: " + enthalpy);
// mixedStream.getThermoSystem().getEnthalpy())
// System.out.println("enthalpy: " + en
// System.out.println("temperature: " +
// mixedStream.getThermoSystem().getTemperature());

// System.out.println("beta " + mixedStream.getThermoSystem().getBeta());

// System.out.println("beta " + mixedStream.getThermoSystem(
// outStream.setThermoSystem(mixedStream.getThermoSystem());
setCalculationIdentifier(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class Separator extends ProcessEquipmentBaseClass implements SeparatorInt
*/
public Separator(String name) {
super(name);
setCalculateSteadyState(false);
setCalculateSteadyState(true);
}

/**
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/neqsim/process/equipment/splitter/Splitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ public Splitter(String name, StreamInterface inStream) {
*
* @param name a {@link java.lang.String} object
* @param inletStream a {@link neqsim.process.equipment.stream.StreamInterface} object
* @param i a int
* @param number_of_splits an int
*/
public Splitter(String name, StreamInterface inletStream, int i) {
public Splitter(String name, StreamInterface inletStream, int number_of_splits) {
this(name);
setSplitNumber(i);
setSplitNumber(number_of_splits);
this.setInletStream(inletStream);
}

/** {@inheritDoc} */
@Override
public void setSplitNumber(int i) {
splitNumber = i;
public void setSplitNumber(int number_of_splits) {
splitNumber = number_of_splits;
splitFactor = new double[splitNumber];
splitFactor[0] = 1.0;
if (inletStream != null) {
Expand Down Expand Up @@ -192,10 +192,8 @@ public boolean needRecalculation() {
&& Math.abs(inletStream.getFluid().getFlowRate("kg/hr") - lastFlowRate)
/ inletStream.getFluid().getFlowRate("kg/hr") < 1e-6
&& Arrays.equals(splitFactor, oldSplitFactor)) {
isSolved = true;
return false;
} else {
isSolved = false;
return true;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/neqsim/process/equipment/stream/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,8 @@ public boolean needRecalculation() {
&& Math.abs(getFluid().getFlowRate("kg/hr") - lastFlowRate)
/ getFluid().getFlowRate("kg/hr") < 1e-6
&& Arrays.equals(getFluid().getMolarComposition(), lastComposition)) {
isSolved = true;
return false;
} else {
isSolved = false;
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/neqsim/process/equipment/tank/Tank.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class Tank extends ProcessEquipmentBaseClass {
*/
public Tank(String name) {
super(name);
setCalculateSteadyState(false);
setCalculateSteadyState(true);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/neqsim/process/equipment/util/Recycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ public void run(UUID id) {
if (numberOfInputStreams == 1 && thermoSystem2.getFlowRate("kg/hr") < 1e-100) {
setErrorCompositon(0.0);
setErrorFlow(0.0);
setErrorTemperature(0.0);
setErrorPressure(0.0);
return;
}
mixedStream.setThermoSystem(thermoSystem2);
Expand Down Expand Up @@ -598,6 +600,9 @@ public void setPriority(int priority) {
/** {@inheritDoc} */
@Override
public boolean solved() {
if (getOutletStream().getFlowRate("kg/hr") < 1e-20 && iterations > 1)
return true;

if (Math.abs(this.errorComposition) < compositionTolerance
&& Math.abs(this.errorFlow) < flowTolerance
&& Math.abs(this.errorTemperature) < temperatureTolerance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;
import java.util.Objects;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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

ArrayList<Recycle> recycleArray = new ArrayList<Recycle>();
Expand Down Expand Up @@ -165,6 +168,7 @@ public boolean hasHigherPriorityLevel() {
*/
public boolean solvedAll() {
for (Recycle recyc : recycleArray) {
logger.info(recyc.getName() + " solved " + recyc.solved());
if (!recyc.solved()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class ThrottlingValve extends TwoPortEquipment implements ValveInterface
*/
public ThrottlingValve(String name) {
super(name);
setCalculateSteadyState(false);
setCalculateSteadyState(true);
}

/**
Expand Down
49 changes: 42 additions & 7 deletions src/main/java/neqsim/process/processmodel/ProcessModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
* <p>
Expand All @@ -14,8 +16,12 @@
* @version $Id: $Id
*/
public class ProcessModel implements Runnable {
private final Map<String, ProcessSystem> processes = new LinkedHashMap<>();
static Logger logger = LogManager.getLogger(ProcessModel.class);
private Map<String, ProcessSystem> processes = new LinkedHashMap<>();

private boolean runStep = false;
private int maxIterations = 50;
private int iterations = 0;

/**
* Checks if the model is running in step mode.
Expand Down Expand Up @@ -52,6 +58,7 @@ public boolean add(String name, ProcessSystem process) {
if (processes.containsKey(name)) {
throw new IllegalArgumentException("A process with the given name already exists");
}
process.setName(name);
processes.put(name, process);
return true;
}
Expand Down Expand Up @@ -93,6 +100,28 @@ public void run() {
e.printStackTrace();
}
}
if (!runStep) {
if (!isFinished() && iterations < maxIterations) {
iterations += 1;
run();
} else {
iterations = 0;
}
}
}

/**
* Checks if all processes are finished.
*
* @return true if all processes are solved, false otherwise.
*/
public boolean isFinished() {
for (ProcessSystem process : processes.values()) {
if (!process.solved()) {
return false;
}
}
return true;
}

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

/**
* Runs the model as a separate thread.
*
* @return the thread running the model.
*/
public Thread runAsThread() {
Thread processThread = new Thread(this);
processThread.start();
return processThread;
public Map<String, Thread> getThreads() {
Map<String, Thread> threads = new LinkedHashMap<>();
try {
for (ProcessSystem process : processes.values()) {
Thread thread = new Thread(process);
threads.put(process.getName(), thread);
}

} catch (Exception ex) {
logger.debug(ex.getMessage(), ex);
}
return threads;
}

}
15 changes: 14 additions & 1 deletion src/main/java/neqsim/process/processmodel/ProcessSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ public void run_step(UUID id) {
// }
} catch (Exception ex) {
// String error = ex.getMessage();
logger.error(ex.getMessage(), ex);
logger.error(
"equipment: " + unitOperations.get(i).getName() + " errror: " + ex.getMessage(), ex);
}
}
for (int i = 0; i < unitOperations.size(); i++) {
Expand Down Expand Up @@ -556,6 +557,18 @@ public void runTransient(double dt, UUID id) {
/** {@inheritDoc} */
@Override
public boolean solved() {
/* */
if (recycleController.solvedAll()) {
for (int i = 0; i < unitOperations.size(); i++) {
logger.info("unit " + unitOperations.get(i).getName() + " solved: "
+ unitOperations.get(i).solved());
if (!unitOperations.get(i).solved()) {
return false;
}
}
} else {
return false;
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @author Even Solbraa
*/
public class RachfordRice implements Serializable {
static Logger logger = LogManager.getLogger(PHsolidFlash.class);
static Logger logger = LogManager.getLogger(RachfordRice.class);
private static final long serialVersionUID = 1000;
private double[] beta = new double[2];
private static String method = "Michelsen2001"; // alternative use Nielsen2023 or Michelsen2001
Expand Down Expand Up @@ -103,6 +103,8 @@ public double calcBetaMichelsen2001(double[] K, double[] z)
g1 += -z[i] / K[i];
}

// logger.debug("Max beta " + maxBeta + " min beta " + minBeta);

if (g0 < 0) {
return tolerance;
}
Expand Down Expand Up @@ -186,7 +188,8 @@ public double calcBetaMichelsen2001(double[] K, double[] z)
nybeta = 1.0 - betal;
}
step = gbeta / deriv;
} while (Math.abs(step) >= 1.5e-10 && iterations < maxIterations);
} while (Math.abs(step) >= 1.0e-11 && (Math.abs(step) >= 1e-9 && iterations < 50)
&& iterations < maxIterations);
if (nybeta <= tolerance) {
nybeta = tolerance;
} else if (nybeta >= 1.0 - tolerance) {
Expand All @@ -196,6 +199,8 @@ public double calcBetaMichelsen2001(double[] K, double[] z)
beta[1] = 1.0 - nybeta;

if (iterations >= maxIterations) {
logger.debug("error " + beta[1]);
logger.debug("gbeta " + gbeta);
logger.debug("K " + Arrays.toString(K));
logger.debug("z " + Arrays.toString(z));
throw new neqsim.util.exception.TooManyIterationsException(new RachfordRice(),
Expand Down Expand Up @@ -333,6 +338,10 @@ public double calcBetaNielsen2023(double[] K, double[] z)
beta[1] = 1.0 - V;

if (iter >= maxIterations) {
logger.error("Rachford rice did not coverge afer " + maxIterations + " iterations");
logger.debug("K " + Arrays.toString(K));
logger.debug("z " + Arrays.toString(z));

throw new neqsim.util.exception.TooManyIterationsException(new RachfordRice(),
"calcBetaNielsen2023", maxIterations);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package neqsim.process.equipment.separator;

import static org.junit.jupiter.api.DynamicTest.stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import neqsim.process.equipment.separator.Separator;
import neqsim.process.equipment.stream.Stream;
import neqsim.process.equipment.stream.StreamInterface;
import neqsim.process.measurementdevice.LevelTransmitter;
Expand Down Expand Up @@ -100,8 +98,13 @@ public void testSimpleSeparator() {
processOps.add(separator1);
processOps.run();

Assertions.assertEquals(0.1598175271755,
separator1.getFluid().getPhase(PhaseType.OIL).getBeta(), 1e-5);

Assertions.assertEquals(0.06976026260, feedStream.getFluid().getPhase(PhaseType.OIL).getBeta(),
1e-5);

Assertions.assertEquals(0.06976026260, separator1.getFluid().getPhase(PhaseType.OIL).getBeta(),
1e-5);


}
}
Loading
Loading