Skip to content

Commit b3c48b6

Browse files
authored
fixed thread run (#1227)
* fixed thread run * update models
1 parent 37b29b4 commit b3c48b6

File tree

5 files changed

+206
-119
lines changed

5 files changed

+206
-119
lines changed

src/main/java/log4j2.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
1212
#appender.console.filter.threshold.level = DEBUG
1313

1414
# Root Logger
15-
rootLogger.level = OFF
15+
rootLogger.level = DEBUG
1616
rootLogger.appenderRef.stdout.ref = STDOUT

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ && getOutletPressure() == getOutletStream().getPressure()) {
160160
* @param percentValveOpening Percentage valve opening (0 to 100).
161161
* @return Adjusted flow coefficient (Cv) in US gallons per minute (USG/min).
162162
*/
163-
private static double adjustCv(double Cv, double percentValveOpening) {
163+
private double adjustCv(double Cv, double percentValveOpening) {
164164
return Cv * (percentValveOpening / 100);
165165
}
166166

@@ -176,7 +176,7 @@ private static double adjustCv(double Cv, double percentValveOpening) {
176176
* @param percentValveOpening Percentage valve opening (0 to 100).
177177
* @return Mass flow rate in kilograms per hour (kg/h).
178178
*/
179-
public static double liquidValveMassFlow(double P1, double P2, double rho, double Cv, double Fp,
179+
public double liquidValveMassFlow(double P1, double P2, double rho, double Cv, double Fp,
180180
double percentValveOpening) {
181181
// Equation unit conversion constant
182182
final double N1 = 0.0865;
@@ -211,8 +211,8 @@ public static double liquidValveMassFlow(double P1, double P2, double rho, doubl
211211
* @param Fp The piping geometry factor (dimensionless).
212212
* @return The percent valve opening.
213213
*/
214-
public static double calcPercentValveOpeningLiquid(double massFlowRate, double P1, double P2,
215-
double rho, double Cv, double Fp) {
214+
public double calcPercentValveOpeningLiquid(double massFlowRate, double P1, double P2, double rho,
215+
double Cv, double Fp) {
216216
// Equation unit conversion constant
217217
final double N1 = 0.0865;
218218

@@ -244,7 +244,7 @@ public static double calcPercentValveOpeningLiquid(double massFlowRate, double P
244244
* @param percentValveOpening Percentage valve opening (0 to 100).
245245
* @return Downstream pressure in bar.
246246
*/
247-
public static double liquidValvePout(double P1, double m, double rho, double Cv, double Fp,
247+
public double liquidValvePout(double P1, double m, double rho, double Cv, double Fp,
248248
double percentValveOpening) {
249249
// Equation unit conversion constant
250250
final double N1 = 0.0865;
@@ -281,7 +281,7 @@ public static double liquidValvePout(double P1, double m, double rho, double Cv,
281281
* @param percentValveOpening Percentage valve opening (0 to 100).
282282
* @return Flow coefficient (Cv) in US gallons per minute (USG/min).
283283
*/
284-
public static double liquidValveCv(double P1, double P2, double rho, double m, double Fp,
284+
public double liquidValveCv(double P1, double P2, double rho, double m, double Fp,
285285
double percentValveOpening) {
286286
// Equation unit conversion constant
287287
final double N1 = 0.0865;

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

+50-39
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,32 @@
1212
*
1313
* Manages a collection of processes that can be run in steps or continuously.
1414
*
15-
* @author Even Solbraa
16-
* @version $Id: $Id
15+
*
1716
*/
1817
public class ProcessModel implements Runnable {
18+
1919
static Logger logger = LogManager.getLogger(ProcessModel.class);
2020
private Map<String, ProcessSystem> processes = new LinkedHashMap<>();
2121

2222
private boolean runStep = false;
2323
private int maxIterations = 50;
24-
private int iterations = 0;
2524

2625
/**
2726
* Checks if the model is running in step mode.
28-
*
29-
* @return true if running in step mode, false otherwise.
3027
*/
3128
public boolean isRunStep() {
3229
return runStep;
3330
}
3431

3532
/**
3633
* Sets the step mode for the process.
37-
*
38-
* @param runStep true to enable step mode, false to disable.
3934
*/
4035
public void setRunStep(boolean runStep) {
4136
this.runStep = runStep;
4237
}
4338

4439
/**
4540
* Adds a process to the model.
46-
*
47-
* @param name the name of the process.
48-
* @param process the process to add.
49-
* @return true if the process was added successfully.
5041
*/
5142
public boolean add(String name, ProcessSystem process) {
5243
if (name == null || name.isEmpty()) {
@@ -65,55 +56,72 @@ public boolean add(String name, ProcessSystem process) {
6556

6657
/**
6758
* Retrieves a process by its name.
68-
*
69-
* @param name the name of the process.
70-
* @return the corresponding process, or null if not found.
7159
*/
7260
public ProcessSystem get(String name) {
7361
return processes.get(name);
7462
}
7563

7664
/**
7765
* Removes a process by its name.
78-
*
79-
* @param name the name of the process to remove.
80-
* @return true if the process was removed, false otherwise.
8166
*/
8267
public boolean remove(String name) {
8368
return processes.remove(name) != null;
8469
}
8570

8671
/**
87-
* Executes all processes, either continuously or in steps based on mode.
72+
* The core run method.
73+
*
74+
* - If runStep == true, each process is run in "step" mode exactly once. - Otherwise (continuous
75+
* mode), it loops up to maxIterations or until all processes are finished (isFinished() == true).
8876
*/
8977
@Override
9078
public void run() {
91-
for (ProcessSystem process : processes.values()) {
92-
try {
93-
if (runStep) {
79+
if (runStep) {
80+
// Step mode: just run each process once in step mode
81+
for (ProcessSystem process : processes.values()) {
82+
try {
83+
if (Thread.currentThread().isInterrupted()) {
84+
logger.debug("Thread was interrupted, exiting run()...");
85+
return;
86+
}
9487
process.run_step();
95-
} else {
96-
process.run();
88+
} catch (Exception e) {
89+
System.err.println("Error running process step: " + e.getMessage());
90+
e.printStackTrace();
9791
}
98-
} catch (Exception e) {
99-
System.err.println("Error running process: " + e.getMessage());
100-
e.printStackTrace();
10192
}
102-
}
103-
if (!runStep) {
104-
if (!isFinished() && iterations < maxIterations) {
105-
iterations += 1;
106-
run();
107-
} else {
108-
iterations = 0;
93+
} else {
94+
int iterations = 0;
95+
while (!Thread.currentThread().isInterrupted() && !isFinished()
96+
&& iterations < maxIterations) {
97+
for (ProcessSystem process : processes.values()) {
98+
if (Thread.currentThread().isInterrupted()) {
99+
logger.debug("Thread was interrupted, exiting run()...");
100+
return;
101+
}
102+
try {
103+
process.run(); // the process's continuous run
104+
} catch (Exception e) {
105+
System.err.println("Error running process: " + e.getMessage());
106+
e.printStackTrace();
107+
}
108+
}
109+
iterations++;
109110
}
110111
}
111112
}
112113

114+
/**
115+
* Starts this model in a new thread and returns that thread.
116+
*/
117+
public Thread runAsThread() {
118+
Thread processThread = new Thread(this);
119+
processThread.start();
120+
return processThread;
121+
}
122+
113123
/**
114124
* Checks if all processes are finished.
115-
*
116-
* @return true if all processes are solved, false otherwise.
117125
*/
118126
public boolean isFinished() {
119127
for (ProcessSystem process : processes.values()) {
@@ -125,11 +133,15 @@ public boolean isFinished() {
125133
}
126134

127135
/**
128-
* Executes all processes in a single step.
136+
* Runs all processes in a single step (used outside of the thread model).
129137
*/
130138
public void runStep() {
131139
for (ProcessSystem process : processes.values()) {
132140
try {
141+
if (Thread.currentThread().isInterrupted()) {
142+
logger.debug("Thread was interrupted, exiting run()...");
143+
return;
144+
}
133145
process.run_step();
134146
} catch (Exception e) {
135147
System.err.println("Error in runStep: " + e.getMessage());
@@ -139,20 +151,19 @@ public void runStep() {
139151
}
140152

141153
/**
142-
* Runs the model as a separate thread.
154+
* (Optional) Creates separate threads for each process (if you need them).
143155
*/
144156
public Map<String, Thread> getThreads() {
145157
Map<String, Thread> threads = new LinkedHashMap<>();
146158
try {
147159
for (ProcessSystem process : processes.values()) {
148160
Thread thread = new Thread(process);
161+
thread.setName(process.getName() + " thread");
149162
threads.put(process.getName(), thread);
150163
}
151-
152164
} catch (Exception ex) {
153165
logger.debug(ex.getMessage(), ex);
154166
}
155167
return threads;
156168
}
157-
158169
}

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -425,14 +425,20 @@ public void run(UUID id) {
425425
iter++;
426426
isConverged = true;
427427
for (int i = 0; i < unitOperations.size(); i++) {
428+
if (Thread.currentThread().isInterrupted()) {
429+
logger.debug("Process simulation was interrupted, exiting run()..." + getName());
430+
break;
431+
}
428432
if (!unitOperations.get(i).getClass().getSimpleName().equals("Recycle")) {
429433
try {
430434
if (iter == 1 || unitOperations.get(i).needRecalculation()) {
431435
unitOperations.get(i).run(id);
432436
}
433437
} catch (Exception ex) {
434438
// String error = ex.getMessage();
435-
logger.error(ex.getMessage(), ex);
439+
logger.error("error running unit uperation " + unitOperations.get(i).getName() + " "
440+
+ ex.getMessage(), ex);
441+
ex.printStackTrace();
436442
}
437443
}
438444
if (unitOperations.get(i).getClass().getSimpleName().equals("Recycle")
@@ -476,7 +482,8 @@ public void run(UUID id) {
476482
* signalDB[timeStepNumber][3 * i + 3] = ((MeasurementDeviceInterface)
477483
* measurementDevices.get(i)) .getUnit(); }
478484
*/
479-
} while (((!isConverged || (iter < 2 && hasRecycle)) && iter < 100) && !runStep);
485+
} while (((!isConverged || (iter < 2 && hasRecycle)) && iter < 100) && !runStep
486+
&& !Thread.currentThread().isInterrupted());
480487

481488
for (int i = 0; i < unitOperations.size(); i++) {
482489
unitOperations.get(i).setCalculationIdentifier(id);
@@ -490,7 +497,10 @@ public void run(UUID id) {
490497
public void run_step(UUID id) {
491498
for (int i = 0; i < unitOperations.size(); i++) {
492499
try {
493-
// if (unitOperations.get(i).needRecalculation()) {
500+
if (Thread.currentThread().isInterrupted()) {
501+
logger.debug("Process simulation was interrupted, exiting run()..." + getName());
502+
break;
503+
}
494504
unitOperations.get(i).run(id);
495505
// }
496506
} catch (Exception ex) {

0 commit comments

Comments
 (0)