File size: 6,635 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'    Sensitivity Analysis Classes
'    Copyright 2009-2014 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.Xml
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports Ciloci.Flee
Imports System.Linq

Namespace Flowsheet.Optimization

    <System.Serializable()> Public Class SensitivityAnalysisCase

        Implements ICloneable, Interfaces.ICustomXMLSerialization

        Public iv1 As New SAVariable
        Public iv2 As New SAVariable
        Public dv As New SAVariable

        Public name As String = ""
        Public description As String = ""
        <Xml.Serialization.XmlIgnore()> Public results As ArrayList
        Public stats As String = ""
        Public numvar As Integer = 1

        Public depvartype As SADependentVariableType = SADependentVariableType.Variable

        Public expression As String = ""
        <System.NonSerialized()> Public exbase As IGenericExpression(Of Double)
        <System.NonSerialized()> Public econtext As ExpressionContext

        Public variables As New Dictionary(Of String, SAVariable)
        Public depvariables As New Dictionary(Of String, SAVariable)

        Sub New()
            iv1 = New SAVariable
            iv2 = New SAVariable
            dv = New SAVariable
            variables = New Dictionary(Of String, SAVariable)
            depvariables = New Dictionary(Of String, SAVariable)
            results = New ArrayList()
        End Sub

        Public Function Clone() As Object Implements System.ICloneable.Clone

            Return ObjectCopy(Me)

        End Function

        Function ObjectCopy(ByVal obj As Object) As Object

            Dim objMemStream As New MemoryStream(50000)
            Dim objBinaryFormatter As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.Clone))

            objBinaryFormatter.Serialize(objMemStream, obj)

            objMemStream.Seek(0, SeekOrigin.Begin)

            ObjectCopy = objBinaryFormatter.Deserialize(objMemStream)

            objMemStream.Close()

        End Function

        Public Function LoadData(data As System.Collections.Generic.List(Of System.Xml.Linq.XElement)) As Boolean Implements Interfaces.ICustomXMLSerialization.LoadData

            XMLSerializer.XMLSerializer.Deserialize(Me, data, True)

            Dim xel As XElement = (From xel2 As XElement In data Select xel2 Where xel2.Name = "IV1").SingleOrDefault
            iv1.LoadData(xel.Elements.ToList)
            xel = (From xel2 As XElement In data Select xel2 Where xel2.Name = "IV2").SingleOrDefault
            iv2.LoadData(xel.Elements.ToList)
            xel = (From xel2 As XElement In data Select xel2 Where xel2.Name = "DV").SingleOrDefault
            dv.LoadData(xel.Elements.ToList)

            For Each xel0 As XElement In (From xel2 As XElement In data Select xel2 Where xel2.Name = "Variables").SingleOrDefault.Elements.ToList
                Dim savar As New SAVariable
                savar.LoadData(xel0.Elements.ToList)
                variables.Add(xel0.@Key, savar)
            Next

            For Each xel0 As XElement In (From xel2 As XElement In data Select xel2 Where xel2.Name = "DepVariables").SingleOrDefault.Elements.ToList
                Dim savar As New SAVariable
                savar.LoadData(xel0.Elements.ToList)
                depvariables.Add(xel0.@Key, savar)
            Next
            Return True

        End Function

        Public Function SaveData() As System.Collections.Generic.List(Of System.Xml.Linq.XElement) Implements Interfaces.ICustomXMLSerialization.SaveData

            Dim elements As List(Of XElement) = XMLSerializer.XMLSerializer.Serialize(Me, True)

            With elements
                .Add(New XElement("IV1", iv1.SaveData.ToArray))
                .Add(New XElement("IV2", iv2.SaveData.ToArray))
                .Add(New XElement("DV", dv.SaveData.ToArray))
                .Add(New XElement("Variables"))
                For Each kvp As KeyValuePair(Of String, SAVariable) In variables
                    .Item(.Count - 1).Add(New XElement("Variable", New XAttribute("Key", kvp.Key), kvp.Value.SaveData.ToArray))
                Next
                .Add(New XElement("DepVariables"))
                For Each kvp As KeyValuePair(Of String, SAVariable) In depvariables
                    .Item(.Count - 1).Add(New XElement("Variable", New XAttribute("Key", kvp.Key), kvp.Value.SaveData.ToArray))
                Next
            End With

            Return elements

        End Function

    End Class

    <System.Serializable()> Public Class SAVariable

        Implements Interfaces.ICustomXMLSerialization

        Public objectID As String = ""
        Public objectTAG As String = ""
        Public propID As String = ""
        Public unit As String = ""
        Public points As Integer = 0
        Public name As String = ""
        Public id As String = ""
        Public currentvalue As Double = 0.0#
        Public lowerlimit As Nullable(Of Double)
        Public upperlimit As Nullable(Of Double)

        Sub New()
            points = 5
        End Sub

        Public Function LoadData(data As System.Collections.Generic.List(Of System.Xml.Linq.XElement)) As Boolean Implements Interfaces.ICustomXMLSerialization.LoadData
            XMLSerializer.XMLSerializer.Deserialize(Me, data, True)
            Return True
        End Function

        Public Function SaveData() As System.Collections.Generic.List(Of System.Xml.Linq.XElement) Implements Interfaces.ICustomXMLSerialization.SaveData
            Return XMLSerializer.XMLSerializer.Serialize(Me, True)
        End Function

    End Class

    Public Enum SADependentVariableType
        Variable = 0
        Expression = 1
    End Enum

End Namespace