-
Notifications
You must be signed in to change notification settings - Fork 0
/
phasor.go
65 lines (54 loc) · 1.54 KB
/
phasor.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
// Copyright 2021 readpe All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package goolx
import (
"fmt"
"math"
"math/cmplx"
)
var (
a1 = NewPhasor(1, 120)
a2 = NewPhasor(1, 240)
)
// PhaseToSeq converts from phase values to sequential components. Three phase only.
func PhaseToSeq(a, b, c Phasor) (seq0, seq1, seq2 Phasor) {
seq0 = (1.0 / 3.0) * (a + b + c)
seq1 = (1.0 / 3.0) * (a + a1*b + a2*c)
seq2 = (1.0 / 3.0) * (a + a2*b + a1*c)
return
}
// SeqToPhase converts from sequential compoents to phase values. Three phase only.
func SeqToPhase(seq0, seq1, seq2 Phasor) (a, b, c Phasor) {
a = seq0 + seq1 + seq2
b = seq0 + a2*seq1 + a1*seq2
c = seq0 + a1*seq1 + a2*seq2
return
}
// Phasor represents a phasor value for common power system calculations.
type Phasor complex128
// NewPhasor returns a new phasor instance.
func NewPhasor(mag, ang float64) Phasor {
return Phasor(cmplx.Rect(mag, ang*math.Pi/180.0))
}
// Mag returns the Phasor absolute magnitude.
func (p Phasor) Mag() float64 {
r, _ := cmplx.Polar(complex128(p))
return r
}
// Ang returns the Phasor angle in degrees.
func (p Phasor) Ang() float64 {
if p.Mag() < 1e-5 {
return 0
}
θ := cmplx.Phase(complex128(p))
return θ * 180.0 / math.Pi
}
// Rect returns the complex128 representation.
func (p Phasor) Rect() complex128 {
return complex128(p)
}
// String implements the stringer interface for the Phasor type.
func (p Phasor) String() string {
return fmt.Sprintf("%0.2f\u2220%0.1f\u00B0", p.Mag(), p.Ang())
}