`Demonstration of the use of variables and variable arithmetic.
`Finds minimum GM according to 46 CFR 170.170.
`Assumes that the wind plane is geometrically modeled.
`Works with GHS and BHS.

`A library file, 170170.LIB is available which implements these calculations.
`See the the ghsport.com Wizards section. 

`Rev 10/10/96 to allow varying trim
`Rev  5/01/02 to accomodate added freeboard per USCG Marine Safety manual
`Rev  1/26/04 restores original TCG

project reqgm170

variable d    `current draft
variable di   `draft increment
variable f    `freeboard
variable fadd `added freeboard
variable fttl `total freeboard
variable hf   `half freeboard
variable h    `heel angle
variable dh   `change to apply to heel angle
variable th   `tangent of heel angle
variable ha   `heeling arm
variable t    `temporary
variable gm   `gm required

`Macro to be used for the heel iteration to find angle of half freeboard:
`(Note:  This is simple and inefficient, but it runs quickly enough.)
macro tryheel
  heel={h}
  fix heel | solve | vary heel
  set t = {HEIGHT} div 2
  `\ heel = {heel}   dh = {dh}   height {HEIGHT}   half fbd = {t}
  if {t}<0 then set h={h} minus {dh} else set h={h} plus {dh}
  set dh={dh} mult 0.75
/
macro trueheel
  set dh = 7
  set h = {dh}
  .tryheel (20)
  set h = {h} div 2
/
macro setheel
  set t = {hf} div {tfb}
  set h = atan {t}
/

macro reqgm  `Takes trim as a parameter
  \\ Draft: {d:2}   Trim: %1
  hmmt off
  heel = 0
  trim = %1
  draft = {d}
  part {dpart}
  ref = {lfb}, {tfb}, {vfb}
  vcg = {d} `a nominal value
  solve weight, lcg, tcg

  set f = {HEIGHT}
  set fadd = {bo4} minus {f} times {aob}
  if {fadd} < 0 then set fadd = 0
  set t = {vfb} plus {fadd}
  ref = {lfb}, {tfb}, {t}

  set fttl = {f} plus {fadd}
  \       Freeboard: {f:2}   Added freeboard: {fadd:2}  Total freeboard: {fttl:2}
  set hf = {fttl} div 2
  \       Half freeboard: {hf:2}

  if {true}<>0 then .trueheel else .setheel
  if {h}>14 then set h = 14
  heel = {h}
  \       Heel angle to half freeboard or 14 degrees: {HEEL:1}
  set th = tan {HEEL}
  \       Using heel angle: {HEEL:1}  tangent: {th:4}

  heel = 0
  hmmt wind
  set t = {FTCG}  `save original tcg
  fix heel | solve | vary heel
  solve tcg
  set ha = {t} minus {FTCG}
  \       Heeling arm: {ha}
  set gm = {ha} div {th}
  \ ----> Required GM: {gm:2}
  tcg = {t} `Restore TCG
  set d = {d} plus {di}
/


variable bo4  `Beam/4 for the purpose of added freeboard (USCG Marine Safety manual 6.B.4)
variable aob  `applicable fraction of total superstructure (a/b) for added freeboard
variable lfb,tfb,vfb `Coordinates (l,t,v) of lowest point on freeboard deck
variable true `1 if true heel at half freeboard is to be found, 0 otherwise
variable (string) dpart

`------ Run the calculations -------

read fvw.gf
report

set dpart="HULL" `Any displacer part in the model

set bo4=0, aob=0 `Use zero for no added freeboard

set true = 1 `Use 0 to approximate half-freeboard tangent as hf / beam
             `Use 1 to find true half-freeboard angle

set lfb = 0, tfb = 12.27, vfb = 12.58  `Lowest point on freeboard deck

wind (pressure) = 0.005

`fix trim  `<--- optional

set di = 1  `draft increment
set d = 6   `initial draft
.reqgm (4) 0  `(number of drafts) trim
set d = 6
.reqgm (4) 1
view
report off
end