-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderived_units.go
186 lines (163 loc) · 2.76 KB
/
derived_units.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
package si_units
type Area struct {
SIUnit
}
func NewArea(name string, area float64) *Area {
id := "area-" + name
return &Area{
SIUnit{
Id: id,
Name: name,
Value: area,
Unit: "m^2",
},
}
}
func CalculateNewArea(name string, num1 *Length, num2 *Length) *Area {
id := "area-" + name
return &Area{
SIUnit{
Id: id,
Name: name,
Value: num1.Value * num2.Value,
Unit: "m^2",
},
}
}
type Volume struct {
SIUnit
}
func NewVolume(name string, vol float64) *Volume {
id := "volume-" + name
return &Volume{
SIUnit{
Id: id,
Name: name,
Value: vol,
Unit: "m^3",
},
}
}
type Density struct {
SIUnit
}
func NewDensity(name string, density float64) *Density {
id := "density-" + name
return &Density{
SIUnit{
Id: id,
Name: name,
Value: density,
Unit: "kg/m^3",
},
}
}
func (d *Density) MultiplyVolume(v *Volume) *Mass {
return NewMass(
d.Name+"*"+v.Name,
d.Value*v.Value,
)
}
type FlowRate struct {
SIUnit
}
func NewFlowRate(name string, flowRate float64) *FlowRate {
id := "flowRate-" + name
return &FlowRate{
SIUnit{
Id: id,
Name: name,
Value: flowRate,
Unit: "m^3/s",
},
}
}
func (f *FlowRate) MultiplyTime(t *Time) *Volume {
return NewVolume(
f.Name+"*"+t.Name,
f.Value*t.Value,
)
}
type MolarMass struct {
SIUnit
}
func NewMolarMass(name string, molarMass float64) *MolarMass {
id := "molarMass-" + name
return &MolarMass{
SIUnit{
Id: id,
Name: name,
Value: molarMass,
Unit: "kg/mol",
},
}
}
// Vector units
type Force struct {
SIUnit
}
func NewForce(name string, f *Vector) *Force {
id := "force-" + name
return &Force{
SIUnit{
Id: id,
Name: name,
VectorValue: f,
Unit: "N",
},
}
}
type Displacement struct {
SIUnit
}
func NewDisplacement(name string, v *Vector) *Displacement {
id := "displacement-" + name
return &Displacement{
SIUnit{
Id: id,
Name: name,
VectorValue: v,
Unit: "m",
},
}
}
type Velocity struct {
SIUnit
}
func NewVelocity(name string, v *Vector) *Velocity {
id := "velocity-" + name
return &Velocity{
SIUnit{
Id: id,
Name: name,
VectorValue: v,
Unit: "m/s",
},
}
}
func (v *Velocity) MultiplyTime(t *Time) *Displacement {
return NewDisplacement(
v.Name+"*"+t.Name,
v.VectorValue.Multiply(t.Value),
)
}
type Acceleration struct {
SIUnit
}
func NewAcceleration(name string, v *Vector) *Acceleration {
id := "acceleration-" + name
return &Acceleration{
SIUnit{
Id: id,
Name: name,
VectorValue: v,
Unit: "m/s^2",
},
}
}
func (a *Acceleration) MultiplyTime(t *Time) *Velocity {
return NewVelocity(
a.Name+"*"+t.Name,
a.VectorValue.Multiply(t.Value),
)
}