-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.go
109 lines (93 loc) · 2.77 KB
/
blocks.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
package loops
import (
"fmt"
)
// In this file some standard blocks are defined.
// An application may define it's own blocks of any type.
// All they need to do, is to implement the Block interface.
//
// See loops.go for the interface definition.
//
// An type which satisfies the interface may be used.
// The type may store complex state data in a struct,
// use a base type such as Scale which is a float64
// or it can be a dummy, such as Add which is an empty struct.
// Scale multiplies it's input with a constant factor.
type Scale float64
func (b Scale) Inputs() int { return 1 }
func (b Scale) Outputs() int { return 1 }
func (b Scale) Step(in, out []float64) bool {
out[0] = float64(b) * in[0]
return true
}
// Add adds too inputs and sends the result to the output channel.
type Add struct{}
func (b Add) Inputs() int { return 2 }
func (b Add) Outputs() int { return 1 }
func (b Add) Step(in, out []float64) bool {
out[0] = in[0] + in[1]
return true
}
// Integrate does a simple time integration.
// The block is used to solve differential equations.
type Integrate struct {
State float64 // This can be set as the initial state.
}
func (b *Integrate) Inputs() int { return 1 }
func (b *Integrate) Outputs() int { return 1 }
func (b *Integrate) Step(in, out []float64) bool {
b.State += in[0] * DT
out[0] = b.State
return true
}
// Source emits a constant value each time it is called.
type Source float64
func (b Source) Inputs() int { return 0 }
func (b Source) Outputs() int { return 1 }
func (b Source) Step(in, out []float64) bool {
out[0] = float64(b)
return true
}
// Print prints every input.
// It is used as a termination block.
// It keeps track of the global time, in order to print both, time and value.
type Print struct {
time float64
}
func (b *Print) Inputs() int { return 1 }
func (b *Print) Outputs() int { return 0 }
func (b *Print) Step(in, out []float64) bool {
fmt.Println(b.time, in[0])
b.time += DT
return true
}
// Tee multiplexes it's input to two ouput channels.
type Tee struct{}
func (b Tee) Inputs() int { return 1 }
func (b Tee) Outputs() int { return 2 }
func (b Tee) Step(in, out []float64) bool {
out[0] = in[0]
out[1] = in[0]
return true
}
// A Stop block can be inserted between two other blocks.
// It transparently copies it's input to the output and terminates
// the program when a stop time is reached.
// It can call registered cleanup functions.
type Stop struct {
Time float64 // Stop time.
Callbacks []func() // A slice of callbacks.
t float64 // current time
}
func (s *Stop) Inputs() int { return 1 }
func (s *Stop) Outputs() int { return 1 }
func (s *Stop) Step(in, out []float64) bool {
if s.t += DT; s.t >= s.Time {
for _, f := range s.Callbacks {
f()
}
return false
}
out[0] = in[0]
return true
}