-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.py
264 lines (210 loc) · 7.23 KB
/
helpers.py
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
import numpy as np
import time
import yaml
import os
import re
def cart2pol(x, y):
rho = np.hypot(x, y)
phi = np.arctan2(y, x)
return(rho, phi)
def pol2cart(rho, phi):
x = rho * np.cos(phi)
y = rho * np.sin(phi)
return(x, y)
def printMat(mat):
'''Prints a matrix to a format that is specified
Parameters
----------
mat : array
Any matrix that is to be printed.
Returns
-------
None.
'''
for i in range(mat.shape[0]):
print( "\t".join(["{:+8.3e}"]*mat.shape[1]).format( *mat[i,:] ))
def printVec(vec):
'''Prints a vector to a format that is specified
Parameters
----------
vec : array
Any vector that is to be printed.
Returns
-------
None.
'''
print( "\t".join(["{:+9.4e}"]*len(vec)).format( *vec ))
def unitVector(r):
'''Returns the unit vector along the direction of input vector r.'''
L = np.linalg.norm(r)
return r/L
def updateYAML_array(fname,outx,outy,turbID,pfID,moorID,hadjust,newFile):
'''
Write turbines and locations to yaml file. Recommend using a different file name for the output than
the input yaml file, because the array table section will be written out in a manner that is not as readable
Parameters
----------
fname : str
filename of yaml to read from
outx : array
1D array of x coordinates for platform locations
outy : array
1D array of y coordinates for platform locations
turbID : array
1D array of ID number of turbine for each platform, referencing turbine listing in ontology yaml
pfID : array
1D array of ID number of platform type for each platform, referencing platform listing in ontology yaml
moorID : str or int
1D array of ID of mooring system for each platform, referencing mooring system in ontology yaml
hadjust : array
1D array of angle rotation for each platform
newFile : str
New file to write yaml to
'''
import ruamel.yaml
yaml = ruamel.yaml.YAML()
# read in yaml file
with open(fname) as fp:
data = yaml.load(fp)
# add rows for all platforms with general info
data['array']['data'] = [] # remove any existing rows in the array table
for i in range(0,len(outx)):
data['array']['data'].append(['fowt'+str(i),turbID[i],pfID[i],moorID[i],float(outx[i]),float(outy[i]),hadjust[i]])
# write to yaml file
with open(newFile,'w') as f:
yaml.dump(data,f)
def updateYAML_MooringConfig(fname,ms,newfile):
'''
Update a yaml file with mooring configuration and mooring line type info from a moorpy system
Parameters
----------
fname : str
YAML file to read in
ms : object
MoorPy system
newfile : str
YAML file to write to
Returns
-------
None.
'''
import ruamel.yaml
yaml = ruamel.yaml.YAML()
from moorpy.subsystem import Subsystem
from moorpy.helpers import lines2ss
# read in yaml file
with open(fname) as fp:
data = yaml.load(fp)
# fill in mooring line types info
for mtype in ms.lineTypes:
data['mooring_line_types'][mtype] = ms.lineTypes[mtype]
for i,line in enumerate(ms.lineList):
if not isinstance(line,Subsystem):
# convert to subsystem
lines2ss(ms)
types = []
lengths = []
connType = []
for seg in line:
types.append(seg.type['name'])
lengths.append(seg.L)
for j,conn in enumerate(line.pointList):
connType.append({})
if conn.m != 0:
connType.append({'m':conn.m})
connType[-1]['v'] = conn.v
connType[-1]['Cd'] = conn.cd
#
def updateYAML_mooring(fname,ms,newfile):
'''
Update a yaml file with mooring line information and platform locations from a moorpy system.
Does not support cables currently.
Parameters
----------
fname : str
YAML file to read in
ms : object
MoorPy system
newfile : str
YAML file to write to
Returns
-------
None.
'''
import ruamel.yaml
yaml = ruamel.yaml.YAML()
# read in yaml file
with open(fname) as fp:
data = yaml.load(fp)
# fill in mooring line types info
for mtype in ms.lineTypes:
data['mooring_line_types'][mtype] = ms.lineTypes[mtype]
# convert to subsystems if needed
# fill in mooring systems info and parse for similar mooring systems (same line types, lengths, spans, # lines, headings)
# msys = []
# for pf in ms.bodyList:
# psys = []
# for pidx in pf.attachedP:
# lineDetails = []
# for lidx in ms.pointList[pidx-1].attached:
# ss = ms.lineList[lidx-1]
# lineDetails.append(ss.span) # span
# # calc heading
# heading = np.pi/2 - np.atan2((ss.rB[1]-ss.rA[1]),(ss.rB[0]-ss.rA[0]))
# lineDetails.append(heading) # heading
# # get segment details
# segType = []
# segL = []
# for seg in ss.lineList:
# segType.append(seg.type['name'])
# segL.append(seg.L)
# lineDetails.extend(segType,segL) # segment details
# # append line details to system for that platform
# psys.append(lineDetails)
# # append platfrom line system to mooring system
# msys.append(psys)
'''spans = []
headings = []
segTypes = []
segLs = []
for pf in ms.bodyList:
spansP = []
headingsP = []
segTypesP = []
segLsP = []
for pidx in pf.attachedP:
for lidx in ms.pointList[pidx-1].attached: # should only be one line
ss = ms.lineList[lidx-1]
spansP.append(ss.span)
# calc heading
heading = np.pi/2 - np.atan2((ss.rB[1]-ss.rA[1]),(ss.rB[0]-ss.rA[0]))
headingsP.append(heading) # heading
# get segment details
segType = []
segL = []
for seg in ss.lineList:
segType.append(seg.type['name'])
segL.append(seg.L)
segTypesP.append(segType)
segLsP.append(segL)
# append line details to system for that platform
spans.append(spansP)
headings.append(headingsP)
segTypes.append(segTypesP)
segLs.append(segLsP)
# first find where lengths of spans are the same
lenspans = [len(span) for span in spans]
uspanL = np.unique(lenspans)
ind = []
for i in range(0,len(uspanL)):
ind.append(np.where(spans == uspanL[i])[0])
# then find where spans are the same
spinds = []
for ix in ind:
for jx in ix:
spinds_in = []
for k in range(0,len(spans[jx])):
spinds_in.append(np.where(spans[ix][k]==spans[jx][k])[0])
if spinds_in[-1]'''
# add rows for all platforms with general info
data['array']['data'] = []