`Macros to model downward pull of anchors using added weights
`and to solve for equilibrium accounting for change in anchor weight
`with change in waterplane.
`Note: Run RIG.RF first to create tlp.gf or download here.
`
`Note: This can be done more easily using negative ground points!

`Define and set variables to represent anchor-to-ship attachment points:
MACRO AnchAtt `%1=anchor number, %2,%3,%4=attachment location on ship
variables anch%1l, anch%1t, anch%1v
SET anch%1l = %2
SET anch%1t = %3
SET anch%1v = %4
/

`Define and set variables to model anchor vertical force functions:
`Force function is f = c + k*h
`where h is the height of the point of attachment above the waterplane.
MACRO AnchFPar `%1=anchor number, %2=k, %3=c, %4=h0
variable anch%1k, anch%1c
SET anch%1k = %2 `Delta force / delta height
SET anch%1c = %3 `Force at zero height
/

`Create or modify a weight for the given anchor:
MACRO AnchWt `%1=anchor number
part hull
refpt = {anch%1l}, {anch%1t}, {anch%1v}
variable f
SET f = {anch%1k} MULT {HEIGHT} PLUS {anch%1c}
ADD "Anchor %1 downward force" {f}, {anch%1l}, {anch%1t}, {anch%1v} /nowarn
/

`Saves present waterplane for future reference:
MACRO WplSave
variable heel0,trim0,depth0
SET heel0 = {HEEL}
SET trim0 = {TRIM}
SET depth0 = {DEPTH}
/

`Checks a given waterplane parameter present value against the saved value.
`Reduces the control variable by one if the difference is small.
MACRO WplChk
variable d
SET d = {%1} MINUS {%10}
SET d = ABS {d}
IF {d} < 0.01 THEN SET solving={solving} MINUS 1
/

`Sets anchor forces for current waterplane and finds new waterplane:
MACRO AnchSol1
IF {solving}=0 THEN EXIT
.AnchWts
SET solving=3
.WplSave
SOLVE
.WplChk HEEL
.WplChk TRIM
.WplChk DEPTH
/

`This assumes four anchors -- redefine this macro if using a different number.
MACRO AnchWts
.AnchWt 1
.AnchWt 2
.AnchWt 3
.AnchWt 4
/

variable solving

`--------------------------------------

MACRO AnchTest

read tlp.gf
depth = 55
vcg = 60
solve weight, lcg

MACRO AnchWts
.AnchWt 1
.AnchWt 2
.AnchWt 3
.AnchWt 4
//

`Define anchor line attachment points:
.AnchAtt 1, 50.0, 50.0p, 50
.AnchAtt 2, 50.0, 50.0s, 50
.AnchAtt 3,-50.0, 50.0p, 50
.AnchAtt 4,-50.0, 50.0s, 50

`Set vertical force function parameters:
     `anch no df/dh f0
.AnchFPar 1, 30, 500
.AnchFPar 2, 30, 500
.AnchFPar 3, 30, 500
.AnchFPar 4, 30, 500

`Solve for equilibrium condition:
set solving=3
.AnchSol1 (10) `Repeat solve process up to given iteration limit
STATUS GHS `Produce status report
/

.AnchTest