-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
SimulationObjectBaseClasses.vb
1832 lines (1432 loc) · 83.7 KB
/
SimulationObjectBaseClasses.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
' Flowsheet Object Base Classes
' Copyright 2008-2024 Daniel Wagner O. de Medeiros
'
' This file is part of DWSIM.
'
' DWSIM is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' DWSIM is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with DWSIM. If not, see <http://www.gnu.org/licenses/>.
Imports System.Runtime.InteropServices
Imports System.Text
Imports DWSIM.Interfaces.Enums.GraphicObjects
Imports DWSIM.Interfaces.Enums
Imports System.Dynamic
Imports System.Reflection
Imports cv = DWSIM.SharedClasses.SystemsOfUnits.Converter
Namespace UnitOperations
<System.Serializable()> <ComVisible(True)> Public MustInherit Class BaseClass
Implements ICloneable, IDisposable, Interfaces.ICustomXMLSerialization
Implements Interfaces.ISimulationObject, Interfaces.IProductInformation
Public Const ClassId As String = ""
<System.NonSerialized()> Protected Friend m_flowsheet As Interfaces.IFlowsheet
Public Shared InitializationAction1 As Action(Of BaseClass)
Public Shared InitializationAction2 As Action(Of BaseClass)
Public Shared InitializationAction3 As Action(Of BaseClass)
<Newtonsoft.Json.JsonIgnore> <Xml.Serialization.XmlIgnore> Public Property LastSolutionInputSnapshot As String = ""
Protected Friend _IsDirty As Boolean = True
ReadOnly Property IsDirty As Boolean Implements ISimulationObject.IsDirty
Get
Return _IsDirty
End Get
End Property
Protected Friend _CanUsePreviousResults As Boolean = False
ReadOnly Property CanUsePreviousResults As Boolean Implements ISimulationObject.CanUsePreviousResults
Get
Return _CanUsePreviousResults
End Get
End Property
Public Property DynamicsSpec As Enums.Dynamics.DynamicsSpecType = Dynamics.DynamicsSpecType.Pressure Implements ISimulationObject.DynamicsSpec
Public Property DynamicsOnly As Boolean = False Implements ISimulationObject.DynamicsOnly
Public Property ExtraProperties As New ExpandoObject Implements ISimulationObject.ExtraProperties
Public Property ExtraPropertiesUnitTypes As New ExpandoObject Implements ISimulationObject.ExtraPropertiesUnitTypes
Public Property ExtraPropertiesDescriptions As New ExpandoObject Implements ISimulationObject.ExtraPropertiesDescriptions
Public Property ExtraPropertiesTypes As New ExpandoObject Implements ISimulationObject.ExtraPropertiesTypes
Public Overridable Property Visible As Boolean = True
<NonSerialized()> <Xml.Serialization.XmlIgnore> Public LaunchExternalPropertyEditor() As Action(Of ISimulationObject)
<NonSerialized()> <Xml.Serialization.XmlIgnore> Public ExtraPropertiesEditor As Form
Public Property OverrideCalculationRoutine As Boolean = False
<System.NonSerialized()> <Xml.Serialization.XmlIgnore> Public CalculationRoutineOverride As Action
Public Property StoreDetailedDebugReport As Boolean = False
Public Property DetailedDebugReport As String
Public ReadOnly Property IsFunctional As Boolean = True Implements ISimulationObject.IsFunctional
Public Overridable Property ComponentDescription() As String = ""
Public Overridable Property ComponentName() As String = ""
Public Overrides Function ToString() As String
If GraphicObject IsNot Nothing Then
Return GraphicObject.Tag
Else
Return MyBase.ToString()
End If
End Function
''' <summary>
''' Gets the current flowsheet where this object is located.
''' </summary>
''' <value></value>
''' <returns>Flowsheet instance.</returns>
''' <remarks></remarks>
Public Overridable ReadOnly Property FlowSheet() As Interfaces.IFlowsheet
Get
If Not m_flowsheet Is Nothing Then
Return m_flowsheet
Else
Return Nothing
End If
End Get
End Property
Public Overridable Property ObjectClass As SimulationObjectClass = SimulationObjectClass.Other Implements ISimulationObject.ObjectClass
Public Overridable ReadOnly Property SupportsDynamicMode As Boolean = False Implements ISimulationObject.SupportsDynamicMode
Public Overridable ReadOnly Property HasPropertiesForDynamicMode As Boolean = False Implements ISimulationObject.HasPropertiesForDynamicMode
#Region " Constructors"
Public Sub New()
InitializationAction1?.Invoke(Me)
InitializationAction2?.Invoke(Me)
InitializationAction3?.Invoke(Me)
End Sub
Sub CreateNew()
If ExtraProperties.Count = 0 Then CreateDynamicProperties()
End Sub
#End Region
#Region " ISimulationObject"
Public Sub AddExtraProperty(pname As String, pvalue As Object) Implements ISimulationObject.AddExtraProperty
Dim col1 = DirectCast(ExtraProperties, IDictionary(Of String, Object))
If Not col1.ContainsKey(pname) Then
col1.Add(pname, pvalue)
Else
Throw New Exception("Property already exists.")
End If
End Sub
Public Sub RemoveExtraProperty(pname As String) Implements ISimulationObject.RemoveExtraProperty
Dim col1 = DirectCast(ExtraProperties, IDictionary(Of String, Object))
If col1.ContainsKey(pname) Then
col1.Remove(pname)
Else
Throw New Exception("Property doesn't exist.")
End If
End Sub
Public Sub SetExtraPropertyValue(pname As String, pvalue As Object) Implements ISimulationObject.SetExtraPropertyValue
Dim col1 = DirectCast(ExtraProperties, IDictionary(Of String, Object))
If col1.ContainsKey(pname) Then
col1(pname) = pvalue
Else
Throw New Exception("Property doesn't exist.")
End If
End Sub
Public Function GetExtraPropertyValue(pname As String) As Object Implements ISimulationObject.GetExtraPropertyValue
Dim col1 = DirectCast(ExtraProperties, IDictionary(Of String, Object))
If col1.ContainsKey(pname) Then
Return col1(pname)
Else
Throw New Exception("Property doesn't exist.")
End If
End Function
Public Sub ClearExtraProperties() Implements ISimulationObject.ClearExtraProperties
Dim col1 = DirectCast(ExtraProperties, IDictionary(Of String, Object))
Dim col2 = DirectCast(ExtraPropertiesDescriptions, IDictionary(Of String, Object))
Dim col3 = DirectCast(ExtraPropertiesUnitTypes, IDictionary(Of String, Object))
Dim col4 = DirectCast(ExtraPropertiesTypes, IDictionary(Of String, Object))
Dim toremove As New List(Of String)
For Each p In col1
If Not col2.ContainsKey(p.Key) And Not col3.ContainsKey(p.Key) Then
toremove.Add(p.Key)
End If
Next
For Each item In toremove
col1.Remove(item)
Next
End Sub
Public Sub AddDynamicProperty(pname As String, pdesc As String, pvalue As Double,
punittype As Enums.UnitOfMeasure, ptype As System.Type) Implements ISimulationObject.AddDynamicProperty
Dim col1 = DirectCast(ExtraProperties, IDictionary(Of String, Object))
Dim col2 = DirectCast(ExtraPropertiesDescriptions, IDictionary(Of String, Object))
Dim col3 = DirectCast(ExtraPropertiesUnitTypes, IDictionary(Of String, Object))
Dim col4 = DirectCast(ExtraPropertiesTypes, IDictionary(Of String, Object))
If Not col1.ContainsKey(pname) Then col1.Add(pname, pvalue)
If Not col2.ContainsKey(pname) Then col2.Add(pname, pdesc)
If Not col3.ContainsKey(pname) Then col3.Add(pname, punittype)
If Not col4.ContainsKey(pname) Then col4.Add(pname, ptype)
End Sub
Public Function SetDynamicProperty(id As String, value As Object) As Boolean Implements ISimulationObject.AddDynamicProperty
Dim col1 = DirectCast(ExtraProperties, IDictionary(Of String, Object))
col1(id) = value
Return True
End Function
Public Function GetDynamicProperty(id As String) As Object Implements ISimulationObject.GetDynamicProperty
Dim col1 = DirectCast(ExtraProperties, IDictionary(Of String, Object))
If col1.ContainsKey(id) Then Return col1(id) Else Return Nothing
End Function
Public Function GetDynamicPropertyUnitType(id As String) As Enums.UnitOfMeasure Implements ISimulationObject.GetDynamicPropertyUnitType
Dim col1 = DirectCast(ExtraPropertiesUnitTypes, IDictionary(Of String, Object))
If col1.ContainsKey(id) Then Return col1(id) Else Return Nothing
End Function
Public Sub RemoveDynamicProperty(pname As String) Implements ISimulationObject.RemoveDynamicProperty
Dim col1 = DirectCast(ExtraProperties, IDictionary(Of String, Object))
Dim col2 = DirectCast(ExtraPropertiesDescriptions, IDictionary(Of String, Object))
Dim col3 = DirectCast(ExtraPropertiesUnitTypes, IDictionary(Of String, Object))
Dim col4 = DirectCast(ExtraPropertiesTypes, IDictionary(Of String, Object))
If col1.ContainsKey(pname) Then col1.Remove(pname)
If col2.ContainsKey(pname) Then col2.Remove(pname)
If col1.ContainsKey(pname) Then col3.Remove(pname)
If col4.ContainsKey(pname) Then col4.Remove(pname)
End Sub
Public Function IsDynamicProperty(pname As String) As Boolean Implements ISimulationObject.IsDynamicProperty
Dim col1 = DirectCast(ExtraPropertiesDescriptions, IDictionary(Of String, Object))
Return col1.ContainsKey(pname)
End Function
Public Overridable Function GetChartModel(name As String) As Object Implements ISimulationObject.GetChartModel
If CreateChartAction IsNot Nothing Then
Return CreateChartAction.Invoke(name)
Else
Return Nothing
End If
End Function
Public Overridable Function GetPropertyDescription(prop As String) As String Implements ISimulationObject.GetPropertyDescription
Return "No description is available for this property."
End Function
Public Overridable Function GetReport(su As IUnitsOfMeasure, ci As Globalization.CultureInfo, numberformat As String) As String Implements ISimulationObject.GetReport
Return "No report is available for this object."
End Function
Public Overridable Function GetVersion() As Version Implements ISimulationObject.GetVersion
Return Me.GetType.Assembly.GetName.Version
End Function
Public MustOverride Function GetDisplayName() As String Implements ISimulationObject.GetDisplayName
Public MustOverride Function GetDisplayDescription() As String Implements ISimulationObject.GetDisplayDescription
Public MustOverride Function GetIconBitmap() As Object Implements ISimulationObject.GetIconBitmap
<NonSerialized> Private _AttachedUtilities As New List(Of IAttachedUtility)
Public Property AttachedUtilities As List(Of IAttachedUtility) Implements ISimulationObject.AttachedUtilities
Get
If _AttachedUtilities Is Nothing Then _AttachedUtilities = New List(Of IAttachedUtility)
Return _AttachedUtilities
End Get
Set(value As List(Of IAttachedUtility))
_AttachedUtilities = value
End Set
End Property
Public Property PreferredFlashAlgorithmTag As String = "" Implements ISimulationObject.PreferredFlashAlgorithmTag
Public Property Calculated As Boolean = False Implements Interfaces.ISimulationObject.Calculated
Public Property DebugMode As Boolean = False Implements Interfaces.ISimulationObject.DebugMode
<Xml.Serialization.XmlIgnore> Public Property DebugText As String = "" Implements Interfaces.ISimulationObject.DebugText
Public Property LastUpdated As New Date Implements Interfaces.ISimulationObject.LastUpdated
''' <summary>
''' Calculates the object.
''' </summary>
''' <param name="args"></param>
''' <remarks>Use 'Solve()' to calculate the object instead.</remarks>
Public Overridable Sub Calculate(Optional ByVal args As Object = Nothing) Implements ISimulationObject.Calculate
Throw New NotImplementedException
End Sub
Public Sub DeCalculate(Optional args As Object = Nothing) Implements ISimulationObject.DeCalculate
'Throw New NotImplementedException
End Sub
Public Overridable Sub RunDynamicModel() Implements ISimulationObject.RunDynamicModel
Throw New Exception("This Unit Operation is not yet supported in Dynamic Mode.")
End Sub
Public Overridable Sub PerformPostCalcValidation() Implements ISimulationObject.PerformPostCalcValidation
If GraphicObject.ObjectType <> ObjectType.MaterialStream And GraphicObject.ObjectType <> ObjectType.EnergyStream And
GraphicObject.ObjectType <> ObjectType.OT_Adjust And GraphicObject.ObjectType <> ObjectType.OT_Spec And
GraphicObject.ObjectType <> ObjectType.OT_Recycle And GraphicObject.ObjectType <> ObjectType.OT_EnergyRecycle Then
'calculate mass balance
Dim mb As Double = 0.0#
Dim eb As Double = 0.0#
Dim mbe As Double = 0.0#
Dim ebe As Double = 0.0#
Dim mbt As Double = 0.0#
Dim ebt As Double = 0.0#
Dim mi, hi, hf As Double
Dim imsc As List(Of ISimulationObject) = GraphicObject.InputConnectors.Where(Function(x) x.IsAttached And Not (x.IsEnergyConnector Or x.Type = ConType.ConEn)).Select(Function(x) FlowSheet.SimulationObjects(x.AttachedConnector.AttachedFrom.Name)).ToList
Dim omsc As List(Of ISimulationObject) = GraphicObject.OutputConnectors.Where(Function(x) x.IsAttached And Not (x.IsEnergyConnector Or x.Type = ConType.ConEn)).Select(Function(x) FlowSheet.SimulationObjects(x.AttachedConnector.AttachedTo.Name)).ToList
For Each ims In imsc
If ims.GraphicObject.Active Then
If DirectCast(ims, IMaterialStream).DefinedFlow = FlowSpec.Mole Then
mi = Convert.ToDouble(ims.GetPropertyValue("PROP_MS_3")) / 1000.0 * Convert.ToDouble(ims.GetPropertyValue("PROP_MS_6"))
Else
mi = Convert.ToDouble(ims.GetPropertyValue("PROP_MS_2"))
End If
mb += mi
mbt += mi
hi = Convert.ToDouble(ims.GetPropertyValue("PROP_MS_7"))
eb -= mi * hi 'kg/s * kJ/kg = kJ/s = kW
ebt += Math.Abs(mi * hi)
'heats of formation
hf = DirectCast(ims, IMaterialStream).GetOverallHeatOfFormation()
eb -= hf
ebt += Math.Abs(hf)
End If
Next
For Each oms In omsc
If oms.GraphicObject.Active Then
If DirectCast(oms, IMaterialStream).DefinedFlow = FlowSpec.Mole Then
mi = Convert.ToDouble(oms.GetPropertyValue("PROP_MS_3")) / 1000.0 * Convert.ToDouble(oms.GetPropertyValue("PROP_MS_6"))
Else
mi = Convert.ToDouble(oms.GetPropertyValue("PROP_MS_2"))
End If
mb -= mi
mbt += mi
hi = Convert.ToDouble(oms.GetPropertyValue("PROP_MS_7"))
eb += mi * hi 'kg/s * kJ/kg = kJ/s = kW
ebt += Math.Abs(mi * hi)
'heats of formation
hf = DirectCast(oms, IMaterialStream).GetOverallHeatOfFormation()
eb += hf
ebt += Math.Abs(hf)
End If
Next
mbe = mb / mbt
Dim iesc As List(Of ISimulationObject) = GraphicObject.InputConnectors.Where(Function(x) x.IsAttached And (x.IsEnergyConnector Or x.Type = ConType.ConEn)).Select(Function(x) FlowSheet.SimulationObjects(x.AttachedConnector.AttachedFrom.Name)).ToList
Dim oesc As List(Of ISimulationObject) = GraphicObject.OutputConnectors.Where(Function(x) x.IsAttached And (x.IsEnergyConnector Or x.Type = ConType.ConEn)).Select(Function(x) FlowSheet.SimulationObjects(x.AttachedConnector.AttachedTo.Name)).ToList
For Each ies In iesc
If ies.GraphicObject.Active Then
eb -= Convert.ToDouble(ies.GetPropertyValue("PROP_ES_0"))
ebt += Math.Abs(Convert.ToDouble(ies.GetPropertyValue("PROP_ES_0")))
End If
Next
For Each oes In oesc
If oes.GraphicObject.Active Then
eb += Convert.ToDouble(oes.GetPropertyValue("PROP_ES_0"))
ebt += Math.Abs(Convert.ToDouble(oes.GetPropertyValue("PROP_ES_0")))
End If
Next
If GraphicObject.EnergyConnector.IsAttached Then
Dim inobj = FlowSheet.SimulationObjects(GraphicObject.EnergyConnector.AttachedConnector.AttachedFrom.Name)
If inobj.GraphicObject.IsEnergyStream And inobj.GraphicObject.Active Then
eb -= Convert.ToDouble(inobj.GetPropertyValue("PROP_ES_0"))
ebt += Math.Abs(Convert.ToDouble(inobj.GetPropertyValue("PROP_ES_0")))
End If
Dim outobj = FlowSheet.SimulationObjects(GraphicObject.EnergyConnector.AttachedConnector.AttachedTo.Name)
If outobj.GraphicObject.IsEnergyStream And outobj.GraphicObject.Active Then
eb += Convert.ToDouble(outobj.GetPropertyValue("PROP_ES_0"))
ebt += Math.Abs(Convert.ToDouble(outobj.GetPropertyValue("PROP_ES_0")))
End If
End If
If iesc.Count + oesc.Count = 0 And Not GraphicObject.EnergyConnector.IsAttached Then
eb = 0
End If
mbe = Math.Abs(mb / mbt)
ebe = Math.Abs(eb / ebt)
If FlowSheet.FlowsheetOptions.MassBalanceRelativeTolerance = 0.0 Then
FlowSheet.FlowsheetOptions.MassBalanceRelativeTolerance = 0.01
End If
If FlowSheet.FlowsheetOptions.EnergyBalanceRelativeTolerance = 0.0 Then
FlowSheet.FlowsheetOptions.EnergyBalanceRelativeTolerance = 0.01
End If
If mbe > FlowSheet.FlowsheetOptions.MassBalanceRelativeTolerance Then
If FlowSheet.FlowsheetOptions.MassBalanceCheck = WarningType.RaiseError Then
Throw New Exception(GraphicObject.Tag + ": " + FlowSheet.GetTranslatedString("MassBalanceMessage") + " (" + mbe.ToString() + " > " + FlowSheet.FlowsheetOptions.MassBalanceRelativeTolerance.ToString + ")")
ElseIf FlowSheet.FlowsheetOptions.MassBalanceCheck = WarningType.ShowWarning Then
FlowSheet.ShowMessage(GraphicObject.Tag + ": " + FlowSheet.GetTranslatedString("MassBalanceMessage") + " (" + mbe.ToString() + " > " + FlowSheet.FlowsheetOptions.MassBalanceRelativeTolerance.ToString + ")", IFlowsheet.MessageType.Warning)
End If
End If
If ebe > FlowSheet.FlowsheetOptions.EnergyBalanceRelativeTolerance Then
If FlowSheet.FlowsheetOptions.EnergyBalanceCheck = WarningType.RaiseError Then
Throw New Exception(GraphicObject.Tag + ": " + FlowSheet.GetTranslatedString("EnergyBalanceMessage") + " (" + ebe.ToString() + " > " + FlowSheet.FlowsheetOptions.EnergyBalanceRelativeTolerance.ToString + ")")
ElseIf FlowSheet.FlowsheetOptions.EnergyBalanceCheck = WarningType.ShowWarning Then
FlowSheet.ShowMessage(GraphicObject.Tag + ": " + FlowSheet.GetTranslatedString("EnergyBalanceMessage") + " (" + ebe.ToString() + " > " + FlowSheet.FlowsheetOptions.EnergyBalanceRelativeTolerance.ToString + ")", IFlowsheet.MessageType.Warning)
End If
End If
End If
End Sub
Public Sub Solve() Implements ISimulationObject.Solve
If FlowSheet.FlowsheetOptions.ForceObjectSolving Then
Calculated = False
If OverrideCalculationRoutine Then
CalculationRoutineOverride.Invoke()
Else
Calculate()
End If
Calculated = True
Else
CheckDirtyStatus()
Calculated = False
If Not CanUsePreviousResults Then Calculate()
Calculated = True
PerformPostCalcValidation()
If GraphicObject IsNot Nothing Then
If GraphicObject.ObjectType <> ObjectType.EnergyStream And GraphicObject.ObjectType <> ObjectType.MaterialStream Then
Dim xdoc = New XDocument()
xdoc.Add(New XElement("Data"))
xdoc.Element("Data").Add(SaveData())
xdoc.Element("Data").Element("Calculated").Remove()
xdoc.Element("Data").Element("LastUpdated").Remove()
LastSolutionInputSnapshot = xdoc.ToString()
xdoc = Nothing
End If
End If
End If
If GHGEmissionData IsNot Nothing Then
GHGEmissionData.OwnerID = Name
GHGEmissionData.Flowsheet = FlowSheet
GHGEmissionData.Update()
End If
End Sub
<NonSerialized> <Xml.Serialization.XmlIgnore> Public fd As DynamicsPropertyEditor
Public Overridable Sub DisplayDynamicsEditForm() Implements ISimulationObject.DisplayDynamicsEditForm
If fd Is Nothing Then
fd = New DynamicsPropertyEditor With {.SimObject = Me}
fd.ShowHint = WeifenLuo.WinFormsUI.Docking.DockState.DockRight
fd.Tag = "ObjectEditor"
Me.FlowSheet.DisplayForm(fd)
Else
If fd.IsDisposed Then
fd = New DynamicsPropertyEditor With {.SimObject = Me}
fd.ShowHint = WeifenLuo.WinFormsUI.Docking.DockState.DockRight
fd.Tag = "ObjectEditor"
Me.FlowSheet.DisplayForm(fd)
Else
fd.Activate()
End If
End If
End Sub
Public Sub UpdateDynamicsEditForm() Implements ISimulationObject.UpdateDynamicsEditForm
If fd IsNot Nothing Then
If Not fd.IsDisposed Then
fd.UIThread(Sub() fd.UpdateInfo())
End If
End If
End Sub
Public Sub CloseDynamicsEditForm() Implements ISimulationObject.CloseDynamicsEditForm
If fd IsNot Nothing Then
If Not fd.IsDisposed Then
fd.Close()
fd = Nothing
End If
End If
End Sub
Public MustOverride Sub DisplayEditForm() Implements ISimulationObject.DisplayEditForm
Public MustOverride Sub UpdateEditForm() Implements ISimulationObject.UpdateEditForm
Public Overridable Function GetEditingForm() As Form Implements ISimulationObject.GetEditingForm
Return Nothing
End Function
''' <summary>
''' Energy Flow property. Only implemented for Energy Streams.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<Xml.Serialization.XmlIgnore()> Overridable Property EnergyFlow() As Nullable(Of Double)
Get
Return Nothing
End Get
Set(ByVal value As Nullable(Of Double))
End Set
End Property
''' <summary>
''' Phase collection, only implemented for Material Streams.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<Xml.Serialization.XmlIgnore()> Public Overridable ReadOnly Property Phases() As Dictionary(Of Integer, Interfaces.IPhase)
Get
Throw New NotImplementedException
End Get
End Property
''' <summary>
''' Validates the object, checking its connections and other parameters.
''' </summary>
''' <remarks></remarks>
Public Overridable Sub Validate() Implements Interfaces.ISimulationObject.Validate
Dim vForm As Interfaces.IFlowsheet = FlowSheet
Dim vCon As Interfaces.IConnectionPoint
'Validate input connections.
For Each vCon In Me.GraphicObject.InputConnectors
If Not vCon.IsAttached Then
Throw New Exception(Me.FlowSheet.GetTranslatedString("Verifiqueasconexesdo"))
End If
Next
'Validate output connections.
For Each vCon In Me.GraphicObject.OutputConnectors
If Not vCon.IsAttached Then
Throw New Exception(Me.FlowSheet.GetTranslatedString("Verifiqueasconexesdo"))
End If
Next
End Sub
Public Overridable Function GetDebugReport() As String Implements Interfaces.ISimulationObject.GetDebugReport
Return "Error - function not implemented"
End Function
Public Sub AppendDebugLine(text As String) Implements Interfaces.ISimulationObject.AppendDebugLine
DebugText += text & vbCrLf & vbCrLf
End Sub
''' <summary>
''' Gets or sets the error message regarding the last calculation attempt.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property ErrorMessage() As String Implements Interfaces.ISimulationObject.ErrorMessage
''' <summary>
''' Checks if a value is valid.
''' </summary>
''' <param name="val">Value to be checked.</param>
''' <param name="onlypositive">Value should be a positive double or not.</param>
''' <param name="paramname">Name of the parameter (ex. P, T, W, H etc.)</param>
''' <remarks></remarks>
Public Sub CheckSpec(val As Double, onlypositive As Boolean, paramname As String) Implements Interfaces.ISimulationObject.CheckSpec
If Not val.IsValid Then Throw New ArgumentException(Me.FlowSheet.GetTranslatedString("ErrorInvalidUOSpecValue") & " (name: " & paramname & ", value: " & val & ")")
If onlypositive Then If val.IsNegative Then Throw New ArgumentException(Me.FlowSheet.GetTranslatedString("ErrorInvalidUOSpecValue") & " (name: " & paramname & ", value: " & val & ")")
End Sub
Public Property Annotation() As String = "" Implements Interfaces.ISimulationObject.Annotation
''' <summary>
''' Checks if an Adjust operation is attached to this object.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property IsAdjustAttached() As Boolean = False Implements Interfaces.ISimulationObject.IsAdjustAttached
''' <summary>
''' If an Adjust object is attached to this object, returns its ID.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property AttachedAdjustId() As String = "" Implements Interfaces.ISimulationObject.AttachedAdjustId
''' <summary>
''' If an Adjust object is attached to this object, returns a variable describing how this object is used by it (manipulated, controlled or reference).
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property AdjustVarType() As Interfaces.Enums.AdjustVarType Implements Interfaces.ISimulationObject.AdjustVarType
''' <summary>
''' Checks if an Specification operation is attached to this object.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property IsSpecAttached() As Boolean = False Implements Interfaces.ISimulationObject.IsSpecAttached
''' <summary>
''' If an Specification object is attached to this object, returns its ID.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property AttachedSpecId() As String = "" Implements Interfaces.ISimulationObject.AttachedSpecId
''' <summary>
''' If an Specification object is attached to this object, returns a variable describing how this object is used by it (target or source).
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property SpecVarType() As Interfaces.Enums.SpecVarType Implements Interfaces.ISimulationObject.SpecVarType
''' <summary>
''' Gets or sets the graphic object representation of this object in the flowsheet.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<Xml.Serialization.XmlIgnore> Public Property GraphicObject() As Interfaces.IGraphicObject Implements Interfaces.ISimulationObject.GraphicObject
''' <summary>
''' Object's Unique ID (Name)
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks>This property is the same as the graphic object 'Name' property.</remarks>
Public Property Name() As String Implements Interfaces.ISimulationObject.Name
Get
Return ComponentName
End Get
Set(value As String)
ComponentName = value
End Set
End Property
Public Overridable Function GetProperties(proptype As PropertyType) As String() Implements Interfaces.ISimulationObject.GetProperties
Dim proplist As New List(Of String)
Dim epcol = DirectCast(ExtraProperties, IDictionary(Of String, Object))
For Each item In epcol
proplist.Add(item.Key)
Next
If FlowSheet IsNot Nothing Then
If FlowSheet.FlowsheetOptions.EnableGHGEmissionsSubsystem Then
If proptype <> PropertyType.WR Then
proplist.Add("GHG Emission Factor")
proplist.Add("GHG Mass Emission")
proplist.Add("GHG Molar Emission")
proplist.Add("CO2eq GHG Mass Emission")
proplist.Add("CO2eq GHG Molar Emission")
proplist.Add("GHG Emission Reference Power Value")
End If
End If
End If
For Each item In AttachedUtilities
proplist.AddRange(item.GetPropertyList().ConvertAll(New Converter(Of String, String)(Function(s As String)
Return item.Name & ": " & s
End Function)))
Next
Return proplist.ToArray
End Function
Public Overridable Function GetPropertyUnit(prop As String, Optional su As Interfaces.IUnitsOfMeasure = Nothing) As String Implements Interfaces.ISimulationObject.GetPropertyUnit
If su Is Nothing Then
su = FlowSheet.FlowsheetOptions.SelectedUnitSystem
End If
Dim epcol = DirectCast(ExtraProperties, IDictionary(Of String, Object))
Dim epucol = DirectCast(ExtraPropertiesUnitTypes, IDictionary(Of String, Object))
If epcol.ContainsKey(prop) Then
If epucol.ContainsKey(prop) Then
Dim utype = epucol(prop)
Return su.GetCurrentUnits(utype)
Else
Return ""
End If
Else
Select Case prop
Case "GHG Emission Factor"
Return "[kg/s]/kW"
Case "GHG Mass Emission"
Return "kg/s"
Case "GHG Molar Emission"
Return "mol/s"
Case "CO2eq GHG Mass Emission"
Return "kg/s"
Case "CO2eq GHG Molar Emission"
Return "mol/s"
Case "GHG Emission Reference Power Value"
Return "kW"
End Select
End If
For Each item In AttachedUtilities
If prop.StartsWith(item.Name) Then
For Each prop1 In item.GetPropertyList()
If prop.Contains(prop1) Then Return item.GetPropertyUnits(prop.Split(": ")(1).Trim)
Next
End If
Next
Return "NF"
End Function
Public Overridable Function GetPropertyValue(prop As String, Optional su As Interfaces.IUnitsOfMeasure = Nothing) As Object Implements Interfaces.ISimulationObject.GetPropertyValue
If su Is Nothing Then
su = FlowSheet.FlowsheetOptions.SelectedUnitSystem
End If
Dim epcol = DirectCast(ExtraProperties, IDictionary(Of String, Object))
Dim epucol = DirectCast(ExtraPropertiesUnitTypes, IDictionary(Of String, Object))
If epcol.ContainsKey(prop) Then
If epucol.ContainsKey(prop) Then
Dim utype = epucol(prop)
Dim units = su.GetCurrentUnits(utype)
Return cv.ConvertFromSI(units, Convert.ToDouble(epcol(prop)))
Else
Return epcol(prop)
End If
Else
Select Case prop
Case "GHG Emission Factor"
If GHGEmissionData IsNot Nothing Then
Return cv.ConvertFromSI(su.emission_factor, GHGEmissionData.GHGEmissionFactor)
Else
Return 0.0
End If
Case "GHG Mass Emission"
If GHGEmissionData IsNot Nothing Then
Return cv.ConvertFromSI(su.massflow, GHGEmissionData.GHGEmissionMassFlow)
Else
Return 0.0
End If
Case "GHG Molar Emission"
If GHGEmissionData IsNot Nothing Then
Return cv.ConvertFromSI(su.molarflow, GHGEmissionData.GHGEmissionMolarFlow)
Else
Return 0.0
End If
Case "CO2eq GHG Mass Emission"
If GHGEmissionData IsNot Nothing Then
Return cv.ConvertFromSI(su.massflow, GHGEmissionData.CO2eqEmissionMassFlow)
Else
Return 0.0
End If
Case "CO2eq GHG Molar Emission"
If GHGEmissionData IsNot Nothing Then
Return cv.ConvertFromSI(su.molarflow, GHGEmissionData.CO2eqEmissionMolarFlow)
Else
Return 0.0
End If
Case "GHG Emission Reference Power Value"
If GHGEmissionData IsNot Nothing Then
Return cv.ConvertFromSI(su.heatflow, GetEnergyConsumption())
Else
Return 0.0
End If
End Select
End If
For Each item In AttachedUtilities
If prop.StartsWith(item.Name) Then
For Each prop1 In item.GetPropertyList()
If prop.Contains(prop1) Then Return item.GetPropertyValue(prop.Split(": ")(1).Trim)
Next
End If
Next
Return Nothing
End Function
Public Overridable Function SetPropertyValue(prop As String, propval As Object, Optional su As Interfaces.IUnitsOfMeasure = Nothing) As Boolean Implements Interfaces.ISimulationObject.SetPropertyValue
Dim epcol = DirectCast(ExtraProperties, IDictionary(Of String, Object))
If epcol.ContainsKey(prop) Then
epcol(prop) = propval
Return True
End If
For Each item In AttachedUtilities
If prop.StartsWith(item.Name) Then
For Each prop1 In item.GetPropertyList()
If prop.Contains(prop1) Then
item.SetPropertyValue(prop.Split(": ")(1).Trim, propval)
Return True
End If
Next
End If
Next
Return False
End Function
Public Overridable Function GetDefaultProperties() As String() Implements ISimulationObject.GetDefaultProperties
Return GetProperties(PropertyType.ALL)
End Function
Public Overridable Property PropertyPackage As IPropertyPackage Implements ISimulationObject.PropertyPackage
Public Function GetFlowsheet() As IFlowsheet Implements ISimulationObject.GetFlowsheet
Return FlowSheet
End Function
Public MustOverride Sub CloseEditForm() Implements ISimulationObject.CloseEditForm
Public MustOverride Function CloneXML() As Object Implements ISimulationObject.CloneXML
Public MustOverride Function CloneJSON() As Object Implements ISimulationObject.CloneJSON
Public MustOverride ReadOnly Property MobileCompatible As Boolean Implements ISimulationObject.MobileCompatible
Public Property UserDefinedChartNames As New List(Of String)
Public Property CreateChartAction As Func(Of String, Object)
Public Overridable Function GetChartModelNames() As List(Of String) Implements ISimulationObject.GetChartModelNames
If UserDefinedChartNames.Count > 0 Then
Return UserDefinedChartNames
Else
Return New List(Of String)
End If
End Function
#End Region
#Region " ICloneable"
''' <summary>
''' Clones the current object, returning a new one with identical properties.
''' </summary>
''' <returns>An object of the same type with the same properties.</returns>
''' <remarks>Properties and fields marked with the 'NonSerializable' attribute aren't cloned.</remarks>
Public Overridable Function Clone() As Object Implements System.ICloneable.Clone
Return ObjectCopy(Me)
End Function
Function ObjectCopy(ByVal obj As UnitOperations.BaseClass) As Object
Return Me.CloneXML
End Function
#End Region
#Region " IDisposable Support "
Public disposedValue As Boolean = False ' To detect redundant calls
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
Me.disposedValue = True
End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
#Region " IXMLSerialization"
''' <summary>
''' Loads object data stored in a collection of XML elements.
''' </summary>
''' <param name="data"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Overridable Function LoadData(data As System.Collections.Generic.List(Of System.Xml.Linq.XElement)) As Boolean Implements Interfaces.ICustomXMLSerialization.LoadData
XMLSerializer.XMLSerializer.Deserialize(Me, data)
ExtraProperties = New ExpandoObject
Dim xel_d = (From xel2 As XElement In data Select xel2 Where xel2.Name = "DynamicProperties")
If Not xel_d Is Nothing Then
Dim dataDyn As List(Of XElement) = xel_d.Elements.ToList
For Each xel As XElement In dataDyn
Try
Dim propname = xel.Element("Name").Value
Dim proptype = xel.Element("PropertyType").Value
Dim ptype As Type = Type.GetType(proptype)
Dim propval = Newtonsoft.Json.JsonConvert.DeserializeObject(xel.Element("Data").Value, ptype)
DirectCast(ExtraProperties, IDictionary(Of String, Object))(propname) = propval
Catch ex As Exception
End Try
Next
End If
ExtraPropertiesDescriptions = New ExpandoObject
Dim xel_dd = (From xel2 As XElement In data Select xel2 Where xel2.Name = "DynamicPropertiesDescriptions")
If Not xel_dd Is Nothing Then
Dim dataDyn As List(Of XElement) = xel_dd.Elements.ToList
For Each xel As XElement In dataDyn
Try
Dim propname = xel.Element("Name").Value
Dim proptype = xel.Element("PropertyType").Value
Dim ptype As Type = Type.GetType(proptype)
Dim propval = Newtonsoft.Json.JsonConvert.DeserializeObject(xel.Element("Data").Value, ptype)
DirectCast(ExtraPropertiesDescriptions, IDictionary(Of String, Object))(propname) = propval
Catch ex As Exception