-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvariable.go
303 lines (269 loc) · 7.57 KB
/
variable.go
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
package dynamo
//----------------------------------------------------------------------
// This file is part of Dynamo.
// Copyright (C) 2011-2020 Bernd Fix
//
// Dynamo is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// Dynamo 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
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later
//----------------------------------------------------------------------
import (
"fmt"
"go/ast"
"math"
"reflect"
"strings"
"unicode"
)
//======================================================================
// A VARIABLE represents a named feature (or a constant) in a system
// model. The name is string beginning with a letter and an optional
// index. In 'strict' mode the name is limited to max. six characters.
//======================================================================
//----------------------------------------------------------------------
// NAME -- A variable name consists of two parts: a simple name part and
// an (optional) index, spearated by a dot ("."). The name must be
// uppercase and start with a letter; the length of a name is limited to
// MAX_NAME_LENGTH. The index part is limited to values as defined in
// INDEX_LIST. The 'stage' attribute classifies the temporality of the
// index (whether it refers to 'now' or 'past' states).
//
// Example names: COFFEE, SHPMTS.JK, INV.K
//----------------------------------------------------------------------
// Name-related constants
const (
MAX_NAME_LENGTH = 6 // max. length of name in 'strict' mode
// Kind of variable
NAME_KIND_CONST = 0
NAME_KIND_INIT = 1
NAME_KIND_LEVEL = 2
NAME_KIND_RATE = 3
NAME_KIND_AUX = 4
NAME_KIND_SUPPL = 5
// Stage of variable
NAME_STAGE_NONE = 0 // only constants can have this stage
NAME_STAGE_OLD = 1
NAME_STAGE_NEW = 2
// Results for Name.Compare:
NAME_MISMATCH = 0 // names don't match
NAME_SAMEVAR = 1 // variables have same name
NAME_SAMEKIND = 2 // variable are of same kind
NAME_SAMESTAGE = 4 // variables have same stage
NAME_MATCH = 7 // names match fully
)
var (
autoId = 0 // last automatic variable identifier
)
// NewAutoVar generates a new automatic variable name
func NewAutoVar() string {
autoId++
return fmt.Sprintf("_%d", autoId)
}
// Class is a classification for variables
type Class struct {
Kind int // NAME_KIND_?
Stage int // NAME_STAGE_?
}
// Name of a state variable
type Name struct {
Class
Name string // Name of the variable
}
// NewName returns a name instance for a given identifier.
func NewName(v ast.Expr) (name *Name, res *Result) {
res = Success()
switch x := v.(type) {
case *ast.Ident:
name = new(Name)
name.Kind = NAME_KIND_CONST
name.Stage = NAME_STAGE_NONE
name.Name = x.Name
if len(name.Name) > MAX_NAME_LENGTH {
if strict {
res = Failure(ErrParseNameLength+": %s", name.Name)
} else {
Msgf("WARN: "+ErrParseNameLength+": %s", name.Name)
}
}
start := []rune(name.Name)[0]
if !unicode.IsLetter(start) && start != '_' {
if strict {
res = Failure(ErrParseInvalidName+": %s", name.Name)
} else {
Msgf("WARN: "+ErrParseInvalidName+": %s", name.Name)
}
}
return
case *ast.SelectorExpr:
if name, res = NewName(x.X); !res.Ok {
return
}
res = name.setIndex(x.Sel.Name)
default:
res = Failure(ErrParseInvalidName+": %s", reflect.TypeOf(v))
}
return
}
// NewNameFromString returns a name instance for a given identifier.
func NewNameFromString(n string) (name *Name, res *Result) {
res = Success()
parts := strings.Split(n, ".")
name = new(Name)
name.Kind = NAME_KIND_CONST
name.Stage = NAME_STAGE_NONE
name.Name = parts[0]
if len(name.Name) > MAX_NAME_LENGTH {
if strict {
res = Failure(ErrParseNameLength+": %d", len(name.Name))
} else {
Msgf("WARN: "+ErrParseNameLength+": %s", name.Name)
}
}
if len(parts) > 1 {
res = name.setIndex(parts[1])
}
return
}
// SetIndex sets name flags for a given index string
func (n *Name) setIndex(idx string) (res *Result) {
res = Success()
switch idx {
case "J":
n.Kind = NAME_KIND_LEVEL
n.Stage = NAME_STAGE_OLD
case "JK":
n.Kind = NAME_KIND_RATE
n.Stage = NAME_STAGE_OLD
case "K":
n.Kind = NAME_KIND_LEVEL
n.Stage = NAME_STAGE_NEW
case "KL":
n.Kind = NAME_KIND_RATE
n.Stage = NAME_STAGE_NEW
default:
res = Failure(ErrParseInvalidIndex+": %s", idx)
}
return
}
// GetIndex returns the variable index
func (n *Name) GetIndex() string {
if n.Stage == NAME_STAGE_OLD {
if n.Kind == NAME_KIND_LEVEL {
return ".J"
}
if n.Kind == NAME_KIND_RATE {
return ".JK"
}
} else if n.Stage == NAME_STAGE_NEW {
if n.Kind == NAME_KIND_LEVEL {
return ".K"
}
if n.Kind == NAME_KIND_RATE {
return ".KL"
}
}
return ""
}
// String returns a name in human-readable format
func (n *Name) String() (name string) {
name = n.Name
switch n.Kind {
case NAME_KIND_CONST:
name += "/C"
case NAME_KIND_INIT:
name += "/I"
case NAME_KIND_AUX:
name += "/A"
case NAME_KIND_SUPPL:
name += "/S"
case NAME_KIND_LEVEL:
name += "/L"
case NAME_KIND_RATE:
name += "/R"
}
return
}
// Compare checks if two names (partially) match.
func (n *Name) Compare(m *Name) int {
match := NAME_MISMATCH
if n.Name == m.Name {
match |= NAME_SAMEVAR
}
if n.Kind == m.Kind {
match |= NAME_SAMEKIND
}
if n.Stage == m.Stage {
match |= NAME_SAMESTAGE
}
return match
}
//----------------------------------------------------------------------
// VARIABLE -- represents variables in equations.
//----------------------------------------------------------------------
// Variable has a floating point value
type Variable float64
// String returns the human-readable representation of a variable
func (v Variable) String() string {
return fmt.Sprintf("%f", v)
}
//----------------------------------------------------------------------
// MATH methods on variables
//----------------------------------------------------------------------
// Sqrt: return the sqare root
func (v Variable) Sqrt() Variable {
return Variable(math.Sqrt(float64(v)))
}
func (v Variable) Sin() Variable {
return Variable(math.Sin(float64(v)))
}
func (v Variable) Cos() Variable {
return Variable(math.Cos(float64(v)))
}
func (v Variable) Exp() Variable {
return Variable(math.Exp(float64(v)))
}
func (v Variable) Log() Variable {
return Variable(math.Log(float64(v)))
}
func (v Variable) Floor() Variable {
return Variable(math.Floor(float64(v)))
}
func (v Variable) Compare(x Variable) int {
return compare(float64(v), float64(x))
}
//----------------------------------------------------------------------
// TSVar -- Time-series variable
//----------------------------------------------------------------------
// TSVar is a named variable with a list of values (time series)
type TSVar struct {
Name string // variable name
Min, Max float64 // plot range
Values []float64 // time-series of values
}
// Add a TSVar value
func (ts *TSVar) Add(y float64) {
if len(ts.Values) == 0 {
ts.Min = y
ts.Max = y
} else if y < ts.Min {
ts.Min = y
} else if y > ts.Max {
ts.Max = y
}
ts.Values = append(ts.Values, y)
}
// Reset time-series
func (ts *TSVar) Reset() {
ts.Values = make([]float64, 0)
}