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

Create-process-group #1256

Closed
wants to merge 4 commits into from
Closed
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 @@ -18,17 +18,6 @@ public class TwoPhaseSeparator extends Separator {
/** Serialization version UID. */
private static final long serialVersionUID = 1000;

SystemInterface thermoSystem;

SystemInterface gasSystem;
SystemInterface waterSystem;
SystemInterface liquidSystem;
SystemInterface thermoSystemCloned;

StreamInterface inletStream;
StreamInterface gasOutStream;
StreamInterface liquidOutStream;

/**
* Constructor for TwoPhaseSeparator.
*
Expand All @@ -43,68 +32,12 @@ public TwoPhaseSeparator(String name) {
* Constructor for TwoPhaseSeparator.
* </p>
*
* @param name a {@link java.lang.String} object
* @param inletStream a {@link neqsim.process.equipment.stream.StreamInterface} object
* @param name a {@link java.lang.String} object
* @param inletStream a {@link neqsim.process.equipment.stream.StreamInterface}
* object
*/
public TwoPhaseSeparator(String name, StreamInterface inletStream) {
super(name, inletStream);
}

/** {@inheritDoc} */
@Override
public void setInletStream(StreamInterface inletStream) {
this.inletStream = inletStream;

thermoSystem = inletStream.getThermoSystem().clone();
gasSystem = thermoSystem.phaseToSystem(thermoSystem.getPhases()[0]);
gasOutStream = new Stream("gasOutStream", gasSystem);

thermoSystem = inletStream.getThermoSystem().clone();
liquidSystem = thermoSystem.phaseToSystem(thermoSystem.getPhases()[1]);
liquidOutStream = new Stream("liquidOutStream", liquidSystem);
}

/** {@inheritDoc} */
@Override
public StreamInterface getLiquidOutStream() {
return liquidOutStream;
}

/** {@inheritDoc} */
@Override
public StreamInterface getGasOutStream() {
return gasOutStream;
}

/** {@inheritDoc} */
@Override
public StreamInterface getGas() {
return getGasOutStream();
}

/** {@inheritDoc} */
@Override
public StreamInterface getLiquid() {
return getLiquidOutStream();
}

/** {@inheritDoc} */
@Override
public void run(UUID id) {
thermoSystem = inletStream.getThermoSystem().clone();
gasSystem = thermoSystem.phaseToSystem(thermoSystem.getPhases()[0]);
gasSystem.setNumberOfPhases(1);
gasOutStream.setThermoSystem(gasSystem);

thermoSystem = inletStream.getThermoSystem().clone();
liquidSystem = thermoSystem.phaseToSystem(thermoSystem.getPhases()[1]);
liquidSystem.setNumberOfPhases(1);
liquidOutStream.setThermoSystem(liquidSystem);
setCalculationIdentifier(id);
}

/** {@inheritDoc} */
@Override
@ExcludeFromJacocoGeneratedReport
public void displayResult() {}
}
175 changes: 170 additions & 5 deletions src/main/java/neqsim/process/processmodel/ProcessModel.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
package neqsim.process.processmodel;

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

/**
* <p>
* ProcessModel class. Manages a collection of processes that can be run in steps or continuously.
* </p>
* A process model that can run multiple processes in parallel.
*
* This class is a simple model that can run multiple processes in parallel. It
* can run in two modes:
* - Step mode: each process is run once in a loop, in the order they were
* added. - Continuous mode:
* each process is run in a loop until all processes are finished or a maximum
* number of iterations
* is reached.
*
* You can also create groups of processes and run them separately.
*/
public class ProcessModel implements Runnable {
/** Logger object for class. */
static Logger logger = LogManager.getLogger(ProcessModel.class);

/** Map of process name -> ProcessSystem. */
private Map<String, ProcessSystem> processes = new LinkedHashMap<>();

/**
* Map of group name -> list of process names in that group.
*
* We store process *names* here, pointing to the actual ProcessSystem in
* `processes`.
* Alternatively, you can store the ProcessSystem references directly.
*/
private Map<String, List<String>> groups = new LinkedHashMap<>();

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

Expand Down Expand Up @@ -64,11 +85,112 @@ public boolean remove(String name) {
return processes.remove(name) != null;
}

/**
* Creates a new group or clears it if it already exists.
*/
public void createGroup(String groupName) {
if (groupName == null || groupName.isEmpty()) {
throw new IllegalArgumentException("Group name cannot be null or empty");
}
groups.put(groupName, new ArrayList<>());
}

/**
* Add a process to a given group (by process name).
*/
public void addProcessToGroup(String groupName, String processName) {
if (!processes.containsKey(processName)) {
throw new IllegalArgumentException("No process with name " + processName + " found");
}

groups.computeIfAbsent(groupName, key -> new ArrayList<>());
List<String> groupProcesses = groups.get(groupName);
if (!groupProcesses.contains(processName)) {
groupProcesses.add(processName);
}
}

/**
* Remove a process from a given group.
*/
public void removeProcessFromGroup(String groupName, String processName) {
if (groups.containsKey(groupName)) {
groups.get(groupName).remove(processName);
}
}

/**
* Runs all processes in the specified group (step or continuous).
*
* If the group name doesn't exist or is empty, does nothing.
*/
public void runGroup(String groupName) {
if (!groups.containsKey(groupName) || groups.get(groupName).isEmpty()) {
logger.debug("Group '{}' does not exist or is empty, nothing to run", groupName);
return;
}

List<String> groupProcesses = groups.get(groupName);
if (runStep) {
// Step mode: just run each process once in step mode
for (String processName : groupProcesses) {
try {
if (Thread.currentThread().isInterrupted()) {
logger.debug("Thread was interrupted, exiting runGroup()...");
return;
}
processes.get(processName).run_step();
} catch (Exception e) {
System.err.println("Error running process step: " + e.getMessage());
e.printStackTrace();
}
}
} else {
// Continuous mode
int iterations = 0;
while (!Thread.currentThread().isInterrupted() && !isGroupFinished(groupName)
&& iterations < maxIterations) {
for (String processName : groupProcesses) {
if (Thread.currentThread().isInterrupted()) {
logger.debug("Thread was interrupted, exiting runGroup()...");
return;
}
try {
processes.get(processName).run();
} catch (Exception e) {
System.err.println("Error running process: " + e.getMessage());
e.printStackTrace();
}
}
iterations++;
}
}
}

/**
* Check if the group has all processes finished.
*/
public boolean isGroupFinished(String groupName) {
if (!groups.containsKey(groupName)) {
// no group or group doesn't exist -> consider it "finished" or throw exception
return true;
}
for (String processName : groups.get(groupName)) {
ProcessSystem process = processes.get(processName);
if (process != null && !process.solved()) {
return false;
}
}
return true;
}

/**
* The core run method.
*
* - If runStep == true, each process is run in "step" mode exactly once. - Otherwise (continuous
* mode), it loops up to maxIterations or until all processes are finished (isFinished() == true).
* - If runStep == true, each process is run in "step" mode exactly once. -
* Otherwise (continuous
* mode), it loops up to maxIterations or until all processes are finished
* (isFinished() == true).
*/
@Override
public void run() {
Expand Down Expand Up @@ -162,4 +284,47 @@ public Map<String, Thread> getThreads() {
}
return threads;
}

/**
* Calculates the total power consumption of all processes in the specified
* unit.
*
* @param unit the unit of power to be used (e.g., "kW", "MW").
* @return the total power consumption of all processes in the specified unit.
*/
public double getPower(String unit) {
double totalPower = 0.0;
for (ProcessSystem process : processes.values()) {
totalPower += process.getPower(unit);
}
return totalPower;
}

/**
* Calculates the total heater duty for all processes in the specified unit.
*
* @param unit the unit for which the heater duty is to be calculated
* @return the total heater duty for the specified unit
*/
public double getHeaterDuty(String unit) {
double totalDuty = 0.0;
for (ProcessSystem process : processes.values()) {
totalDuty += process.getHeaterDuty(unit);
}
return totalDuty;
}

/**
* Calculates the total cooler duty for all processes in the specified unit.
*
* @param unit the unit for which the cooler duty is to be calculated
* @return the total cooler duty for the specified unit
*/
public double getCoolerDuty(String unit) {
double totalDuty = 0.0;
for (ProcessSystem process : processes.values()) {
totalDuty += process.getCoolerDuty(unit);
}
return totalDuty;
}
}
Loading
Loading