-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
185 lines (165 loc) · 4.34 KB
/
main.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
package main
import (
"CE366VerticalStress/estimation"
"CE366VerticalStress/types"
"encoding/json"
"errors"
"fmt"
"github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus"
"os"
"strconv"
)
var LineMap map[string]*types.Line
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(log.DebugLevel)
LineMap = make(map[string]*types.Line)
}
func main() {
app := fiber.New(fiber.Config{
AppName: "CE366 Vertical Stress Contours",
})
app.Static("/ce366", "./public")
app.Post("/api/contours", func(c *fiber.Ctx) error {
requestParameters := new(types.RequestParameters)
LineMap = make(map[string]*types.Line)
if err := c.BodyParser(requestParameters); err != nil {
return err
}
qP, err := strconv.ParseFloat(requestParameters.Q, 64)
if err != nil {
return err
}
bP, err := strconv.ParseFloat(requestParameters.B, 64)
if err != nil {
return err
}
parameters := types.Parameters{
Q: qP,
B: bP,
}
log.Info("will calculate with the following parameters", parameters)
jsonBytes, err := ProcessContours(parameters)
if err != nil {
return err
}
return c.JSON(string(jsonBytes))
})
app.Listen(":9924")
}
func ProcessContours(p types.Parameters) ([]byte, error) {
//if err := f.SaveAs("points.xlsx"); err != nil { }
var contours []types.Contour
// Populate the slice with Contour elements
for i := 2; i <= 9; i++ {
coefficient := float64(i) / 10
name := "Contour-" + strconv.FormatFloat(coefficient, 'f', 1, 64)
var color string
if i%9 == 0 {
color = "black"
} else if absInt(i-9) == 1 {
color = "indigo"
} else if absInt(i-9) == 2 {
color = "steelblue"
} else if absInt(i-9) == 3 {
color = "slategrey"
} else if absInt(i-9) == 4 {
color = "forestgreen"
} else if absInt(i-9) == 5 {
color = "red"
} else if absInt(i-9) == 6 {
color = "green"
} else if absInt(i-9) == 7 {
color = "midnightblue"
} else if absInt(i-9) == 8 {
color = "red"
}
contours = append(contours, types.Contour{Name: name, Coefficient: coefficient, Color: color})
}
// Iterate over the slice and print each element
for _, contour := range contours {
//fmt.Printf("Name: %s, Coefficient: %.1f\n", contour.Name, contour.Coefficient)
ProcessCurve(contour.Name, contour.Coefficient, p, contour.Color)
}
return LineMapToJSON()
}
func ProcessCurve(name string, coeff float64, p types.Parameters, color string) {
curveList := make([]*types.CurveInf, 0)
curve00 := types.CurveInf{Name: name, Coefficient: coeff}
curveList = append(curveList, &curve00)
est := estimation.Estimator{Q: p.Q, B: p.B, Curves: curveList}
est.CalculateTheContours()
for _, curve := range est.Curves {
for _, dataPoint := range curve.Points {
SavePointToLine(curve.Name, dataPoint, color)
}
}
}
func SavePointToLine(name string, point types.RelativePointInfo, color string) {
n, ok := LineMap[name]
if ok {
point1 := types.Point{
X: point.Y,
Y: point.Z * -1.0,
}
point2 := types.Point{
X: point.Y * -1.0,
Y: point.Z * -1.0,
}
n.Points = append(n.Points, point1, point2)
} else {
LineMap[name] = &types.Line{
Color: color,
Name: name,
Points: []types.Point{},
}
s := LineMap[name]
point0 := types.Point{
X: point.Y,
Y: point.Z * -1.0,
}
point02 := types.Point{
X: point.Y * -1.0,
Y: point.Z * -1.0,
}
s.Points = append(s.Points, point0, point02)
}
}
func LineMapToJSON() ([]byte, error) {
// Convert lines to JSON
linesJSON, err := json.Marshal(LineMap)
if err != nil {
fmt.Println("Error marshaling lines:", err)
return nil, errors.New(err.Error())
}
return linesJSON, nil
/*// Create and write to points.js file
file, err := os.Create("points.js")
if err != nil {
fmt.Println("Error creating points.js file:", err)
return nil, errors.New( err.Error())
}
defer file.Close()
jsContent := fmt.Sprintf("window.lines = %s;", linesJSON)
_, err = file.WriteString(jsContent)
if err != nil {
fmt.Println("Error writing to points.js file:", err)
return nil, errors.New( err.Error())
}
*/
}
func absInt(x int) int {
return absDiffInt(x, 0)
}
func absDiffInt(x, y int) int {
if x < y {
return y - x
}
return x - y
}