# 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: warfarin_PK_ODE_mog
# File: UseCase10_1
# Dated: Mon Aug 01 08:23:20 CEST 2016

model {
  for (ind_subj in 1:N_subj) {
    for (ind_t in 1:N_t[ind_subj]) {
      logtWTinterp[ind_subj,ind_t] <- interp.function(grid[ind_subj,ind_t], grid_logtWT[ind_subj,1:N_t_logtWT[ind_subj]], logtWT[ind_subj,1:N_t_logtWT[ind_subj]])
      K[ind_subj,ind_t] <- (CL[ind_subj,ind_t] / VC[ind_subj,ind_t])
      K23[ind_subj,ind_t] <- (Q[ind_subj] / VC[ind_subj,ind_t])
      CC_obs_mean[ind_subj,ind_t] <- (CC[ind_subj,ind_t] + 0)
      CC_obs_prec[ind_subj,ind_t] <- 1 / ((pow(((RUV_ADD + (RUV_PROP * CC[ind_subj,ind_t]))),2))*pow(1,2))
      CC_obs[ind_subj,ind_t] ~ dnorm(CC_obs_mean[ind_subj,ind_t], CC_obs_prec[ind_subj,ind_t])
      CC_obs_res[ind_subj,ind_t] <- CC_obs[ind_subj,ind_t]-CC_obs_mean[ind_subj,ind_t]
      log(CL[ind_subj,ind_t]) <- log(POP_CL) + POP_BETA_CL_WT * logtWTinterp[ind_subj,ind_t] + eta_PPV_CL[ind_subj]
      log(VC[ind_subj,ind_t]) <- log(POP_VC) + POP_BETA_V_WT * logtWTinterp[ind_subj,ind_t] + eta_PPV_VC[ind_subj]
    }
    K32[ind_subj] <- (Q[ind_subj] / VP[ind_subj])
    eta_PPV_Q[ind_subj] ~ dnorm(0, eta_PPV_Q_prec)
    eta_PPV_VP[ind_subj] ~ dnorm(0, eta_PPV_VP_prec)
    eta_PPV_KA[ind_subj] ~ dnorm(0, eta_PPV_KA_prec)
    eta_PPV_TLAG[ind_subj] ~ dnorm(0, eta_PPV_TLAG_prec)
    log(Q[ind_subj])<- log(POP_Q) + eta_PPV_Q[ind_subj]
    log(VP[ind_subj])<- log(POP_VP) + eta_PPV_VP[ind_subj]
    log(KA[ind_subj])<- log(POP_KA) + eta_PPV_KA[ind_subj]
    log(TLAG[ind_subj])<- log(POP_TLAG) + eta_PPV_TLAG[ind_subj]
    ETA1[ind_subj,1:2] ~ dmnorm(mu1[], T1[,])
    eta_PPV_CL[ind_subj] <- ETA1[ind_subj,1]
    eta_PPV_VC[ind_subj] <- ETA1[ind_subj,2]
    der99wb_unipv[ind_subj,1:N_t[ind_subj], 1:3]<- 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] <- n_cov_cont
    theta[ind_subj,2] <- max_m_logtWT
    theta[ind_subj,3] <- N_t_logtWT[ind_subj]
    theta[ind_subj,3+2*max_m_logtWT +1] <- eta_PPV_KA[ind_subj]
    theta[ind_subj,3+2*max_m_logtWT +2] <- POP_KA
    theta[ind_subj,3+2*max_m_logtWT +3] <- eta_PPV_Q[ind_subj]
    theta[ind_subj,3+2*max_m_logtWT +4] <- POP_Q
    theta[ind_subj,3+2*max_m_logtWT +5] <- eta_PPV_VC[ind_subj]
    theta[ind_subj,3+2*max_m_logtWT +6] <- POP_VC
    theta[ind_subj,3+2*max_m_logtWT +7] <- POP_BETA_V_WT
    theta[ind_subj,3+2*max_m_logtWT +8] <- eta_PPV_VP[ind_subj]
    theta[ind_subj,3+2*max_m_logtWT +9] <- POP_VP
    theta[ind_subj,3+2*max_m_logtWT +10] <- eta_PPV_CL[ind_subj]
    theta[ind_subj,3+2*max_m_logtWT +11] <- POP_CL
    theta[ind_subj,3+2*max_m_logtWT +12] <- POP_BETA_CL_WT
    for (i in 1 : max_m_logtWT){ 
	theta[ind_subj,i+3] <- grid_logtWT[ind_subj,i]
	}
	for (i in 1 : max_m_logtWT){ 
	theta[ind_subj,i+3+max_m_logtWT] <- logtWT[ind_subj,i]
	}
      theta[ind_subj,3+2*max_m_logtWT+13] <- 0.0 #IC Ad1
      theta[ind_subj,3+2*max_m_logtWT+14] <- 0.0 #IC CENTRAL
      theta[ind_subj,3+2*max_m_logtWT+15] <- 0.0 #IC PERIPHERAL
      theta[ind_subj,3+2*max_m_logtWT+16] <- 1 #F Ad1
      theta[ind_subj,3+2*max_m_logtWT+17] <- 1 #F CENTRAL
      theta[ind_subj,3+2*max_m_logtWT+18] <- 1 #F PERIPHERAL
      theta[ind_subj,3+2*max_m_logtWT+19] <- TLAG[ind_subj] #TLAG Ad1
      theta[ind_subj,3+2*max_m_logtWT+20] <- 0 #TLAG CENTRAL
      theta[ind_subj,3+2*max_m_logtWT+21] <- 0 #TLAG PERIPHERAL
      for (ind_q in 1:N_t[ind_subj]) {
        Ad1[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,1] + 0.0
        CENTRAL[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,2] + 0.0
        PERIPHERAL[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,3] + 0.0
        CC[ind_subj,ind_q] <- (CENTRAL[ind_subj,ind_q] / VC[ind_subj,ind_q])
      }
    }
    eta_PPV_Q_prec <- (1/(pow(PPV_Q,2)))
    eta_PPV_VP_prec <- (1/(pow(PPV_VP,2)))
    eta_PPV_KA_prec <- (1/(pow(PPV_KA,2)))
    eta_PPV_TLAG_prec <- (1/(pow(PPV_TLAG,2)))
    T1[1:2,1:2] <- inverse(OMEGA1[,])
    mu1[1] <- 0.0
    mu1[2] <- 0.0
    OMEGA1[1,1] <- pow(PPV_CL,2)
    OMEGA1[1,2] <- CORR_CL_VC*PPV_CL*PPV_VC
    OMEGA1[2,1] <- CORR_CL_VC*PPV_CL*PPV_VC
    OMEGA1[2,2] <- pow(PPV_VC,2)
    MU_CL <- (log(0.1))
    MU_KA <- (log(0.362))
    MU_TLAG <- (log(1))
    MU_V <- (log(8))
    MU_VC <- (log(8))
    MU_Q <- (log(0.2))
    MU_VP <- (log(20))
    MU_PPV_CL <- (log(0.01))
    MU_PPV_V <- (log(0.3))
    MU_PPV_VC <- (log(0.1))
    MU_PPV_KA <- (log(0.1))
    MU_PPV_Q <- (log(0.1))
    MU_PPV_VP <- (log(0.1))
    VAR_CL <- 0.01
    VAR_V <- 0.09
    VAR_PPV_CL <- 1.0E-4
    VAR_PPV_V <- 0.001
    VAR_KA <- 0.001
    VAR_TLAG <- 0.001
    VAR_VC <- 0.03
    VAR_Q <- 0.01
    VAR_VP <- 0.01
    VAR_PPV_VC <- pow((log(pow(0.1,2))),2)
    VAR_PPV_KA <- pow((log(pow(0.1,2))),2)
    VAR_PPV_Q <- pow((log(pow(0.1,2))),2)
    VAR_PPV_VP <- pow((log(pow(0.1,2))),2)
    CORR_CL_V_MIN <- 0.1
    CORR_CL_V_MAX <- 0.2
    MU_RUV_PROP <- (log(0.1))
    MU_RUV_ADD <- (log(1.0E-4))
    VAR_RUV_PROP <- 0.1
    VAR_RUV_ADD <- 0.001
    CLmin <- 0.09
    CLmax <- 0.11
    BETA_CL_WT <- 0.75
    BETA_V_WT <- 1
    PPV_TLAG <- 0.1
    PPV_V <- 0.14
    PPV_KA <- 0.94
    RUV_PROP <- 0.11
    RUV_ADD <- 1.0E-4
    CORR_CL_V <- 0.28
    POP_KA <- 1.7
    PPV_CL <- 0.3
    POP_TLAG <- 1
    POP_V <- 8
    POP_CL <- 0.1
    POP_VC <- 0.1
    POP_Q <- 0.1
    POP_VP <- 0.1
    PPV_VC <- 0.1
    PPV_Q <- 0.1
    PPV_VP <- 0.1
    CORR_CL_VC <- 0.28
    POP_BETA_CL_WT <- 1
    POP_BETA_V_WT <- 1
    # PRIORS
}
