# Script generated by the UniPV WinBUGS converter
# 'WinBUGSConverter' copyright (c) BMS - University of Pavia (2016)
# Converter Version: 1.0.0
# Source: PharmML 0.8.1
# Target: WINBUGS 1.4.3
# Run ID: run_id_0
# Model: Generated from MDL. MOG ID: outputMog
# File: InstallationTest_COMPARTMENT_BUGS
# Dated: Sun Jul 31 17:25:13 CEST 2016

model {
  for (ind_subj in 1:N_subj) {
    for (ind_t in 1:N_t[ind_subj]) {
      Y_mean[ind_subj,ind_t] <- (CONC[ind_subj,ind_t] + 0)
      Y[ind_subj,ind_t] ~ dnorm(Y_mean[ind_subj,ind_t], Y_prec)
      Y_res[ind_subj,ind_t] <- Y[ind_subj,ind_t]-Y_mean[ind_subj,ind_t]
    }
    eta_K[ind_subj] ~ dnorm(0, eta_K_prec)
    eta_V[ind_subj] ~ dnorm(0, eta_V_prec)
    log(K[ind_subj])<- log(POP_K) + eta_K[ind_subj]
    log(V[ind_subj])<- log(POP_V) + eta_V[ind_subj]
    der99wb_unipv[ind_subj,1:N_t[ind_subj], 1:1]<- function.model1(grid[ind_subj,1:N_t[ind_subj]], amt[ind_subj,1:N_t[ind_subj]],  rate[ind_subj,1:N_t[ind_subj]], ii[ind_subj,1:N_t[ind_subj]], evid[ind_subj,1:N_t[ind_subj]], cmt[ind_subj,1:N_t[ind_subj]], addl[ind_subj,1:N_t[ind_subj]], ss[ind_subj,1:N_t[ind_subj]], theta[ind_subj,])
    theta[ind_subj,1] <- eta_K[ind_subj]
    theta[ind_subj,2] <- lPOP_K
    theta[ind_subj,3] <- 0.0 #IC CENTRAL
    theta[ind_subj,4] <- 1 #F CENTRAL
    theta[ind_subj,5] <- 0 #TLAG CENTRAL
    for (ind_q in 1:N_t[ind_subj]) {
      CENTRAL[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,1] + 0.0
      CONC[ind_subj,ind_q] <- (CENTRAL[ind_subj,ind_q] / V[ind_subj])
    }
  }
  Y_prec <- 1 / ((pow((SD_ADD),2))*1)
  eta_K_prec <- (1/(OMEGA_K))
  eta_V_prec <- (1/(OMEGA_V))
  lMU_POP_K <- (log(MU_POP_K))
  lMU_POP_V <- (log(MU_POP_V))
  POP_K <- (exp(lPOP_K))
  POP_V <- (exp(lPOP_V))
  OMEGA_K <- (1 / TAU_K)
  OMEGA_V <- (1 / TAU_V)
  SD_ADD <- (1 / (sqrt(TAU)))
  MU_POP_K <- 0.1
  MU_POP_V <- 8
  VAR_POP_K <- 0.03
  VAR_POP_V <- 0.03
  a_OMEGA_K <- 1
  b_OMEGA_K <- 0.01
  a_OMEGA_V <- 1
  b_OMEGA_V <- 0.01
  a_POP_T <- 1
  b_POP_T <- 0.01
  # PRIORS
  TAU_K ~ dgamma(a_OMEGA_K, TAU_K_rate)
  TAU_K_rate <- b_OMEGA_K
  lPOP_V ~ dnorm(lMU_POP_V, lPOP_V_prec)
  lPOP_V_prec <- (1/(VAR_POP_V))
  TAU ~ dgamma(a_POP_T, TAU_rate)
  TAU_rate <- b_POP_T
  lPOP_K ~ dnorm(lMU_POP_K, lPOP_K_prec)
  lPOP_K_prec <- (1/(VAR_POP_K))
  TAU_V ~ dgamma(a_OMEGA_V, TAU_V_rate)
  TAU_V_rate <- b_OMEGA_V
}
