-
Notifications
You must be signed in to change notification settings - Fork 23
/
Hull.js
657 lines (470 loc) · 15.6 KB
/
Hull.js
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import JSONSpecObject from "./JSONSpecObject.js";
import { parametricWeightHull } from "../math/parametricWeightParsons.js";
import { bisectionSearch, lerp } from "../math/interpolation.js";
import { sectionCalculation } from "../math/areaCalculations.js";
import { trapezoidCalculation } from "../math/areaCalculations.js";
import { patchColumnCalculation, combineVolumes } from "../math/volumeCalculations.js";
import Vectors from "../math/Vectors.js";
export default class DerivedObject extends JSONSpecObject {
constructor( spec ) {
super( spec );
}
setFromSpecification( spec ) {
this.halfBreadths = spec.halfBreadths;
//this.buttockHeights = spec.buttockHeights;
this.attributes = spec.attributes; //this could/should include LOA, BOA, Depth
this.levelsNeedUpdate = true;
this.style = spec.style || {};
return this;
}
getWeight( designState ) {
let ha = this.attributes;
let B = ha.BOA;
let D = ha.Depth;
let cp = designState.calculationParameters;
let K = cp.K;
let L = cp.LWL_design;
let T = cp.Draft_design;
let Cb = cp.Cb_design;
let vsm = 0.514444 * cp.speed; // Convert the design speed from knots to m/s
let Fn = vsm / Math.pow( 9.81 * L, 0.5 ); // Calculates Froude number
//This is not a good way to estimate the hull weight.
let parsons = parametricWeightHull( K, L, B, T, D, Cb, Fn );
parsons.mass *= 1000; //ad hoc conversion to kg, because the example K value is aimed at ending with tonnes.
let output = parsons;
//console.info("Hull weight:", output);
return output;
}
getStation( x ) {
let ha = this.attributes;
let xr = x / ha.LOA;
let sts = this.halfBreadths.stations;
let wls = this.halfBreadths.waterlines;
let tab = this.halfBreadths.table;
let { index: a, mu: mu } = bisectionSearch( sts, xr );
let st;
if ( a < 0 || a >= sts.length ) st = new Array( wls.length ).fill( null );
else if ( a + 1 === sts.length ) st = tab.map( row => row[ sts.length - 1 ] );
else {
st = [];
for ( let j = 0; j < wls.length; j ++ ) {
let after = tab[ j ][ a ];
let forward = tab[ j ][ a + 1 ];
if ( ( after === null || isNaN( after ) ) && ( forward === null || isNaN( forward ) ) ) {
st.push( null );
} else {
//Simply correcting by "|| 0" is not consistent with what is done in getWaterline. It may be better to correct upper nulls by nearest neighbor below.
st.push( lerp( after || 0, forward || 0, mu ) );
}
}
}
for ( let j = 0; j < this.halfBreadths.waterlines.length; j ++ ) {
st[ j ] *= 0.5 * ha.BOA;
if ( isNaN( st[ j ] ) || st[ j ] === null ) st[ j ] = null;
}
return st;
}
getWaterline( z ) {
let ha = this.attributes;
let zr = z / ha.Depth; //using zr requires fewer operations and less memory than a scaled copy of wls.
let wls = this.halfBreadths.waterlines;//.map(wl=>wl*ha.Depth);
let sts = this.halfBreadths.stations;
let tab = this.halfBreadths.table;
if ( zr < wls[ 0 ] ) {
//console.warn("getWaterLine: z below lowest defined waterline. Defaulting to all zero offsets.");
return new Array( sts.length ).fill( 0 );
} else {
let a, mu;
if ( zr > wls[ wls.length - 1 ] ) {
//console.warn("getWaterLine: z above highest defined waterline. Proceeding with highest data entries.");
a = wls.length - 2; //if this level is defined...
mu = 1;
//wl = tab[a].slice();
} else {
( { index: a, mu: mu } = bisectionSearch( wls, zr ) );
if ( a === wls.length - 1 ) {
a = wls.length - 2;
mu = 1;
}
}
//Try to do linear interpolation between closest data waterlines, but handle null values well:
let wl = new Array( sts.length );
for ( let j = 0; j < wl.length; j ++ ) {
let lower, upper;
let b = a;
//Find lower value for interpolation
if ( tab[ b ][ j ] !== null && ! isNaN( tab[ b ][ j ] ) ) {
lower = tab[ b ][ j ];
} else {
b = a + 1;
while ( b < wls.length && ( isNaN( tab[ b ][ j ] ) || tab[ b ][ j ] === null ) ) {
b ++;
}
if ( b !== wls.length ) {
//Inner NaN
lower = 0;
} else {
//Upper NaN, search below:
b = a - 1;
while ( b >= 0 && ( isNaN( tab[ b ][ j ] ) || tab[ b ][ j ] === null ) ) {
b --;
}
if ( b === - 1 ) {
//No number found:
lower = 0;
upper = 0;
} else {
lower = tab[ b ][ j ];
upper = lower;
}
}
}
//Find upper value for interpolation
let c = a + 1;
if ( upper !== undefined ) { /*upper found above*/ } else if ( tab[ c ][ j ] !== null && ! isNaN( tab[ c ][ j ] ) ) {
upper = tab[ c ][ j ];
} else {
//The cell value is NaN.
//Upper is not defined.
//That means either tab[a][j] is a number
//or tab[a][j] is an inner NaN and
//there exists at least one number above it.
//In both cases I have to check above a+1.
c = a + 2;
while ( c < wls.length && ( isNaN( tab[ c ][ j ] ) || tab[ c ][ j ] === null ) ) {
c ++;
}
if ( c === wls.length ) upper = lower;
else {
upper = tab[ c ][ j ];
}
}
//Linear interpolation
wl[ j ] = lerp( lower, upper, mu );
//Scale numerical values
if ( wl[ j ] !== null && ! isNaN( wl[ j ] ) ) wl[ j ] *= 0.5 * ha.BOA;
}
return wl;
}
}
//typically deck bounds
waterlineCalculation( z, bounds ) {
let { minX, maxX, minY, maxY } = bounds || {};
//console.group/*Collapsed*/("waterlineCalculation.");
//console.info("Arguments: z=", z, " Boundaries: ", arguments[1]);
let wl = this.getWaterline( z );
//console.info("wl: ", wl); //DEBUG
let LOA = this.attributes.LOA;
let sts = this.halfBreadths.stations.slice();
for ( let i = 0; i < sts.length; i ++ ) {
sts[ i ] *= LOA;
}
let hasMinX = ( minX !== undefined ) && minX !== sts[ 0 ];
let hasMaxX = ( maxX !== undefined ) && maxX !== sts[ sts.length - 1 ];
if ( hasMinX || hasMaxX ) {
let first = 0;
let wlpre;
if ( hasMinX ) {
let muf;
( { index: first, mu: muf } = bisectionSearch( sts, minX ) );
let lower = wl[ first ];
let upper = wl[ first + 1 ];
if ( ( lower === null || isNaN( lower ) ) && ( upper === null || isNaN( upper ) ) ) {
wlpre = null;
} else {
wlpre = lerp( lower || 0, upper || 0, muf );
}
}
let last = sts.length - 1;
let wlsuff;
if ( hasMaxX ) {
let mul;
( { index: last, mu: mul } = bisectionSearch( sts, maxX ) );
let lower = wl[ last ];
let upper = wl[ last + 1 ];
if ( ( lower === null || isNaN( lower ) ) && ( upper === null || isNaN( upper ) ) ) {
wlsuff = null;
} else {
wlsuff = lerp( lower || 0, upper || 0, mul );
}
}
//Add virtual entries according to specified boundaries:
sts = sts.slice( first + 1, last + 1 );
wl = wl.slice( first + 1, last + 1 );
if ( hasMinX ) {
sts.unshift( minX );
wl.unshift( wlpre );
}
if ( hasMaxX ) {
sts.push( maxX );
wl.push( wlsuff );
}
}
//This does not yet account properly for undefined minY, maxY.
let port = [], star = [];
for ( let i = 0; i < wl.length; i ++ ) {
if ( wl[ i ] === null || isNaN( wl[ i ] ) ) {
star[ i ] = minY || null;
port[ i ] = maxY || null;
} else {
star[ i ] = Math.max( - wl[ i ], minY || - wl[ i ] );
port[ i ] = Math.min( wl[ i ], maxY || wl[ i ] );
}
}
//DEBUG
//console.info("Arguments to sectionCalculation:", sts, star, port);
//sectionCalculation can potentially be served some nulls.
let sc = sectionCalculation( { xs: sts, ymins: star, ymaxs: port } );
let LWL = sc.maxX - sc.minX;
let BWL = sc.maxY - sc.minY;
let Cwp = sc.A / ( LWL * BWL );
let APP = this.attributes.APP || sc.minX;
let FPP = this.attributes.FPP || sc.maxX;
let LBP = FPP - APP;
let output = {
z: z,
xc: sc.xc,
yc: sc.yc,
Awp: sc.A,
Ix: sc.Ix,
Iy: sc.Iy,
maxX: sc.maxX,
minX: sc.minX,
maxY: sc.maxY,
minY: sc.minY,
Cwp: Cwp,
LWL: LWL,
LBP: LBP,
BWL: BWL
};
//console.info("Output from waterlineCalculation: ", output);
//console.groupEnd();
return output;
}
stationCalculation( x, maxZ ) {
let wls = this.halfBreadths.waterlines.map( wl => this.attributes.Depth * wl );
let port = this.getStation( x );
if ( maxZ !== null && ! isNaN( maxZ ) ) {
let { index, mu } = bisectionSearch( wls, maxZ );
if ( index < wls.length - 1 ) {
wls[ index + 1 ] = lerp( wls[ index ], wls[ index + 1 ], mu );
port[ index + 1 ] = lerp( port[ index ], port[ index + 1 ], mu );
wls = wls.slice( 0, index + 2 );
port = port.slice( 0, index + 2 );
}
}
let star = port.map( hb => - hb );
let sc = sectionCalculation( { xs: wls, ymins: star, ymaxs: port } );
return {
x: x, //or xc? or cg.. Hm.
yc: sc.yc,
zc: sc.xc,
A: sc.A,
Iz: sc.Ix,
Iy: sc.Iy,
maxZ: sc.maxX,
minZ: sc.minX,
maxY: sc.maxY,
minY: sc.minY
};
}
calculateAttributesAtDraft( T ) {
function levelCalculation( hull,
z,
prev = {
z: 0,
Vs: 0,
Vbb: 0,
As: 0,
minX: 0,
maxX: 0,
minY: 0,
maxY: 0,
prMinY: 0,
prMaxY: 0,
Ap: 0,
Cv: { x: 0, y: 0, z: 0 }
} ) {
let wlc = hull.waterlineCalculation( z, {} );
let lev = {};
Object.assign( lev, wlc );
//Projected area calculation (approximate):
lev.prMinY = wlc.minY;
lev.prMaxY = wlc.maxY;
//DEBUG:
//console.info("prev.Ap = ", prev.Ap);
//console.info("Parameters to trapezoidCalculation: (%.2f, %.2f, %.2f, %.2f, %.2f, %.2f)", prev.prMinY, prev.prMaxY, lev.prMinY, lev.prMaxY, prev.z, z);
let AT = trapezoidCalculation( prev.prMinY, prev.prMaxY, lev.prMinY, lev.prMaxY, prev.z, z )[ "A" ];
//console.log("Calculated area of trapezoid: ", AT);
lev.Ap = prev.Ap + AT;
//lev.Ap = prev.Ap
// + trapezoidCalculation(prev.prMinY, prev.prMaxY, lev.prMinY, lev.prMaxY, prev.z, z)["A"];
//DEBUG END
//level bounds are for the bounding box of the submerged part of the hull
if ( wlc.minX !== null && ! isNaN( wlc.minX ) && wlc.minX <= prev.minX )
lev.minX = wlc.minX;
else
lev.minX = prev.minX;
if ( wlc.maxX !== null && ! isNaN( wlc.maxX ) && wlc.maxX >= prev.maxX )
lev.maxX = wlc.maxX;
else
lev.maxX = prev.maxX;
if ( wlc.minY !== null && ! isNaN( wlc.minY ) && wlc.minY <= prev.minY )
lev.minY = wlc.minY;
else
lev.minY = prev.minY;
if ( wlc.maxY !== null && ! isNaN( wlc.maxY ) && wlc.maxY >= prev.maxY )
lev.maxY = wlc.maxY;
else
lev.maxY = prev.maxY;
lev.Vbb = ( lev.maxX - lev.minX ) * ( lev.maxY - lev.minY ) * z;
//Keep level maxX and minX for finding end cap areas:
lev.maxXwp = wlc.maxX;
lev.minXwp = wlc.minX;
//Find bilinear patches in the slice, and combine them.
//Many possibilities for getting the coordinate systems wrong.
let calculations = [];
let sts = hull.halfBreadths.stations.map( st => st * hull.attributes.LOA );
let wl = hull.getWaterline( z );
let prwl = hull.getWaterline( prev.z );
for ( let j = 0; j < sts.length - 1; j ++ ) {
let port =
patchColumnCalculation( sts[ j ], sts[ j + 1 ], prev.z, z, - prwl[ j ], - wl[ j ], - prwl[ j + 1 ], - wl[ j + 1 ] );
calculations.push( port );
let star =
patchColumnCalculation( sts[ j ], sts[ j + 1 ], prev.z, z, prwl[ j ], wl[ j ], prwl[ j + 1 ], wl[ j + 1 ] );
calculations.push( star );
}
//console.log(calculations); //DEBUG
let C = combineVolumes( calculations );
//Cv of slice. Note that switching of yz must
//be done before combining with previous level
let Cv = { x: C.Cv.x, y: C.Cv.z, z: C.Cv.y };
lev.Vs = prev.Vs + C.V; //hull volume below z
lev.As = prev.As + C.As; //outside surface below z
//End caps:
if ( lev.minXwp <= sts[ 0 ] )
lev.As += hull.stationCalculation( lev.minXwp, z )[ "A" ];
if ( lev.maxXwp >= sts[ sts.length - 1 ] )
lev.As += hull.stationCalculation( lev.maxXwp, z )[ "A" ];
//center of volume below z (some potential for accumulated rounding error when calculating an accumulated average like this):
lev.Cv = Vectors.scale( Vectors.add(
Vectors.scale( prev.Cv, prev.Vs ),
Vectors.scale( Cv, C.V )
), 1 / ( lev.Vs || 2 ) );
lev.Cb = lev.Vs / lev.Vbb;
lev.Cp = lev.Vs / ( lev.Ap * ( lev.maxX - lev.minX ) );
return lev;
}
if ( T === null || isNaN( T ) ) {
console.error( "Hull.prototype.calculateAttributesAtDraft(T): No draft specified. Returning undefined." );
return;
} else if ( T < 0 || T > this.attributes.Depth ) {
console.error( "Hull.prototype.calculateAttributesAtDraft(T): Draft parameter " + T + "outside valid range of [0,Depth]. Returning undefined." );
}
let wls = this.halfBreadths.waterlines.map( wl => this.attributes.Depth * wl );
// new ES6
//This is the part that can be reused as long as the geometry remains unchanged:
if ( this.levelsNeedUpdate ) {
this.levels = [];
for ( let i = 0; i < wls.length; i ++ ) {
let z = wls[ i ];
let lev = levelCalculation( this, z, this.levels[ i - 1 ] );
//Bottom cap, only on the lowest level:
if ( i === 0 ) {
lev.As += lev.Awp;
}
this.levels.push( lev );
}
this.levelsNeedUpdate = false;
}
//Find highest data waterline below or at water level:
let { index, mu } = bisectionSearch( wls, T );
//console.info("Highest data waterline below or at water level: " + index);
//console.log(this.levels);
let lc;
if ( mu === 0 ) lc = this.levels[ index ];
else lc = levelCalculation( this, T, this.levels[ index ] );
//Filter and rename for output
return {
xcwp: lc.xc, //water plane values
LCF: lc.xc,
ycwp: lc.yc,
TCF: lc.yc,
Awp: lc.Awp,
Ixwp: lc.Ix,
BMt: lc.Ix / lc.Vs,
Iywp: lc.Iy,
BMl: lc.Iy / lc.Vs,
maxXs: lc.maxX, //boundaries of the submerged part of the hull
minXs: lc.minX,
maxYs: lc.maxY,
minYs: lc.minY,
Cwp: lc.Cwp,
LWL: lc.LWL,
LBP: lc.LBP,
BWL: lc.BWL,
Ap: lc.Ap, //projected area in length direction
Cp: lc.Cp, //prismatic coefficient
//Vbb: lc.Vbb,
Vs: lc.Vs, //volume of submerged part of the hull
Cb: lc.Cb,
Cm: lc.Cb / lc.Cp,
As: lc.As, //wetted area
Cv: lc.Cv, //center of buoyancy
LCB: lc.Cv.x,
TCB: lc.Cv.y,
KB: lc.Cv.z
};
}
// }()
//M is the mass (in kg) of the ship
calculateDraftAtMass( M, epsilon = 0.001, rho = 1025 ) {
let VT = M / rho; //Target submerged volume (1025=rho_seawater)
//Interpolation:
let a = 0;
let b = this.attributes.Depth; //depth is not draft ¿?
let t = 0.5 * b;
//Souce: https://en.wikipedia.org/wiki/Secant_method
// Secant Method to Find out where is the zero point
// Used to find out the Draft but can be generalized
let V1 = 0 - VT;
let V2 = VT; //Just inserting V2 an ordinary value to not have to calculate it twice
let n = 0;
while ( Math.abs( t - a ) > epsilon ) {
//This following condition force just receive draft from [0;Depth]
if ( t > b ) {
t = b;
}
V2 = this.calculateAttributesAtDraft( t )[ "Vs" ] - VT;
// debugger
let dx = ( V2 - V1 ) / ( t - a );
if ( dx > 0.1 || dx < - 0.1 ) {
a = t;
V1 = V2;
t = t - V2 / dx;
//In case the derived of function is close to 0 we can follow the Bisection method
//Source: https://en.wikipedia.org/wiki/Bisection_method
} else {
let ts = 0.5 * ( a + t ); //intermediate point
let Vs = this.calculateAttributesAtDraft( ts )[ "Vs" ] - VT; //this values must be calculated twice, see better example
if ( Vs > 0 ) {
t = ts;
V2 = Vs;
} else {
a = ts;
V1 = Vs;
}
}
}
return t;
}
getSpecification() {
return {
halfBreadths: this.halfBreadths,
//buttockHeights: this.buttockHeights
attributes: this.attributes,
style: this.style
};
}
}