Skip to content

Commit

Permalink
Fix multifeed (#845)
Browse files Browse the repository at this point in the history
* fixed minor bug in stability analysis

* fix multifeed

* update

* update

* fixed bug

* update

* remove log
  • Loading branch information
EvenSol authored Nov 12, 2023
1 parent 14bf3f8 commit 40af2b5
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import neqsim.processSimulation.processEquipment.TwoPortEquipment;
import neqsim.processSimulation.processEquipment.heatExchanger.Heater;
import neqsim.processSimulation.processEquipment.mixer.Mixer;
import neqsim.processSimulation.processEquipment.separator.ThreePhaseSeparator;
import neqsim.processSimulation.processEquipment.stream.Stream;
import neqsim.processSimulation.processEquipment.stream.StreamInterface;
import neqsim.processSimulation.processSystem.ProcessSystem;
import neqsim.thermo.system.SystemInterface;
import neqsim.thermodynamicOperations.ThermodynamicOperations;
import neqsim.util.exception.InvalidInputException;

/**
* <p>
Expand All @@ -18,10 +24,9 @@
*/
public class FlowSetter extends TwoPortEquipment {
private static final long serialVersionUID = 1000;
static Logger logger = LogManager.getLogger(GORfitter.class);

double pressure = 1.01325;
double temperature = 15.0;
private static final Logger logger = LogManager.getLogger(FlowSetter.class);
double[] pressure = new double[] {1.01325};
double[] temperature = new double[] {15.0};
String unitT = "C";
String unitP = "bara";

Expand All @@ -32,6 +37,8 @@ public class FlowSetter extends TwoPortEquipment {
String unitOilFlowRate = "m3/hr";
String unitWaterFlowRate = "m3/hr";

ProcessSystem referenceProcess = null;

@Deprecated
/**
* <p>
Expand Down Expand Up @@ -83,54 +90,6 @@ public void setInletStream(StreamInterface inletStream) {
}
}

/**
* <p>
* Getter for the field <code>pressure</code>.
* </p>
*
* @return a double
*/
public double getPressure() {
return pressure;
}

/**
* <p>
* Setter for the field <code>pressure</code>.
* </p>
*
* @param pressure a double
* @param unitP a {@link java.lang.String} object
*/
public void setPressure(double pressure, String unitP) {
this.pressure = pressure;
this.unitP = unitP;
}

/**
* <p>
* getTemperature.
* </p>
*
* @return a double
*/
public double getTemperature() {
return temperature;
}

/**
* <p>
* Setter for the field <code>temperature</code>.
* </p>
*
* @param temperature a double
* @param unitT a {@link java.lang.String} object
*/
public void setTemperature(double temperature, String unitT) {
this.temperature = temperature;
this.unitT = unitT;
}

/**
* <p>
* Get setGasFlowRate
Expand Down Expand Up @@ -176,13 +135,13 @@ public double getGasFlowRate(String flowUnit) {
conversionFactor = 1.0;
break;
case "Sm3/hr":
conversionFactor = 1.0 / 3600.0;
conversionFactor = 1.0 * 3600.0;
break;
case "Sm3/day":
conversionFactor = 1.0 / 3600.0 / 24.0;
conversionFactor = 1.0 * 3600.0 * 24.0;
break;
case "MSm3/day":
conversionFactor = 1.0 / 3600.0 / 24.0 / 1e6;
conversionFactor = 1.0 * 3600.0 * 24.0 / 1e6;
break;
default:
throw new RuntimeException("unit not supported " + flowUnit);
Expand Down Expand Up @@ -232,10 +191,10 @@ public double getOilFlowRate(String flowUnit) {
conversionFactor = 1.0;
break;
case "m3/hr":
conversionFactor = 1.0 / 3600.0;
conversionFactor = 1.0 * 3600.0;
break;
case "m3/day":
conversionFactor = 1.0 / 3600.0 / 24.0;
conversionFactor = 1.0 * 3600.0 * 24.0;
break;
default:
throw new RuntimeException("unit not supported " + flowUnit);
Expand Down Expand Up @@ -285,10 +244,10 @@ public double getWaterFlowRate(String flowUnit) {
conversionFactor = 1.0;
break;
case "m3/hr":
conversionFactor = 1.0 / 3600.0;
conversionFactor = 1.0 * 3600.0;
break;
case "m3/day":
conversionFactor = 1.0 / 3600.0 / 24.0;
conversionFactor = 1.0 * 3600.0 * 24.0;
break;
default:
throw new RuntimeException("unit not supported " + flowUnit);
Expand All @@ -301,49 +260,148 @@ public double getWaterFlowRate(String flowUnit) {
public void run(UUID id) {
SystemInterface tempFluid = inStream.getThermoSystem().clone();

if (referenceProcess == null) {
referenceProcess = createReferenceProcess(inStream);
}

if (tempFluid.getFlowRate("kg/sec") < 1e-6) {
outStream.setThermoSystem(tempFluid);
return;
}

tempFluid.setTemperature(temperature, unitT);
tempFluid.setPressure(pressure, unitP);
double error = 0.0;
do {
error = 0.0;
// ((StreamInterface) referenceProcess.getUnit("feed stream")).setFluid(tempFluid);
Stream ins = new Stream(tempFluid);
ins.run();
referenceProcess = createReferenceProcess(ins);
referenceProcess.run();
((StreamInterface) referenceProcess.getUnit("gas")).getFluid()
.initPhysicalProperties("density");
((StreamInterface) referenceProcess.getUnit("oil")).getFluid()
.initPhysicalProperties("density");

ThermodynamicOperations thermoOps = new ThermodynamicOperations(tempFluid);
try {
thermoOps.TPflash();
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
double[] moleChange = new double[tempFluid.getNumberOfComponents()];
for (int i = 0; i < tempFluid.getNumberOfComponents(); i++) {
moleChange[i] = ((StreamInterface) referenceProcess.getUnit("gas")).getFluid()
.getComponent(i).getNumberOfMolesInPhase()
* (getGasFlowRate("Sm3/hr")
/ ((StreamInterface) referenceProcess.getUnit("gas")).getFlowRate("Sm3/hr"))
- ((StreamInterface) referenceProcess.getUnit("gas")).getFluid().getComponent(i)
.getNumberOfMolesInPhase()

tempFluid.initPhysicalProperties("density");
+ ((StreamInterface) referenceProcess.getUnit("oil")).getFluid().getComponent(i)
.getNumberOfMolesInPhase()
* (getOilFlowRate("m3/hr")
/ ((StreamInterface) referenceProcess.getUnit("oil")).getFlowRate("m3/hr"))
- ((StreamInterface) referenceProcess.getUnit("oil")).getFluid().getComponent(i)
.getNumberOfMolesInPhase();
error += Math.abs(moleChange[i]);
}
tempFluid.init(0);
for (int i = 0; i < tempFluid.getNumberOfComponents(); i++) {
tempFluid.addComponent(i, moleChange[i]);
}

} while (error > ((StreamInterface) referenceProcess.getUnit("feed stream")).getFluid()
.getTotalNumberOfMoles() / 1e6);

double[] moleChange = new double[tempFluid.getNumberOfComponents()];
for (int i = 0; i < tempFluid.getNumberOfComponents(); i++) {
moleChange[i] = tempFluid.getPhase("gas").getComponent(i).getx()
* (getGasFlowRate("Sm3/sec") / tempFluid.getPhase("gas").getMolarVolume("m3/mol"))
+ tempFluid.getPhase("oil").getComponent(i).getx()
* (getOilFlowRate("m3/sec") / tempFluid.getPhase("oil").getMolarVolume("m3/mol"))
- tempFluid.getComponent(i).getNumberOfmoles();
}
tempFluid.init(0);
for (int i = 0; i < tempFluid.getNumberOfComponents(); i++) {
tempFluid.addComponent(i, moleChange[i]);
}
if (waterFlowRate > 0) {
tempFluid.addComponent("water", waterFlowRate * 1000.0, "kg/sec");
tempFluid.addComponent("water", getWaterFlowRate("m3/hr") * 1000.0, "kg/hr");
}
tempFluid.setPressure((inStream.getThermoSystem()).getPressure());
tempFluid.setTemperature((inStream.getThermoSystem()).getTemperature());

ThermodynamicOperations thermoOps = new ThermodynamicOperations(tempFluid);
try {
thermoOps.TPflash();
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}

outStream.setThermoSystem(tempFluid);
outStream.run();
outStream.getFluid().initPhysicalProperties();

outStream.setCalculationIdentifier(id);
setCalculationIdentifier(id);
}

public ProcessSystem createReferenceProcess(StreamInterface feedStream) {

ProcessSystem referenceProcess = new ProcessSystem();

StreamInterface feedStream1 = new Stream("feed stream", feedStream.getFluid().clone());
feedStream1.setTemperature(temperature[0], unitT);
feedStream1.setPressure(pressure[0], unitP);
referenceProcess.add(feedStream1);

ThreePhaseSeparator separator1ststage =
new ThreePhaseSeparator("1st stage separator", feedStream1);
referenceProcess.add(separator1ststage);

Mixer gasMixer = new Mixer("gas mixer");
gasMixer.addStream(separator1ststage.getGasOutStream());

StreamInterface gasExport = new Stream("gas", gasMixer.getOutletStream());
StreamInterface oilExport = null;

if (temperature.length == 0 || temperature == null) {
throw new RuntimeException(
new InvalidInputException(this, "getReferenceProcess", "temperature", "can not be null"));
} else if (temperature.length == 1) {
oilExport = new Stream("oil", separator1ststage.getOilOutStream());
} else if (temperature.length == 2) {
Heater heater2ndstage = new Heater("2nd stage heater", separator1ststage.getOilOutStream());
heater2ndstage.setOutPressure(pressure[1]);
heater2ndstage.setOutTemperature(temperature[1]);
referenceProcess.add(heater2ndstage);

ThreePhaseSeparator separator2ndstage =
new ThreePhaseSeparator("2nd stage separator", heater2ndstage.getOutletStream());
referenceProcess.add(separator2ndstage);

gasMixer.addStream(separator2ndstage.getGasOutStream());
oilExport = new Stream("oil", separator2ndstage.getOilOutStream());
} else if (temperature.length == 3) {
Heater heater2ndstage = new Heater("2nd stage heater", separator1ststage.getOilOutStream());
heater2ndstage.setOutPressure(pressure[1]);
heater2ndstage.setOutTemperature(temperature[1]);
referenceProcess.add(heater2ndstage);

ThreePhaseSeparator separator2ndstage =
new ThreePhaseSeparator("2nd stage separator", heater2ndstage.getOutletStream());
referenceProcess.add(separator2ndstage);

Heater heater3rdstage = new Heater("3rd stage heater", separator2ndstage.getOilOutStream());
heater3rdstage.setOutPressure(pressure[2]);
heater3rdstage.setOutTemperature(temperature[2]);
referenceProcess.add(heater3rdstage);

ThreePhaseSeparator separator3rdstage =
new ThreePhaseSeparator("3rd stage separator", heater3rdstage.getOutletStream());
referenceProcess.add(separator3rdstage);

gasMixer.addStream(separator2ndstage.getGasOutStream());
gasMixer.addStream(separator3rdstage.getGasOutStream());
oilExport = new Stream("oil", separator3rdstage.getOilOutStream());
}

referenceProcess.add(gasMixer);
referenceProcess.add(gasExport);
referenceProcess.add(oilExport);

return referenceProcess;
}

public ProcessSystem getReferenceProcess() {
return referenceProcess;
}

public void setSeparationPT(double[] pressure, String unitP, double[] temperature, String unitT) {
this.pressure = pressure;
this.unitP = unitP;
this.temperature = temperature;
this.unitT = unitT;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ public void stabilityAnalysis() {
iter++;
err = 0;

if (iter <= 250 || !system.isImplementedCompositionDeriativesofFugacity()) {
if (iter % 7 == 0) {
if (iter <= 150 || !system.isImplementedCompositionDeriativesofFugacity()) {
if (iter % 7 == 0 && iter < 150) {
double vec1 = 0.0;

double vec2 = 0.0;
Expand All @@ -396,6 +396,9 @@ public void stabilityAnalysis() {
err += Math.abs((logWi[i] - oldlogw[i]) / oldlogw[i]);
Wi[j][i] = Math.exp(logWi[i]);
}
if (err > errOld) {
continue;
}
} else {
for (int i = 0; i < system.getPhase(0).getNumberOfComponents(); i++) {
oldoldoldlogw[i] = oldoldlogw[i];
Expand Down
Loading

0 comments on commit 40af2b5

Please sign in to comment.