R/hydrostat.R

Defines functions hydroFormalSoln radPress

## MIT License
##
## Copyright (c) 2018 Oliver Dechant
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions {
##
## The above copyright notice and this permission notice shall be included in all
## copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.

useful <- readRDS("./data/useful.rds")

hydroFormalSoln <- function(numDeps, grav, tauRos, kappa, temp, guessPGas) {
  #this approach is based on integrating the formal solution of the hydrostatic
  #equilibrium equation on the otical depth (Tau) scale.
  press <- matrix(0.0, numDeps, 2)
  logC <- useful[,"logC"]
  logSigma <- useful[,"logSigma"]
  radFac <- log(4.0)+logSigma-log(3.0)-logC
  logEg <- log(grav)
  log1p5 <- log(1.5)
  logE <- log10(exp(1))
  #compute radiation pressure for this temperature structure and add it to Pgas
  logPRad <- rep(0.0, numDeps)
  logPTot <- rep(0.0, numDeps)

  for (i in 1:numDeps) {
    logPRad[i] <- radFac+4.0*temp[i, 2]
    pRad <- exp(logPRad[i])
    pT <- guessPGas[i,1]+pRad
    logPTot[i] <- log(pT)
  }
  press[1, 1] <- 0.1*guessPGas[1, 1]
  press[1, 2] <- log(press[1, 1])
  #corresponding value of basic integrated quantity at top of atmosphere
  logSum <- 1.5*press[1][2]+log(0.666667)-logEg
  sum <- rep(0.0, numDeps)
  sum[1] <- exp(logSum)
  #integrate inward on logTau scale
  #this is not an integral for Delta P, but for P once integral at each tau is
  #exponentiated by 2/3
  #accumulate basic integral to be exponentiated, then construct pressure values

  #jump start integration with an Euler step
  deltaLogT <- tauRos[2, 2]-tauRos[1, 2]
  #log of integrand
  logInteg <- tauRos[2, 2]+0.5*logPTot[2]-kappa[2, 2]
  lastInteg <- exp(logInteg)
  sum[2] <- sum[1]+lastInteg*deltaLogT
  #continue with extended trapezoid rule

  for (i in 3:numDeps) {
    deltaLogT <- tauRos[i, 2]-tauRos[i-1, 2]
    logInteg <- tauRos[i, 2]+0.5*logPTot[i]-kappa[i, 2]
    integ <- exp(logInteg)
    term <- 0.5*(integ+lastInteg)*deltaLogT
    #accumulate basic integrated quantity
    sum[i] <- sum[i-1]+term
    lastInteg <- integ
  }

  #evaluate total pressure from basic integrated quantity at each depth our
  #integration variable is the natural log, so 1/log(e) factor is unneeded
  for (i in 1:numDeps) {
    logPress <- 0.666667*(log1p5-logEg+log(sum[i]))
    #subtract radiation pressure
    logHelp <- logPRad[i]-logPress
    help <- exp(logHelp)
    #for hot and low g start: limit Prad to 50% Ptot so we don't get negative
    #Pgas and rho values
    if (is.na(help)) {
      help = 0.0
    }
    if (help > 0.5) {
      help <- 0.5
    }
    press[i, 2] <- logPress+log(1.0-help)
    press[i, 1] <- exp(press[i, 2])
  }
  #gas pressure
  press
}

#Compute radiation pressure
radPress <- function(numDeps, temp) {
  pRad <- matrix(0,0, numDeps, 2)
  logC <- as.double(useful[,"logC"])
  logSigma <- as.double(useful[,"logSigma"])
  radFac <- log(4.0)+logSigma-log(3.0)-logC

  for (i in 1:numDeps) {
    pRad[i, 2] <- radFac+4.0*temp[i, 2]
    pRad[i, 1] <- exp(pRad[i, 2])
  }

  pRad
}
increasechief/chromastar documentation built on May 14, 2019, 5:14 a.m.