# 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: magni2006
# File: Executable_Magni_2006_diabetes_MinimalModel
# Dated: Mon Aug 01 23:42:32 CEST 2016

model {
  for (ind_subj in 1:N_subj) {
    for (ind_t in 1:N_t[ind_subj]) {
      INSinterp[ind_subj,ind_t] <- interp.function(grid[ind_subj,ind_t], grid_INS[ind_subj,1:N_t_INS[ind_subj]], INS[ind_subj,1:N_t_INS[ind_subj]])
      Y_G_mean[ind_subj,ind_t] <- (GLUC[ind_subj,ind_t] + 0)
      Y_G_prec[ind_subj,ind_t] <- 1 / ((pow(((CV * GLUC[ind_subj,ind_t])),2))*SIGMA_RES_G)
      Y_G[ind_subj,ind_t] ~ dnorm(Y_G_mean[ind_subj,ind_t], Y_G_prec[ind_subj,ind_t])
      Y_G_res[ind_subj,ind_t] <- Y_G[ind_subj,ind_t]-Y_G_mean[ind_subj,ind_t]
    }
    SG[ind_subj] <- pop_SG
    SI[ind_subj] <- pop_SI
    P2[ind_subj] <- pop_P2
    GB[ind_subj] <- pop_GB
    IB[ind_subj] <- pop_IB
    G0[ind_subj] <- pop_G0
    der99wb_unipv[ind_subj,1:N_t[ind_subj], 1:2]<- 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_INS
    theta[ind_subj,3] <- N_t_INS[ind_subj]
    theta[ind_subj,3+2*max_m_INS +1] <- pop_GB
    theta[ind_subj,3+2*max_m_INS +2] <- pop_IB
    theta[ind_subj,3+2*max_m_INS +3] <- logSG_POP
    theta[ind_subj,3+2*max_m_INS +4] <- logP2_POP
    theta[ind_subj,3+2*max_m_INS +5] <- logSI_POP
    for (i in 1 : max_m_INS){ 
	theta[ind_subj,i+3] <- grid_INS[ind_subj,i]
	}
	for (i in 1 : max_m_INS){ 
	theta[ind_subj,i+3+max_m_INS] <- INS[ind_subj,i]
	}
      theta[ind_subj,3+2*max_m_INS+6] <- G0[ind_subj] #IC GLUC
      theta[ind_subj,3+2*max_m_INS+7] <- 0 #IC X_REMOTE
      theta[ind_subj,3+2*max_m_INS+8] <- 1 #F GLUC
      theta[ind_subj,3+2*max_m_INS+9] <- 1 #F X_REMOTE
      theta[ind_subj,3+2*max_m_INS+10] <- 0 #TLAG GLUC
      theta[ind_subj,3+2*max_m_INS+11] <- 0 #TLAG X_REMOTE
      for (ind_q in 1:N_t[ind_subj]) {
        GLUC[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,1] + G0[ind_subj]
        X_REMOTE[ind_subj,ind_q] <- der99wb_unipv[ind_subj,ind_q,2] + 0
      }
    }
    MU_SG_POP <- (-(3.7))
    MU_SI_POP <- (-(7.5))
    MU_P2_POP <- (-(3.0))
    MU_G0_POP <- 5.6
    SIGMA_SG_POP <- 0.45
    SIGMA_SI_POP <- 0.71
    SIGMA_P2_POP <- 0.63
    SIGMA_G0_POP <- 0.12
    pop_SG <- (exp(logSG_POP))
    pop_SI <- (exp(logSI_POP))
    pop_P2 <- (exp(logP2_POP))
    pop_G0 <- (exp(logG0_POP))
    SIGMA_RES_G <- 1
    CV <- 0.02
    pop_GB <- 82
    pop_IB <- 7
    # PRIORS
    logSG_POP ~ dnorm(MU_SG_POP, logSG_POP_prec)
    logSG_POP_prec <- 1/pow(SIGMA_SG_POP,2)
    logG0_POP ~ dnorm(MU_G0_POP, logG0_POP_prec)
    logG0_POP_prec <- 1/pow(SIGMA_G0_POP,2)
    logSI_POP ~ dnorm(MU_SI_POP, logSI_POP_prec)
    logSI_POP_prec <- 1/pow(SIGMA_SI_POP,2)
    logP2_POP ~ dnorm(MU_P2_POP, logP2_POP_prec)
    logP2_POP_prec <- 1/pow(SIGMA_P2_POP,2)
}
