<commit_before>module Drasil.SSP.GenDefs (normForcEq, bsShrFEq, resShr, mobShr,
  normShrR, momentEql, generalDefinitions,
  normForcEqGD, bsShrFEqGD, resShrGD, mobShrGD, normShrRGD, momentEqlGD,
  mobShearWOGD, resShearWOGD, srfWtrFGD) where

import Prelude hiding (sin, cos, tan)
import Language.Drasil
import Theory.Drasil (GenDefn, gd)

import Drasil.DocLang.SRS as SRS (physSyst)

import Data.Drasil.Utils (weave)

import Data.Drasil.SI_Units (metre, newton)

import Data.Drasil.Concepts.Documentation (analysis, assumption, component,
  constant, definition, method_, value)
import Data.Drasil.Concepts.Math (area, equation)
import Data.Drasil.Concepts.PhysicalProperties (len)
import Data.Drasil.Concepts.Physics (distance, pressure, twoD, weight)
import Data.Drasil.Concepts.SolidMechanics (normForce, shearForce)
import Data.Drasil.Quantities.Physics (displacement, force, torque)
import Data.Drasil.Theories.Physics (weightGD, torqueDD)

import Data.Drasil.SentenceStructures (foldlSent, foldlSent_, foldlSentCol, 
  getTandS, isThe, ofThe, sAnd, sIs, sOf, andThe)

import Drasil.SSP.Assumptions (assumpFOSL, assumpSLH, assumpSP, assumpSLI,
  assumpINSFL, assumpPSC, assumpSBSBISL, assumpWIBE, assumpWISE, assumpNESSS,
  assumpHFSM)
import Drasil.SSP.BasicExprs (eqlExpr, eqlExprN, momExpr)
import Drasil.SSP.DataDefs (intersliceWtrF, angleA, angleB, lengthB, lengthLb, 
  slcHeight, stressDD, ratioVariation)
import Drasil.SSP.Defs (intrslce, slice, slope, slopeSrf, slpSrf, soil, 
  soilPrpty, waterTable)
import Drasil.SSP.Figures (fig_forceacting)
import Drasil.SSP.References (chen2005, fredlund1977, karchewski2012)
import Drasil.SSP.TMods (factOfSafety, equilibrium, mcShrStrgth, effStress)
import Drasil.SSP.Unitals (baseAngle, baseHydroForce, baseLngth, baseWthX, 
  dryWeight, earthqkLoadFctr, effCohesion, fricAngle, fs, genericA, genericSpWght, impLoadAngle, intNormForce, intShrForce, 
  index, inxi, inxiM1, midpntHght, mobShrI, momntArm, momntOfBdy, normToShear, 
  nrmFSubWat, rotForce, satWeight, scalFunc, shearFNoIntsl, shrResI, shrResI, 
  shrStress, totNrmForce, shearRNoIntsl, shrResI, slcWght, sliceHght, sliceHghtW, slipHght,
  slopeHght, surfHydroForce, surfAngle, surfLoad, watrForce, waterHght, waterWeight, 
  dryVol, satVol, waterVol, yi, zcoord)

---------------------------
--  General Definitions  --
---------------------------
generalDefinitions :: [GenDefn]
generalDefinitions = [normForcEqGD, bsShrFEqGD, resShrGD, mobShrGD,
 effNormFGD, resShearWOGD, mobShearWOGD, normShrRGD, momentEqlGD, weightGD, 
 sliceWghtGD, baseWtrFGD, srfWtrFGD]

normForcEqGD, bsShrFEqGD, resShrGD, mobShrGD, effNormFGD, resShearWOGD, 
  mobShearWOGD, normShrRGD, momentEqlGD, sliceWghtGD, baseWtrFGD, 
  srfWtrFGD :: GenDefn
normForcEqGD = gd normForcEq (getUnit totNrmForce)   [nmFEq_deriv]    
  [makeCite chen2005]                      "normForcEq"  [nmFEq_desc]
bsShrFEqGD   = gd bsShrFEq   (getUnit mobShrI)       [bShFEq_deriv]
  [makeCite chen2005]                      "bsShrFEq"    [bShFEq_desc]
resShrGD     = gd resShr     (getUnit shrResI)       [resShr_deriv]   
  [makeCite chen2005]                      "resShr"      [resShr_desc]
mobShrGD     = gd mobShr     (getUnit mobShrI)       [mobShr_deriv]   
  [makeCite chen2005]                      "mobShr"      [mobShr_desc]
effNormFGD   = gd effNormF   (getUnit nrmFSubWat)    [effNormF_deriv] 
  [makeCite chen2005]                      "effNormF"    [effNormF_desc]
resShearWOGD = gd resShearWO (getUnit shearRNoIntsl) []         
  (map makeCite[chen2005, karchewski2012]) "resShearWO"  [resShearWO_desc]
mobShearWOGD = gd mobShearWO (getUnit shearFNoIntsl) []
  (map makeCite[chen2005, karchewski2012]) "mobShearWO"  [mobShearWO_desc]
normShrRGD   = gd normShrR   (getUnit intShrForce)   [] 
  [makeCite chen2005]                      "normShrR"    [nmShrR_desc]
momentEqlGD  = gd momentEql  (Just newton)           momEqlDeriv
  [makeCite chen2005]                      "momentEql"   [momEql_desc]
sliceWghtGD  = gd sliceWght  (getUnit slcWght)       sliceWghtDeriv
  [makeCite fredlund1977]                  "sliceWght"   [sliceWghtNotes]
baseWtrFGD   = gd baseWtrF   (getUnit baseHydroForce) bsWtrFDeriv
  [makeCite fredlund1977]                  "baseWtrF"    [bsWtrFNotes]
srfWtrFGD    = gd srfWtrF    (getUnit surfHydroForce) srfWtrFDeriv   
  [makeCite fredlund1977]                  "srfWtrF"     [srfWtrFNotes]

--
normForcEq :: RelationConcept
normForcEq = makeRC "normForcEq" (nounPhraseSP "normal force equilibrium")
  nmFEq_desc nmFEq_rel

nmFEq_rel :: Relation
nmFEq_rel = inxi totNrmForce $= eqlExprN cos sin
  (\x y -> x - inxiM1 intShrForce + inxi intShrForce + y)

nmFEq_desc :: Sentence
nmFEq_desc = foldlSent [S "This equation satisfies", makeRef2S equilibrium +:+.
  S "in the normal direction", ch slcWght, S "is defined in", 
  makeRef2S sliceWghtGD `sC` ch surfHydroForce,  S "is defined in", 
  makeRef2S srfWtrFGD `sC` ch surfAngle, S "is defined in",
  makeRef2S angleB `sC` S "and", ch baseAngle, S "is defined in", 
  makeRef2S angleA]

nmFEq_deriv :: Sentence
nmFEq_deriv = foldlSent [at_start normForcEq, S "is derived from the free",
  S "body diagram of", makeRef2S fig_forceacting, S "in", 
  (makeRef2S $ SRS.physSyst ([]::[Contents]) ([]::[Section]))]

--
bsShrFEq :: RelationConcept
bsShrFEq = makeRC "bsShrFEq" (nounPhraseSP "base shear force equilibrium")
  bShFEq_desc bShFEq_rel

bShFEq_rel :: Relation
bShFEq_rel = inxi mobShrI $= eqlExpr sin cos
  (\x y -> x - inxiM1 intShrForce + inxi intShrForce + y)

bShFEq_desc :: Sentence
bShFEq_desc = foldlSent [S "This equation satisfies", makeRef2S equilibrium +:+.
  S "in the shear direction", ch slcWght, S "is defined in", 
  makeRef2S sliceWghtGD `sC` ch surfHydroForce, S "is defined in", 
  makeRef2S srfWtrFGD `sC` ch surfAngle, S "is defined in",
  makeRef2S angleB `sC` S "and", ch baseAngle, S "is defined in", 
  makeRef2S angleA]

bShFEq_deriv :: Sentence
bShFEq_deriv = foldlSent [at_start bsShrFEq, S "is derived from the free",
  S "body diagram of", makeRef2S fig_forceacting, S "in", 
  (makeRef2S $ SRS.physSyst ([]::[Contents]) ([]::[Section]))]

--
shrResEqn :: Expr
shrResEqn = inxi nrmFSubWat * tan (inxi fricAngle) + inxi effCohesion *
  inxi baseLngth

resShr :: RelationConcept
resShr = makeRC "resShr" (nounPhraseSP "resistive shear force")
  resShr_desc resShr_rel -- genDef3Label

resShr_rel :: Relation
resShr_rel = inxi shrResI $= shrResEqn

resShr_desc :: Sentence
resShr_desc = foldlSent_ [ch baseLngth, S "is defined in" +:+. 
  makeRef2S lengthLb]

resShr_deriv :: Sentence
resShr_deriv = foldlSent_ [S "Derived by substituting", makeRef2S stressDD,
  S "into the Mohr-Coulomb", phrase shrStress `sC` makeRef2S mcShrStrgth `sC`
  S "and multiplying both sides of the", phrase equation, S "by", 
  phrase genericA `ofThe` phrase slice, S "in the shear-" :+: ch zcoord  +:+. 
  S "plane", S "Since the", phrase slope, S "is assumed to extend infinitely",
  S "in the", ch zcoord :+: S "-direction", sParen (makeRef2S assumpPSC) `sC` 
  S "the resulting", plural force, S "are expressed per", phrase metre,
  S "in the", ch zcoord :+: S "-direction.", S "The", 
  getTandS fricAngle `andThe` getTandS effCohesion, S "are not indexed by",
  ch index, S "becaused they are assumed to be isotropic", 
  sParen (makeRef2S assumpSLI) `andThe` phrase soil, S "is assumed to be",
  S "homogeneous, with", phrase constant, plural soilPrpty, S "throughout" +:+.
  sParen (makeRef2S assumpSLH `sC` makeRef2S assumpSP)]

--
mobShr :: RelationConcept
mobShr = makeRC "mobShr"
  (nounPhraseSP "mobilized shear force") mobShr_desc mobShr_rel -- genDef4Label

mobShr_rel :: Relation
mobShr_rel = inxi mobShrI $= inxi shrResI / sy fs $= shrResEqn / sy fs

mobShr_desc :: Sentence
mobShr_desc = foldlSent_ [ch baseLngth, S "is defined in" +:+. 
  makeRef2S lengthLb]

mobShr_deriv :: Sentence
mobShr_deriv = foldlSent_ [at_start mobShrI, S "is derived by dividing",
  phrase definition `ofThe` ch shrResI, S "from" +:+. makeRef2S resShrGD,
  S "by", phrase definition `ofThe` phrase fs, S "from" +:+.
  makeRef2S factOfSafety, S "The", getTandS fs, S "is not indexed by", ch index,
  S "because it is assumed to be", phrase constant, S "for the entire",
  phrase slpSrf +:+. sParen (makeRef2S assumpFOSL)]

--
effNormF :: RelationConcept
effNormF = makeRC "effNormF"
  (nounPhraseSP "effective normal force") effNormF_desc effNormF_rel

effNormF_rel :: Relation
effNormF_rel = inxi nrmFSubWat $= inxi totNrmForce - inxi baseHydroForce

effNormF_desc :: Sentence
effNormF_desc = ch baseHydroForce +:+ S "is defined in" +:+. makeRef2S baseWtrFGD

effNormF_deriv :: Sentence
effNormF_deriv = foldlSent [
  S "Derived by substituting", makeRef2S stressDD, S "into", 
  makeRef2S effStress `sAnd` S "multiplying both sides of the", phrase equation,
  S "by the", phrase genericA `ofThe` phrase slice, S "in the shear-" :+: 
  ch zcoord +:+. S "plane", S "Since the", phrase slope, 
  S "is assumed to extend infinitely in the", ch zcoord :+: S "-direction", 
  sParen (makeRef2S assumpPSC) `sC` S "the resulting", plural force, S "are expressed per", phrase metre, S "in the", ch zcoord :+: S "-direction" ]

--
normShrR :: RelationConcept
normShrR = makeRC "normShrR"
  (nounPhraseSP "interslice normal and shear force proportionality") 
  nmShrR_desc nmShrR_rel -- genDef5Label

nmShrR_rel :: Relation
nmShrR_rel = sy intShrForce $= sy normToShear * sy scalFunc * sy intNormForce

nmShrR_desc :: Sentence
nmShrR_desc = foldlSent [S "Mathematical representation of the primary", 
  phrase assumption, S "for the Morgenstern-Price", phrase method_ +:+.
  sParen (makeRef2S assumpINSFL), ch scalFunc, S "is defined in", 
  makeRef2S ratioVariation]

--
resShearWO :: RelationConcept
resShearWO = makeRC "resShearWO"
  (nounPhraseSP "resistive shear force, without interslice normal and shear forces") resShearWO_desc resShearWO_rel

resShearWO_rel :: Relation
resShearWO_rel = inxi shearRNoIntsl $= 
  (((inxi slcWght) + (inxi surfHydroForce) * (cos (inxi surfAngle))) *
  (cos (inxi baseAngle)) + (negate (inxi watrForce) + (inxiM1 watrForce) + 
  (inxi surfHydroForce) * sin (inxi surfAngle)) * (sin (inxi baseAngle)) -
  (inxi baseHydroForce)) * tan (inxi fricAngle) + (inxi effCohesion) *
  (inxi baseLngth)

resShearWO_desc :: Sentence
resShearWO_desc = foldlSent_ [ch slcWght, S "is defined in", 
  makeRef2S sliceWghtGD `sC` ch surfHydroForce, S "is defined in", 
  makeRef2S srfWtrFGD `sC` ch surfAngle, S "is defined in", 
  makeRef2S angleB `sC` ch baseAngle, S "is defined in",
  makeRef2S angleA `sC` ch watrForce, S "is defined in",
  makeRef2S intersliceWtrF `sC` ch baseHydroForce, S "is defined in",
  makeRef2S baseWtrFGD `sC` S "and", ch baseLngth, S "is defined in" +:+. 
  makeRef2S lengthLb]

--
--
mobShearWO :: RelationConcept
mobShearWO = makeRC "mobShearWO"
  (nounPhraseSP "mobilized shear force, without interslice normal and shear forces") mobShearWO_desc mobShearWO_rel

mobShearWO_rel :: Relation
mobShearWO_rel = inxi shearFNoIntsl $= ((inxi slcWght) + (inxi surfHydroForce) *
  (cos (inxi surfAngle))) * (sin (inxi baseAngle)) - (negate (inxi watrForce) + 
  (inxiM1 watrForce) + (inxi surfHydroForce) * sin (inxi surfAngle)) * (cos (inxi baseAngle))

mobShearWO_desc :: Sentence
mobShearWO_desc = foldlSent_ [ch slcWght, S "is defined in", 
  makeRef2S sliceWghtGD `sC` ch surfHydroForce, S "is defined in", 
  makeRef2S srfWtrFGD `sC` ch surfAngle, S "is defined in", 
  makeRef2S angleB `sC` ch baseAngle, S "is defined in",
  makeRef2S angleA `sC` S "and", ch watrForce, S "is defined in" +:+.
  makeRef2S intersliceWtrF]

--

momentEql :: RelationConcept
momentEql = makeRC "momentEql" (nounPhraseSP "moment equilibrium")
  momEql_desc momEql_rel -- genDef6Label

momEql_rel :: Relation
momEql_rel = 0 $= momExpr (\ x y -> x +
  (inxi baseWthX / 2 * (inxi intShrForce + inxiM1 intShrForce)) + y)

momEql_desc :: Sentence
momEql_desc = foldlSent [S "This", phrase equation, S "satisfies", 
  makeRef2S equilibrium, S "for the net" +:+. phrase momntOfBdy, ch baseWthX,
  S "is defined in", makeRef2S lengthB `sC` ch baseAngle, S "is defined in",
  makeRef2S angleA `sC` ch slcWght, S "is defined in", 
  makeRef2S sliceWghtGD `sC` ch midpntHght, S "is defined in", 
  makeRef2S slcHeight `sC` ch surfHydroForce, S "is defined in", 
  makeRef2S srfWtrFGD `sC` S "and", ch surfAngle, S "is defined in", 
  makeRef2S angleB]

momEqlDeriv :: Derivation
momEqlDeriv = weave [momEqlDerivSentences, momEqlDerivEqns]

momEqlDerivSentences :: [Sentence]
momEqlDerivSentences = map foldlSentCol [momEqlDerivTorqueSentence, 
  momEqlDerivMomentSentence, momEqlDerivNormaliSentence, 
  momEqlDerivNormaliM1Sentence, momEqlDerivWateriSentence, 
  momEqlDerivWateriM1Sentence, momEqlDerivSheariSentence, 
  momEqlDerivSheariM1Sentence, momEqlDerivSeismicIntSentence, 
  momEqlDerivSeismicSentence, momEqlDerivSeismicWSentence, 
  momEqlDerivHydroSentence, momEqlDerivExtSentence, momEqlDerivFinalSentence]

momEqlDerivEqns :: [Sentence]
momEqlDerivEqns = map E [momEqlDerivTorqueEqn, momEqlDerivMomentEqn,
  momEqlDerivNormaliEqn, momEqlDerivNormaliM1Eqn, momEqlDerivWateriEqn, 
  momEqlDerivWateriM1Eqn, momEqlDerivSheariEqn, 
  momEqlDerivSheariM1Eqn, momEqlDerivSeismicIntEqn,
  momEqlDerivSeismicEqn, momEqlDerivSeismicWEqn,
  momEqlDerivHydroEqn, momEqlDerivExtEqn, 
  momEqlDerivFinalEqn]

momEqlDerivTorqueSentence, momEqlDerivMomentSentence,
  momEqlDerivNormaliSentence, momEqlDerivNormaliM1Sentence, 
  momEqlDerivWateriSentence, momEqlDerivWateriM1Sentence, 
  momEqlDerivSheariSentence, momEqlDerivSheariM1Sentence, 
  momEqlDerivSeismicIntSentence, momEqlDerivSeismicSentence, 
  momEqlDerivSeismicWSentence, momEqlDerivHydroSentence, 
  momEqlDerivExtSentence, momEqlDerivFinalSentence :: [Sentence]

momEqlDerivTorqueEqn, momEqlDerivMomentEqn,
  momEqlDerivNormaliEqn, momEqlDerivNormaliM1Eqn, momEqlDerivWateriEqn, 
  momEqlDerivWateriM1Eqn, momEqlDerivSheariEqn, 
  momEqlDerivSheariM1Eqn, momEqlDerivSeismicIntEqn,
  momEqlDerivSeismicEqn, momEqlDerivSeismicWEqn,
  momEqlDerivHydroEqn, momEqlDerivExtEqn, 
  momEqlDerivFinalEqn :: Expr

momEqlDerivTorqueSentence = [at_start momntOfBdy, S "is equal to", 
  phrase torque `sC` S "so the", phrase equation, S "from", makeRef2S torqueDD,
  S "will be used to calculate", plural momntOfBdy]

momEqlDerivMomentSentence = [S "Considering one dimension, with",
  plural momntOfBdy, S "in the clockwise direction as positive and", 
  plural momntOfBdy, S "in the counterclockwise direction as negative" `sC`
  S "and replacing the", phrase torque, S "symbol with the", phrase momntOfBdy, 
  S "symbol, the", phrase equation, S "simplifies to"]

momEqlDerivNormaliSentence = [S "where", ch rotForce, S "is the", 
  phrase rotForce `sAnd` ch momntArm, S "is the", phrase momntArm `sC`
  S "or the distance between the", phrase force, S "and the axis about" +:+.
  S "which the rotation acts",
  S "To represent the", phrase momentEqlGD `sC` S "the", plural momntOfBdy,
  S "from each", phrase force, S "acting on a", phrase slice +:+. 
  S "must be considered and added together", S "The", plural force,
  S "acting on a", phrase slice, S "are all shown in" +:+.
  makeRef2S fig_forceacting,
  S "The midpoint of the base of a", phrase slice, S "is considered as the",
  S "axis of rotation, from which the", phrase momntArm +:+. S "is measured",
  S "Considering first the", phrase intrslce, phrase normForce,
  S "acting on", phrase slice, S "interface", ch index `sC` S "the", 
  phrase momntOfBdy, S "is negative because the", phrase force, 
  S "tends to rotate the", phrase slice, S "in a counterclockwise",
  S "direction" `sC` S "and the", phrase momntArm, S "is", S "height" `ofThe`
  phrase force, S "plus the difference in height between the base at", 
  phrase slice, S "interface", ch index `andThe` S "base at the midpoint of",
  phrase slice +:+. ch index, 
  S "Thus, the", phrase momntOfBdy, S "is expressed as"]

momEqlDerivNormaliM1Sentence = [S "For the", E (sy index - 1) :+: S "th",
  phrase slice, S "interface, the", phrase momntOfBdy, S "is similar but in",
  S "the opposite direction"]

momEqlDerivWateriSentence = [S "Next, the", phrase intrslce, S "normal water",
  phrase force +:+. S "is considered", S "This", phrase force, S "is zero at",
  S "height" `ofThe` phrase waterTable `sC` S "then increases linearly towards",
  S "base" `ofThe` phrase slice, S "due to the increasing water" +:+.
  phrase pressure, S "For such a triangular distribution, the resultant", 
  phrase force +:+. S "acts at one-third of the height", S "Thus, for the",
  phrase intrslce, S "normal water", phrase force, S "acting on", phrase slice, 
  S "interface", ch index `sC` S "the", phrase momntOfBdy, S "is" ]

momEqlDerivWateriM1Sentence = [S "The", phrase momntOfBdy, S "for the",
  phrase intrslce, S "normal water", phrase force, S "acting on", phrase slice,
  S "interface", E (sy index - 1), S "is"]

momEqlDerivSheariSentence = [S "The", phrase intrslce, phrase shearForce, 
  S "at", phrase slice, S "interface", ch index, S "tends to rotate in the",
  S "clockwise direction, and the", phrase momntArm, S "is the", phrase len, 
  S "from the", phrase slice, S "edge to the", phrase slice, S "midpoint" `sC`
  S "equivalent to half of", S "width" `ofThe` phrase slice `sC` S "so the",
  phrase momntOfBdy +:+. S "is"]

momEqlDerivSheariM1Sentence = [S "The", phrase intrslce, phrase shearForce,
  S "at", phrase slice, S "interface", E (sy index - 1), S "also tends to",
  S "rotate in the clockwise direction, and has the same", phrase momntArm `sC`
  S "so the", phrase momntOfBdy, S "is"]

-- FIXME: Once differentials are expressible in Expr, change "sy yi" to the differential dy. "ch yi" actually means y and should stay as-is.
momEqlDerivSeismicIntSentence = [S "Seismic", plural force, S "act over the",
  S "entire height of the" +:+. phrase slice, S "For each horizontal segment",
  S "of the", phrase slice `sC` S "the seismic", phrase force, S "is",
  E (sy earthqkLoadFctr * inxi slcWght), S "where", E (inxi slcWght),<commit_msg>Added reference to relevant issue in FIXME<commit_after>module Drasil.SSP.GenDefs (normForcEq, bsShrFEq, resShr, mobShr,
  normShrR, momentEql, generalDefinitions,
  normForcEqGD, bsShrFEqGD, resShrGD, mobShrGD, normShrRGD, momentEqlGD,
  mobShearWOGD, resShearWOGD, srfWtrFGD) where

import Prelude hiding (sin, cos, tan)
import Language.Drasil
import Theory.Drasil (GenDefn, gd)

import Drasil.DocLang.SRS as SRS (physSyst)

import Data.Drasil.Utils (weave)

import Data.Drasil.SI_Units (metre, newton)

import Data.Drasil.Concepts.Documentation (analysis, assumption, component,
  constant, definition, method_, value)
import Data.Drasil.Concepts.Math (area, equation)
import Data.Drasil.Concepts.PhysicalProperties (len)
import Data.Drasil.Concepts.Physics (distance, pressure, twoD, weight)
import Data.Drasil.Concepts.SolidMechanics (normForce, shearForce)
import Data.Drasil.Quantities.Physics (displacement, force, torque)
import Data.Drasil.Theories.Physics (weightGD, torqueDD)

import Data.Drasil.SentenceStructures (foldlSent, foldlSent_, foldlSentCol, 
  getTandS, isThe, ofThe, sAnd, sIs, sOf, andThe)

import Drasil.SSP.Assumptions (assumpFOSL, assumpSLH, assumpSP, assumpSLI,
  assumpINSFL, assumpPSC, assumpSBSBISL, assumpWIBE, assumpWISE, assumpNESSS,
  assumpHFSM)
import Drasil.SSP.BasicExprs (eqlExpr, eqlExprN, momExpr)
import Drasil.SSP.DataDefs (intersliceWtrF, angleA, angleB, lengthB, lengthLb, 
  slcHeight, stressDD, ratioVariation)
import Drasil.SSP.Defs (intrslce, slice, slope, slopeSrf, slpSrf, soil, 
  soilPrpty, waterTable)
import Drasil.SSP.Figures (fig_forceacting)
import Drasil.SSP.References (chen2005, fredlund1977, karchewski2012)
import Drasil.SSP.TMods (factOfSafety, equilibrium, mcShrStrgth, effStress)
import Drasil.SSP.Unitals (baseAngle, baseHydroForce, baseLngth, baseWthX, 
  dryWeight, earthqkLoadFctr, effCohesion, fricAngle, fs, genericA, genericSpWght, impLoadAngle, intNormForce, intShrForce, 
  index, inxi, inxiM1, midpntHght, mobShrI, momntArm, momntOfBdy, normToShear, 
  nrmFSubWat, rotForce, satWeight, scalFunc, shearFNoIntsl, shrResI, shrResI, 
  shrStress, totNrmForce, shearRNoIntsl, shrResI, slcWght, sliceHght, sliceHghtW, slipHght,
  slopeHght, surfHydroForce, surfAngle, surfLoad, watrForce, waterHght, waterWeight, 
  dryVol, satVol, waterVol, yi, zcoord)

---------------------------
--  General Definitions  --
---------------------------
generalDefinitions :: [GenDefn]
generalDefinitions = [normForcEqGD, bsShrFEqGD, resShrGD, mobShrGD,
 effNormFGD, resShearWOGD, mobShearWOGD, normShrRGD, momentEqlGD, weightGD, 
 sliceWghtGD, baseWtrFGD, srfWtrFGD]

normForcEqGD, bsShrFEqGD, resShrGD, mobShrGD, effNormFGD, resShearWOGD, 
  mobShearWOGD, normShrRGD, momentEqlGD, sliceWghtGD, baseWtrFGD, 
  srfWtrFGD :: GenDefn
normForcEqGD = gd normForcEq (getUnit totNrmForce)   [nmFEq_deriv]    
  [makeCite chen2005]                      "normForcEq"  [nmFEq_desc]
bsShrFEqGD   = gd bsShrFEq   (getUnit mobShrI)       [bShFEq_deriv]
  [makeCite chen2005]                      "bsShrFEq"    [bShFEq_desc]
resShrGD     = gd resShr     (getUnit shrResI)       [resShr_deriv]   
  [makeCite chen2005]                      "resShr"      [resShr_desc]
mobShrGD     = gd mobShr     (getUnit mobShrI)       [mobShr_deriv]   
  [makeCite chen2005]                      "mobShr"      [mobShr_desc]
effNormFGD   = gd effNormF   (getUnit nrmFSubWat)    [effNormF_deriv] 
  [makeCite chen2005]                      "effNormF"    [effNormF_desc]
resShearWOGD = gd resShearWO (getUnit shearRNoIntsl) []         
  (map makeCite[chen2005, karchewski2012]) "resShearWO"  [resShearWO_desc]
mobShearWOGD = gd mobShearWO (getUnit shearFNoIntsl) []
  (map makeCite[chen2005, karchewski2012]) "mobShearWO"  [mobShearWO_desc]
normShrRGD   = gd normShrR   (getUnit intShrForce)   [] 
  [makeCite chen2005]                      "normShrR"    [nmShrR_desc]
momentEqlGD  = gd momentEql  (Just newton)           momEqlDeriv
  [makeCite chen2005]                      "momentEql"   [momEql_desc]
sliceWghtGD  = gd sliceWght  (getUnit slcWght)       sliceWghtDeriv
  [makeCite fredlund1977]                  "sliceWght"   [sliceWghtNotes]
baseWtrFGD   = gd baseWtrF   (getUnit baseHydroForce) bsWtrFDeriv
  [makeCite fredlund1977]                  "baseWtrF"    [bsWtrFNotes]
srfWtrFGD    = gd srfWtrF    (getUnit surfHydroForce) srfWtrFDeriv   
  [makeCite fredlund1977]                  "srfWtrF"     [srfWtrFNotes]

--
normForcEq :: RelationConcept
normForcEq = makeRC "normForcEq" (nounPhraseSP "normal force equilibrium")
  nmFEq_desc nmFEq_rel

nmFEq_rel :: Relation
nmFEq_rel = inxi totNrmForce $= eqlExprN cos sin
  (\x y -> x - inxiM1 intShrForce + inxi intShrForce + y)

nmFEq_desc :: Sentence
nmFEq_desc = foldlSent [S "This equation satisfies", makeRef2S equilibrium +:+.
  S "in the normal direction", ch slcWght, S "is defined in", 
  makeRef2S sliceWghtGD `sC` ch surfHydroForce,  S "is defined in", 
  makeRef2S srfWtrFGD `sC` ch surfAngle, S "is defined in",
  makeRef2S angleB `sC` S "and", ch baseAngle, S "is defined in", 
  makeRef2S angleA]

nmFEq_deriv :: Sentence
nmFEq_deriv = foldlSent [at_start normForcEq, S "is derived from the free",
  S "body diagram of", makeRef2S fig_forceacting, S "in", 
  (makeRef2S $ SRS.physSyst ([]::[Contents]) ([]::[Section]))]

--
bsShrFEq :: RelationConcept
bsShrFEq = makeRC "bsShrFEq" (nounPhraseSP "base shear force equilibrium")
  bShFEq_desc bShFEq_rel

bShFEq_rel :: Relation
bShFEq_rel = inxi mobShrI $= eqlExpr sin cos
  (\x y -> x - inxiM1 intShrForce + inxi intShrForce + y)

bShFEq_desc :: Sentence
bShFEq_desc = foldlSent [S "This equation satisfies", makeRef2S equilibrium +:+.
  S "in the shear direction", ch slcWght, S "is defined in", 
  makeRef2S sliceWghtGD `sC` ch surfHydroForce, S "is defined in", 
  makeRef2S srfWtrFGD `sC` ch surfAngle, S "is defined in",
  makeRef2S angleB `sC` S "and", ch baseAngle, S "is defined in", 
  makeRef2S angleA]

bShFEq_deriv :: Sentence
bShFEq_deriv = foldlSent [at_start bsShrFEq, S "is derived from the free",
  S "body diagram of", makeRef2S fig_forceacting, S "in", 
  (makeRef2S $ SRS.physSyst ([]::[Contents]) ([]::[Section]))]

--
shrResEqn :: Expr
shrResEqn = inxi nrmFSubWat * tan (inxi fricAngle) + inxi effCohesion *
  inxi baseLngth

resShr :: RelationConcept
resShr = makeRC "resShr" (nounPhraseSP "resistive shear force")
  resShr_desc resShr_rel -- genDef3Label

resShr_rel :: Relation
resShr_rel = inxi shrResI $= shrResEqn

resShr_desc :: Sentence
resShr_desc = foldlSent_ [ch baseLngth, S "is defined in" +:+. 
  makeRef2S lengthLb]

resShr_deriv :: Sentence
resShr_deriv = foldlSent_ [S "Derived by substituting", makeRef2S stressDD,
  S "into the Mohr-Coulomb", phrase shrStress `sC` makeRef2S mcShrStrgth `sC`
  S "and multiplying both sides of the", phrase equation, S "by", 
  phrase genericA `ofThe` phrase slice, S "in the shear-" :+: ch zcoord  +:+. 
  S "plane", S "Since the", phrase slope, S "is assumed to extend infinitely",
  S "in the", ch zcoord :+: S "-direction", sParen (makeRef2S assumpPSC) `sC` 
  S "the resulting", plural force, S "are expressed per", phrase metre,
  S "in the", ch zcoord :+: S "-direction.", S "The", 
  getTandS fricAngle `andThe` getTandS effCohesion, S "are not indexed by",
  ch index, S "becaused they are assumed to be isotropic", 
  sParen (makeRef2S assumpSLI) `andThe` phrase soil, S "is assumed to be",
  S "homogeneous, with", phrase constant, plural soilPrpty, S "throughout" +:+.
  sParen (makeRef2S assumpSLH `sC` makeRef2S assumpSP)]

--
mobShr :: RelationConcept
mobShr = makeRC "mobShr"
  (nounPhraseSP "mobilized shear force") mobShr_desc mobShr_rel -- genDef4Label

mobShr_rel :: Relation
mobShr_rel = inxi mobShrI $= inxi shrResI / sy fs $= shrResEqn / sy fs

mobShr_desc :: Sentence
mobShr_desc = foldlSent_ [ch baseLngth, S "is defined in" +:+. 
  makeRef2S lengthLb]

mobShr_deriv :: Sentence
mobShr_deriv = foldlSent_ [at_start mobShrI, S "is derived by dividing",
  phrase definition `ofThe` ch shrResI, S "from" +:+. makeRef2S resShrGD,
  S "by", phrase definition `ofThe` phrase fs, S "from" +:+.
  makeRef2S factOfSafety, S "The", getTandS fs, S "is not indexed by", ch index,
  S "because it is assumed to be", phrase constant, S "for the entire",
  phrase slpSrf +:+. sParen (makeRef2S assumpFOSL)]

--
effNormF :: RelationConcept
effNormF = makeRC "effNormF"
  (nounPhraseSP "effective normal force") effNormF_desc effNormF_rel

effNormF_rel :: Relation
effNormF_rel = inxi nrmFSubWat $= inxi totNrmForce - inxi baseHydroForce

effNormF_desc :: Sentence
effNormF_desc = ch baseHydroForce +:+ S "is defined in" +:+. makeRef2S baseWtrFGD

effNormF_deriv :: Sentence
effNormF_deriv = foldlSent [
  S "Derived by substituting", makeRef2S stressDD, S "into", 
  makeRef2S effStress `sAnd` S "multiplying both sides of the", phrase equation,
  S "by the", phrase genericA `ofThe` phrase slice, S "in the shear-" :+: 
  ch zcoord +:+. S "plane", S "Since the", phrase slope, 
  S "is assumed to extend infinitely in the", ch zcoord :+: S "-direction", 
  sParen (makeRef2S assumpPSC) `sC` S "the resulting", plural force, S "are expressed per", phrase metre, S "in the", ch zcoord :+: S "-direction" ]

--
normShrR :: RelationConcept
normShrR = makeRC "normShrR"
  (nounPhraseSP "interslice normal and shear force proportionality") 
  nmShrR_desc nmShrR_rel -- genDef5Label

nmShrR_rel :: Relation
nmShrR_rel = sy intShrForce $= sy normToShear * sy scalFunc * sy intNormForce

nmShrR_desc :: Sentence
nmShrR_desc = foldlSent [S "Mathematical representation of the primary", 
  phrase assumption, S "for the Morgenstern-Price", phrase method_ +:+.
  sParen (makeRef2S assumpINSFL), ch scalFunc, S "is defined in", 
  makeRef2S ratioVariation]

--
resShearWO :: RelationConcept
resShearWO = makeRC "resShearWO"
  (nounPhraseSP "resistive shear force, without interslice normal and shear forces") resShearWO_desc resShearWO_rel

resShearWO_rel :: Relation
resShearWO_rel = inxi shearRNoIntsl $= 
  (((inxi slcWght) + (inxi surfHydroForce) * (cos (inxi surfAngle))) *
  (cos (inxi baseAngle)) + (negate (inxi watrForce) + (inxiM1 watrForce) + 
  (inxi surfHydroForce) * sin (inxi surfAngle)) * (sin (inxi baseAngle)) -
  (inxi baseHydroForce)) * tan (inxi fricAngle) + (inxi effCohesion) *
  (inxi baseLngth)

resShearWO_desc :: Sentence
resShearWO_desc = foldlSent_ [ch slcWght, S "is defined in", 
  makeRef2S sliceWghtGD `sC` ch surfHydroForce, S "is defined in", 
  makeRef2S srfWtrFGD `sC` ch surfAngle, S "is defined in", 
  makeRef2S angleB `sC` ch baseAngle, S "is defined in",
  makeRef2S angleA `sC` ch watrForce, S "is defined in",
  makeRef2S intersliceWtrF `sC` ch baseHydroForce, S "is defined in",
  makeRef2S baseWtrFGD `sC` S "and", ch baseLngth, S "is defined in" +:+. 
  makeRef2S lengthLb]

--
--
mobShearWO :: RelationConcept
mobShearWO = makeRC "mobShearWO"
  (nounPhraseSP "mobilized shear force, without interslice normal and shear forces") mobShearWO_desc mobShearWO_rel

mobShearWO_rel :: Relation
mobShearWO_rel = inxi shearFNoIntsl $= ((inxi slcWght) + (inxi surfHydroForce) *
  (cos (inxi surfAngle))) * (sin (inxi baseAngle)) - (negate (inxi watrForce) + 
  (inxiM1 watrForce) + (inxi surfHydroForce) * sin (inxi surfAngle)) * (cos (inxi baseAngle))

mobShearWO_desc :: Sentence
mobShearWO_desc = foldlSent_ [ch slcWght, S "is defined in", 
  makeRef2S sliceWghtGD `sC` ch surfHydroForce, S "is defined in", 
  makeRef2S srfWtrFGD `sC` ch surfAngle, S "is defined in", 
  makeRef2S angleB `sC` ch baseAngle, S "is defined in",
  makeRef2S angleA `sC` S "and", ch watrForce, S "is defined in" +:+.
  makeRef2S intersliceWtrF]

--

momentEql :: RelationConcept
momentEql = makeRC "momentEql" (nounPhraseSP "moment equilibrium")
  momEql_desc momEql_rel -- genDef6Label

momEql_rel :: Relation
momEql_rel = 0 $= momExpr (\ x y -> x +
  (inxi baseWthX / 2 * (inxi intShrForce + inxiM1 intShrForce)) + y)

momEql_desc :: Sentence
momEql_desc = foldlSent [S "This", phrase equation, S "satisfies", 
  makeRef2S equilibrium, S "for the net" +:+. phrase momntOfBdy, ch baseWthX,
  S "is defined in", makeRef2S lengthB `sC` ch baseAngle, S "is defined in",
  makeRef2S angleA `sC` ch slcWght, S "is defined in", 
  makeRef2S sliceWghtGD `sC` ch midpntHght, S "is defined in", 
  makeRef2S slcHeight `sC` ch surfHydroForce, S "is defined in", 
  makeRef2S srfWtrFGD `sC` S "and", ch surfAngle, S "is defined in", 
  makeRef2S angleB]

momEqlDeriv :: Derivation
momEqlDeriv = weave [momEqlDerivSentences, momEqlDerivEqns]

momEqlDerivSentences :: [Sentence]
momEqlDerivSentences = map foldlSentCol [momEqlDerivTorqueSentence, 
  momEqlDerivMomentSentence, momEqlDerivNormaliSentence, 
  momEqlDerivNormaliM1Sentence, momEqlDerivWateriSentence, 
  momEqlDerivWateriM1Sentence, momEqlDerivSheariSentence, 
  momEqlDerivSheariM1Sentence, momEqlDerivSeismicIntSentence, 
  momEqlDerivSeismicSentence, momEqlDerivSeismicWSentence, 
  momEqlDerivHydroSentence, momEqlDerivExtSentence, momEqlDerivFinalSentence]

momEqlDerivEqns :: [Sentence]
momEqlDerivEqns = map E [momEqlDerivTorqueEqn, momEqlDerivMomentEqn,
  momEqlDerivNormaliEqn, momEqlDerivNormaliM1Eqn, momEqlDerivWateriEqn, 
  momEqlDerivWateriM1Eqn, momEqlDerivSheariEqn, 
  momEqlDerivSheariM1Eqn, momEqlDerivSeismicIntEqn,
  momEqlDerivSeismicEqn, momEqlDerivSeismicWEqn,
  momEqlDerivHydroEqn, momEqlDerivExtEqn, 
  momEqlDerivFinalEqn]

momEqlDerivTorqueSentence, momEqlDerivMomentSentence,
  momEqlDerivNormaliSentence, momEqlDerivNormaliM1Sentence, 
  momEqlDerivWateriSentence, momEqlDerivWateriM1Sentence, 
  momEqlDerivSheariSentence, momEqlDerivSheariM1Sentence, 
  momEqlDerivSeismicIntSentence, momEqlDerivSeismicSentence, 
  momEqlDerivSeismicWSentence, momEqlDerivHydroSentence, 
  momEqlDerivExtSentence, momEqlDerivFinalSentence :: [Sentence]

momEqlDerivTorqueEqn, momEqlDerivMomentEqn,
  momEqlDerivNormaliEqn, momEqlDerivNormaliM1Eqn, momEqlDerivWateriEqn, 
  momEqlDerivWateriM1Eqn, momEqlDerivSheariEqn, 
  momEqlDerivSheariM1Eqn, momEqlDerivSeismicIntEqn,
  momEqlDerivSeismicEqn, momEqlDerivSeismicWEqn,
  momEqlDerivHydroEqn, momEqlDerivExtEqn, 
  momEqlDerivFinalEqn :: Expr

momEqlDerivTorqueSentence = [at_start momntOfBdy, S "is equal to", 
  phrase torque `sC` S "so the", phrase equation, S "from", makeRef2S torqueDD,
  S "will be used to calculate", plural momntOfBdy]

momEqlDerivMomentSentence = [S "Considering one dimension, with",
  plural momntOfBdy, S "in the clockwise direction as positive and", 
  plural momntOfBdy, S "in the counterclockwise direction as negative" `sC`
  S "and replacing the", phrase torque, S "symbol with the", phrase momntOfBdy, 
  S "symbol, the", phrase equation, S "simplifies to"]

momEqlDerivNormaliSentence = [S "where", ch rotForce, S "is the", 
  phrase rotForce `sAnd` ch momntArm, S "is the", phrase momntArm `sC`
  S "or the distance between the", phrase force, S "and the axis about" +:+.
  S "which the rotation acts",
  S "To represent the", phrase momentEqlGD `sC` S "the", plural momntOfBdy,
  S "from each", phrase force, S "acting on a", phrase slice +:+. 
  S "must be considered and added together", S "The", plural force,
  S "acting on a", phrase slice, S "are all shown in" +:+.
  makeRef2S fig_forceacting,
  S "The midpoint of the base of a", phrase slice, S "is considered as the",
  S "axis of rotation, from which the", phrase momntArm +:+. S "is measured",
  S "Considering first the", phrase intrslce, phrase normForce,
  S "acting on", phrase slice, S "interface", ch index `sC` S "the", 
  phrase momntOfBdy, S "is negative because the", phrase force, 
  S "tends to rotate the", phrase slice, S "in a counterclockwise",
  S "direction" `sC` S "and the", phrase momntArm, S "is", S "height" `ofThe`
  phrase force, S "plus the difference in height between the base at", 
  phrase slice, S "interface", ch index `andThe` S "base at the midpoint of",
  phrase slice +:+. ch index, 
  S "Thus, the", phrase momntOfBdy, S "is expressed as"]

momEqlDerivNormaliM1Sentence = [S "For the", E (sy index - 1) :+: S "th",
  phrase slice, S "interface, the", phrase momntOfBdy, S "is similar but in",
  S "the opposite direction"]

momEqlDerivWateriSentence = [S "Next, the", phrase intrslce, S "normal water",
  phrase force +:+. S "is considered", S "This", phrase force, S "is zero at",
  S "height" `ofThe` phrase waterTable `sC` S "then increases linearly towards",
  S "base" `ofThe` phrase slice, S "due to the increasing water" +:+.
  phrase pressure, S "For such a triangular distribution, the resultant", 
  phrase force +:+. S "acts at one-third of the height", S "Thus, for the",
  phrase intrslce, S "normal water", phrase force, S "acting on", phrase slice, 
  S "interface", ch index `sC` S "the", phrase momntOfBdy, S "is" ]

momEqlDerivWateriM1Sentence = [S "The", phrase momntOfBdy, S "for the",
  phrase intrslce, S "normal water", phrase force, S "acting on", phrase slice,
  S "interface", E (sy index - 1), S "is"]

momEqlDerivSheariSentence = [S "The", phrase intrslce, phrase shearForce, 
  S "at", phrase slice, S "interface", ch index, S "tends to rotate in the",
  S "clockwise direction, and the", phrase momntArm, S "is the", phrase len, 
  S "from the", phrase slice, S "edge to the", phrase slice, S "midpoint" `sC`
  S "equivalent to half of", S "width" `ofThe` phrase slice `sC` S "so the",
  phrase momntOfBdy +:+. S "is"]

momEqlDerivSheariM1Sentence = [S "The", phrase intrslce, phrase shearForce,
  S "at", phrase slice, S "interface", E (sy index - 1), S "also tends to",
  S "rotate in the clockwise direction, and has the same", phrase momntArm `sC`
  S "so the", phrase momntOfBdy, S "is"]

-- FIXME: Once differentials are expressible in Expr (issue #1407), change "sy yi" to the differential dy. "ch yi" actually means y and should stay as-is.
momEqlDerivSeismicIntSentence = [S "Seismic", plural force, S "act over the",
  S "entire height of the" +:+. phrase slice, S "For each horizontal segment",
  S "of the", phrase slice `sC` S "the seismic", phrase force, S "is",
  E (sy earthqkLoadFctr * inxi slcWght), S "where", E (inxi slcWght),
{'fraction_non_alphanumeric': 0.1041890440386681, 'fraction_numerical': 0.00837257980115123, 'mean_word_length': 4.635573490609964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2260284', 'n_tokens_mistral': 14426, 'n_tokens_neox': 13790, 'n_words': 3939}