Skip to content

Commit 27bf800

Browse files
authoredDec 16, 2024··
multi process model with test (#1212)
* first version of multi model with test * update * update * update * update
1 parent 93be1b3 commit 27bf800

File tree

4 files changed

+288
-3
lines changed

4 files changed

+288
-3
lines changed
 

‎.devcontainer/devcontainer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
"shengchen.vscode-checkstyle",
2626
"mechatroner.rainbow-csv",
2727
"redhat.vscode-xml",
28-
"vscjava.vscode-java-test"
28+
"vscjava.vscode-java-test",
29+
"GitHub.copilot",
30+
"GitHub.copilot-chat"
2931
],
3032
"settings": {
3133
"java.configuration.updateBuildConfiguration": "interactive",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package neqsim.process.processmodel;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* <p>
8+
* ProcessModel class.
9+
* </p>
10+
*
11+
* Manages a collection of processes that can be run in steps or continuously.
12+
*
13+
* @author Even Solbraa
14+
* @version $Id: $Id
15+
*/
16+
public class ProcessModel implements Runnable {
17+
private final Map<String, ProcessSystem> processes = new LinkedHashMap<>();
18+
private boolean runStep = false;
19+
20+
/**
21+
* Checks if the model is running in step mode.
22+
*
23+
* @return true if running in step mode, false otherwise.
24+
*/
25+
public boolean isRunStep() {
26+
return runStep;
27+
}
28+
29+
/**
30+
* Sets the step mode for the process.
31+
*
32+
* @param runStep true to enable step mode, false to disable.
33+
*/
34+
public void setRunStep(boolean runStep) {
35+
this.runStep = runStep;
36+
}
37+
38+
/**
39+
* Adds a process to the model.
40+
*
41+
* @param name the name of the process.
42+
* @param process the process to add.
43+
* @return true if the process was added successfully.
44+
*/
45+
public boolean add(String name, ProcessSystem process) {
46+
if (name == null || name.isEmpty()) {
47+
throw new IllegalArgumentException("Name cannot be null or empty");
48+
}
49+
if (process == null) {
50+
throw new IllegalArgumentException("Process cannot be null");
51+
}
52+
if (processes.containsKey(name)) {
53+
throw new IllegalArgumentException("A process with the given name already exists");
54+
}
55+
processes.put(name, process);
56+
return true;
57+
}
58+
59+
/**
60+
* Retrieves a process by its name.
61+
*
62+
* @param name the name of the process.
63+
* @return the corresponding process, or null if not found.
64+
*/
65+
public ProcessSystem get(String name) {
66+
return processes.get(name);
67+
}
68+
69+
/**
70+
* Removes a process by its name.
71+
*
72+
* @param name the name of the process to remove.
73+
* @return true if the process was removed, false otherwise.
74+
*/
75+
public boolean remove(String name) {
76+
return processes.remove(name) != null;
77+
}
78+
79+
/**
80+
* Executes all processes, either continuously or in steps based on mode.
81+
*/
82+
@Override
83+
public void run() {
84+
for (ProcessSystem process : processes.values()) {
85+
try {
86+
if (runStep) {
87+
process.run_step();
88+
} else {
89+
process.run();
90+
}
91+
} catch (Exception e) {
92+
System.err.println("Error running process: " + e.getMessage());
93+
e.printStackTrace();
94+
}
95+
}
96+
}
97+
98+
/**
99+
* Executes all processes in a single step.
100+
*/
101+
public void runStep() {
102+
for (ProcessSystem process : processes.values()) {
103+
try {
104+
process.run_step();
105+
} catch (Exception e) {
106+
System.err.println("Error in runStep: " + e.getMessage());
107+
e.printStackTrace();
108+
}
109+
}
110+
}
111+
112+
/**
113+
* Runs the model as a separate thread.
114+
*
115+
* @return the thread running the model.
116+
*/
117+
public Thread runAsThread() {
118+
Thread processThread = new Thread(this);
119+
processThread.start();
120+
return processThread;
121+
}
122+
123+
}

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

+21-2
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,34 @@ public void add(ProcessEquipmentInterface[] operations) {
132132
getUnitOperations().addAll(Arrays.asList(operations));
133133
}
134134

135+
/**
136+
* <p>
137+
* Replace a unitoperation
138+
* </p>
139+
*
140+
* @param name Name of the object to replace
141+
* @param newObject the object to replace it with
142+
* @return a {@link java.lang.Boolean} object
143+
*/
144+
public boolean replaceUnit(String name, ProcessEquipmentInterface newObject) {
145+
try {
146+
ProcessEquipmentInterface unit = (ProcessEquipmentInterface) getUnit(name);
147+
unit = newObject;
148+
} catch (Exception e) {
149+
logger.error(e.getMessage(), e);
150+
}
151+
return true;
152+
}
153+
135154
/**
136155
* <p>
137156
* Get process equipmen by name.
138157
* </p>
139158
*
140159
* @param name Name of
141-
* @return a {@link java.lang.Object} object
160+
* @return a {@link neqsim.process.equipment.ProcessEquipmentInterface} object
142161
*/
143-
public Object getUnit(String name) {
162+
public ProcessEquipmentInterface getUnit(String name) {
144163
for (int i = 0; i < getUnitOperations().size(); i++) {
145164
if (getUnitOperations().get(i) instanceof ModuleInterface) {
146165
for (int j = 0; j < ((ModuleInterface) getUnitOperations().get(i)).getOperations()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package neqsim.process.processmodel;
2+
3+
import java.io.File;
4+
import org.apache.logging.log4j.LogManager;
5+
import org.apache.logging.log4j.Logger;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
import neqsim.process.equipment.compressor.Compressor;
9+
import neqsim.process.equipment.separator.ThreePhaseSeparator;
10+
import neqsim.process.equipment.stream.Stream;
11+
import neqsim.thermo.system.SystemInterface;
12+
13+
/**
14+
* CombinedModelsTest is a test class for validating the combined process model which includes an
15+
* inlet model and a compressor process.
16+
*
17+
* <p>
18+
* The class contains methods to set up individual process systems and combine them into a single
19+
* process model. It also includes a test method to verify the behavior of the combined process
20+
* model.
21+
*
22+
* <p>
23+
* Methods:
24+
* <ul>
25+
* <li>{@link #getinletModel()}: Sets up the inlet process model including a well stream and a
26+
* three-phase separator.</li>
27+
* <li>{@link #getCompressorProcess()}: Sets up the compressor process model including a gas feed
28+
* stream and a compressor.</li>
29+
* <li>{@link #getCombinedModel()}: Combines the inlet process model and the compressor process
30+
* model into a single process model.</li>
31+
* <li>{@link #testCombinedProcess()}: Tests the combined process model by configuring the
32+
* temperature and pressure for the well stream and the outlet pressure for the compressor, running
33+
* the process, and asserting the expected outlet temperature of the compressor.</li>
34+
* </ul>
35+
*
36+
* <p>
37+
* Dependencies:
38+
* <ul>
39+
* <li>Logger: Used for logging debug information.</li>
40+
* <li>ProcessSystem: Represents a process system containing various units.</li>
41+
* <li>Stream: Represents a stream in the process system.</li>
42+
* <li>ThreePhaseSeparator: Represents a three-phase separator unit in the process system.</li>
43+
* <li>Compressor: Represents a compressor unit in the process system.</li>
44+
* <li>ProcessModel: Represents the combined process model.</li>
45+
* <li>Assertions: Used for asserting expected values in the test method.</li>
46+
* </ul>
47+
*/
48+
public class CombinedModelsTest {
49+
static Logger logger = LogManager.getLogger(CombinedModelsTest.class);
50+
51+
public ProcessSystem getinletModel() {
52+
File file = new File("src/test/java/neqsim/process/processmodel");
53+
String fileFluid1 = file.getAbsolutePath() + "/feedfluid.e300";
54+
SystemInterface wellFluid = neqsim.thermo.util.readwrite.EclipseFluidReadWrite.read(fileFluid1);
55+
// wellFluid.setMultiPhaseCheck(true);
56+
57+
Stream wellStreamHP = new neqsim.process.equipment.stream.Stream("HP well stream", wellFluid);
58+
wellStreamHP.setFlowRate(10.0, "MSm3/day");
59+
60+
ThreePhaseSeparator firstStageSeparator =
61+
new neqsim.process.equipment.separator.ThreePhaseSeparator("1st stage separator",
62+
wellStreamHP);
63+
64+
ProcessSystem process1 = new ProcessSystem();
65+
process1.add(wellStreamHP);
66+
process1.add(firstStageSeparator);
67+
68+
return process1;
69+
};
70+
71+
public ProcessSystem getCompressorProcess() {
72+
73+
neqsim.process.equipment.stream.Stream gasFeedStream =
74+
new neqsim.process.equipment.stream.Stream("compressor feed stream");
75+
76+
neqsim.process.equipment.compressor.Compressor compressor1 =
77+
new neqsim.process.equipment.compressor.Compressor("Compressor1", gasFeedStream);
78+
compressor1.setPolytropicEfficiency(0.56);
79+
compressor1.setUsePolytropicCalc(true);
80+
81+
ProcessSystem process1 = new ProcessSystem();
82+
process1.add(gasFeedStream);
83+
process1.add(compressor1);
84+
85+
return process1;
86+
}
87+
88+
public ProcessModel getCombinedModel() {
89+
ProcessSystem inletProcess = getinletModel();
90+
ProcessSystem compressorProcess = getCompressorProcess();
91+
((Compressor) compressorProcess.getUnit("Compressor1")).setInletStream(
92+
((ThreePhaseSeparator) inletProcess.getUnit("1st stage separator")).getGasOutStream());
93+
94+
95+
ProcessModel combinedProcess = new ProcessModel();
96+
combinedProcess.add("feed process", inletProcess);
97+
combinedProcess.add("compressor process", compressorProcess);
98+
99+
return combinedProcess;
100+
}
101+
102+
/**
103+
* Test method for the combined process model.
104+
*
105+
* This test sets up a combined process model, configures the temperature and pressure for the "HP
106+
* well stream" in the "feed process", and sets the outlet pressure for "Compressor1" in the
107+
* "compressor process". The process is then run, and the test asserts that the outlet temperature
108+
* of "Compressor1" is as expected.
109+
*
110+
* The expected outlet temperature of "Compressor1" is 164.44139872 degrees Celsius with a
111+
* tolerance of 0.1 degrees.
112+
*/
113+
@Test
114+
public void testCombinedProcess() {
115+
ProcessModel fullProcess = getCombinedModel();
116+
fullProcess.setRunStep(true);
117+
118+
// Set fullProcess properties;
119+
((Stream) (fullProcess.get("feed process")).getUnit("HP well stream")).setTemperature(80.0,
120+
"C");
121+
((Stream) (fullProcess.get("feed process")).getUnit("HP well stream")).setPressure(50.0,
122+
"bara");
123+
124+
((Compressor) (fullProcess.get("compressor process")).getUnit("Compressor1"))
125+
.setOutletPressure(100.0, "bara");
126+
127+
128+
try {
129+
fullProcess.run();
130+
} catch (Exception ex) {
131+
logger.debug(ex.getMessage(), ex);
132+
}
133+
134+
Assertions.assertEquals(164.44139872,
135+
((Compressor) fullProcess.get("compressor process").getUnit("Compressor1"))
136+
.getOutletStream().getTemperature("C"),
137+
0.1);
138+
139+
}
140+
141+
}

0 commit comments

Comments
 (0)
Please sign in to comment.