Mixer Example 110On Calculate Method
Definition
Assembly: CapeOpen (in CapeOpen.dll) Version: 1.0.0.0 (1.0.0.0)
public override void OnCalculate()Public Overrides Sub OnCalculateRemarks
Example
An example of how to calculate a unit operation. This method obtains material objects from each of ports using the PortCollection class. The ICapeThermoMaterial interface is used to obtain the names using the CompIds() method, flows of each of the components present in the material object and overall pressure obtained using the GetOverallProp(String, String, Object) method. The enthaply of each phase in the inlet materials is calculated using CalcSinglePhaseProp(Object, String) method. The inlet enthalpy was multiplied by the phase flow (phase fraction * overall flow) which was summed to determine the outlet stream enthalpy. The output pressure from the lower of the two streams' pressure and pressure drop parameter. Lastly, the results of the calculations are applied to the output material object using the SetOverallProp(String, String, Object) method. The last method of the calculate method is to call the material's CalcEquilibrium(Object, Object, String) method.
A calculation report is generated by this method that is made avalable through the ProduceReport(String) method.
In this case, the inlet materials need to be released. This is accomplished using the ReleaseComObject(Object) method. Using this method to release the outlet material object would result in an null object reference error.
The method also documents use of the throwException(Exception) method to provide CAPE-OPEN compliant error handling.
public override void OnCalculate()
{
this.calcReport = String.Empty;
// Log a message using the simulation context (pop-up message commented out.
if (this.SimulationContext is CapeOpen.ICapeDiagnostic)
{
((CapeOpen.ICapeDiagnostic)this.SimulationContext).LogMessage("Starting Mixer Calculation");
}
this.calcReport = String.Concat(this.calcReport, "Starting Mixer Calculation", System.Environment.NewLine);
// Get the material Object from Port 0.
ICapeThermoMaterial in1 = null;
ICapeThermoMaterial in2 = null;
ICapeThermoMaterial outlet = null;
ICapeThermoMaterial temp = null;
Object comps = null;
Object forms = null;
Object names = null;
Object bTemp = null;
Object molWts = null;
Object casNos = null;
String[] compIds1 = null;
String[] compIds2 = null;
String[] compIds3 = null;
try
{
temp = (ICapeThermoMaterial)this.Ports[0].connectedObject;
in1 = (ICapeThermoMaterial)temp.CreateMaterial();
in1.CopyFromMaterial(temp);
if (temp.GetType().IsCOMObject) System.Runtime.InteropServices.Marshal.ReleaseComObject(temp);
((CapeOpen.ICapeThermoCompounds)in1).GetCompoundList(comps, forms, names, bTemp, molWts, casNos);
compIds1 = (String[])comps;
temp = (ICapeThermoMaterial)this.Ports[1].connectedObject;
in2 = (ICapeThermoMaterial)temp.CreateMaterial();
in2.CopyFromMaterial(temp);
if (temp.GetType().IsCOMObject) System.Runtime.InteropServices.Marshal.FinalReleaseComObject(temp);
((CapeOpen.ICapeThermoCompounds)in2).GetCompoundList(comps, forms, names, bTemp, molWts, casNos);
compIds2 = (String[])comps;
outlet = (ICapeThermoMaterial)this.Ports[2].connectedObject;
((CapeOpen.ICapeThermoCompounds)outlet).GetCompoundList(comps, forms, names, bTemp, molWts, casNos);
compIds3 = (String[])comps;
}
catch (System.Exception p_Ex)
{
CapeOpen.CapeInvalidOperationException ex = new CapeOpen.CapeInvalidOperationException("Material object does not support CAPE-OPEN Thermodynamics 1.1.", p_Ex);
this.calcReport = String.Concat(this.calcReport, "Material object does not support CAPE-OPEN Thermodynamics 1.1.", System.Environment.NewLine);
this.throwException(ex);
}
int l1 = compIds1.Length;
int l2 = compIds2.Length;
int l3 = compIds3.Length;
if (l1 != l2)
{
this.calcReport = String.Concat(this.calcReport, "Compounds in imlet materials do not match.", System.Environment.NewLine);
throw new CapeOpen.CapeInvalidOperationException("Compounds in imlet materials do not match.");
}
if (l1 != l3)
{
this.calcReport = String.Concat(this.calcReport, "Compounds in imlet materials does not match outlet material.", System.Environment.NewLine);
throw new CapeOpen.CapeInvalidOperationException("Compounds in imlet materials does not match outlet material.");
}
for (int i = 0; i < l3; i++)
{
if ((String.Compare(compIds1[i], compIds2[i])) != 0)
{
this.calcReport = String.Concat(this.calcReport, "Compounds in imlet materials do not match.", System.Environment.NewLine);
throw new CapeOpen.CapeInvalidOperationException("Compounds in imlet materials do not match.");
}
if (String.Compare(compIds1[i], compIds3[i]) != 0)
{
this.calcReport = String.Concat(this.calcReport, "Compounds in imlet materials does not match outlet material.", System.Environment.NewLine);
throw new CapeOpen.CapeInvalidOperationException("Compounds in imlet materials does not mAtch outlet material.");
}
}
double[] flow1 = null;
double[] flow2 = null;
double[] press1 = null;
double[] press2 = null;
double[] flow3 = new double[l3];
String[] props = { "enthalpy" };
String[] overall = { "Overall" };
double[] enthalpy = { 0 };
Object obj = null;
Object obj1 = null;
Object obj2 = null;
Object obj3 = null;
String[] phases = null;
CapeOpen.ICapeThermoEquilibriumRoutine eqRoutine;
try
{
in1.GetOverallProp("flow", "mole", obj);
flow1 = (double[])obj;
double totalFlow = 0;
foreach (double flow in flow1)
{
totalFlow = totalFlow + flow;
}
in1.GetOverallProp("pressure", "", obj);
press1 = (double[])obj;
CapeOpen.ICapeThermoPropertyRoutine p_Calc = (CapeOpen.ICapeThermoPropertyRoutine)in1;
CapeOpen.ICapeThermoPhases phaseList = (CapeOpen.ICapeThermoPhases)in1;
phaseList.GetPhaseList(obj1, obj2, obj3);
phases = (String[])obj1;
foreach (String phase in phases)
{
p_Calc.CalcSinglePhaseProp(props, phase);
in1.GetSinglePhaseProp("enthalpy", phase, "mole", obj);
double[] enth = (double[])obj;
in1.GetSinglePhaseProp("phaseFraction", phase, "mole", obj);
double[] fract = (double[])obj;
enthalpy[0] = enthalpy[0] + totalFlow * fract[0] * enth[0];
}
}
catch (System.Exception p_Ex)
{
CapeOpen.ECapeUser user = (CapeOpen.ECapeUser)in1;
this.calcReport = String.Concat(this.calcReport, user.description, System.Environment.NewLine);
this.throwException(p_Ex);
}
try
{
in2.GetOverallProp("flow", "mole", obj);
flow2 = (double[])obj;
double totalFlow = 0;
foreach (double flow in flow2)
{
totalFlow = totalFlow + flow;
}
in2.GetOverallProp("pressure", "", obj);
press2 = (double[])obj;
CapeOpen.ICapeThermoPropertyRoutine p_Calc = (CapeOpen.ICapeThermoPropertyRoutine)in2;
CapeOpen.ICapeThermoPhases phaseList = (CapeOpen.ICapeThermoPhases)in2;
phaseList.GetPhaseList(obj1, obj2, obj3);
phases = (String[])obj1;
foreach (String phase in phases)
{
p_Calc.CalcSinglePhaseProp(props, phase);
in2.GetSinglePhaseProp("enthalpy", phase, "mole", obj);
double[] enth = (double[])obj;
in2.GetSinglePhaseProp("phaseFraction", phase, "mole", obj);
double[] fract = (double[])obj;
enthalpy[0] = enthalpy[0] + totalFlow * fract[0] * enth[0];
}
}
catch (System.Exception p_Ex)
{
CapeOpen.ECapeUser user = (CapeOpen.ECapeUser)in2;
this.calcReport = String.Concat(this.calcReport, user.description, System.Environment.NewLine);
this.throwException(p_Ex);
}
for (int i = 0; i < l3; i++)
{
flow3[i] = flow1[i] + flow2[i];
}
try
{
outlet.SetOverallProp("flow", "mole", flow3);
double[] press = new double[1];
press[0] = press1[0];
if (press1[0] > press2[0]) press[0] = press2[0];
double pressdrop = ((CapeOpen.RealParameter)this.Parameters[0]).SIValue;
press[0] = press[0] - pressdrop;
outlet.SetOverallProp("pressure", "", press);
outlet.GetOverallProp("totalFlow", "mole", obj);
double[] totalFlow = (double[])obj;
enthalpy[0] = enthalpy[0] / totalFlow[0];
outlet.SetOverallProp("enthalpy", "mole", enthalpy);
this.calcReport = String.Concat(this.calcReport, "The outlet pressure is: ", press[0].ToString(), "Pa", System.Environment.NewLine);
int[] status = { 0, 0 };
outlet.SetPresentPhases(phases, status);
this.calcReport = String.Concat(this.calcReport, "The outlet ethalpy is: ", enthalpy[0].ToString(), "J/Mole", System.Environment.NewLine);
eqRoutine = (CapeOpen.ICapeThermoEquilibriumRoutine)outlet;
String[] spec1 = { "pressure", String.Empty, "Overall" };
String[] spec2 = { "enthalpy", String.Empty, "Overall" };
eqRoutine.CalcEquilibrium(spec1, spec2, "unspecified");
this.calcReport = String.Concat(this.calcReport, "Calculated pressure-enthalpy flash", System.Environment.NewLine);
}
catch (System.Exception p_Ex)
{
CapeOpen.ECapeUser user = (CapeOpen.ECapeUser)outlet;
this.calcReport = String.Concat(this.calcReport, user.description, System.Environment.NewLine);
this.throwException(p_Ex);
}
if (in1.GetType().IsCOMObject) System.Runtime.InteropServices.Marshal.ReleaseComObject(in1);
if (in2.GetType().IsCOMObject) System.Runtime.InteropServices.Marshal.ReleaseComObject(in2);
// Log the end of the calculation.
if (this.SimulationContext is CapeOpen.ICapeDiagnostic)
((CapeOpen.ICapeDiagnostic)this.SimulationContext).LogMessage("Ending Mixer Calculation");
this.calcReport = String.Concat(this.calcReport, "Ending Mixer Calculation");
((CapeOpen.ICapeDiagnostic)this.SimulationContext).PopUpMessage("Ending Mixer Calculation");
}