warfarin_PKPD_turnover_design = designObj{
  DECLARED_VARIABLES{
  	INPUT_KA :: dosingTarget
  	CP_obs ::observation
  	PCA_obs ::observation
  }

  INTERVENTION{
 	admin1 : {type is bolus, input=INPUT_KA, amount=100, doseTime=[0] }
 	admin2 : {type is bolus, input=INPUT_KA, amount=120, doseTime=[0] }
 	admin3 : {type is bolus, input=INPUT_KA, amount=60, doseTime=[0] }
 	admin4 : {type is bolus, input=INPUT_KA, amount=113, doseTime=[0] }
 	admin5 : {type is bolus, input=INPUT_KA, amount=90, doseTime=[0] }
 	admin6 : {type is bolus, input=INPUT_KA, amount=135, doseTime=[0] }
 	admin7 : {type is bolus, input=INPUT_KA, amount=75, doseTime=[0] }
 	admin8 : {type is bolus, input=INPUT_KA, amount=105, doseTime=[0] }
 	admin9 : {type is bolus, input=INPUT_KA, amount=123, doseTime=[0] }
 	admin10 : {type is bolus, input=INPUT_KA, amount=85, doseTime=[0] }
 	admin11 : {type is bolus, input=INPUT_KA, amount=87, doseTime=[0] }
 	admin12 : {type is bolus, input=INPUT_KA, amount=117, doseTime=[0] }
 	admin13 : {type is bolus, input=INPUT_KA, amount=112, doseTime=[0] }
 	admin14 : {type is bolus, input=INPUT_KA, amount=95.5, doseTime=[0] }
 	admin15 : {type is bolus, input=INPUT_KA, amount=88.5, doseTime=[0] }
 	admin16 : {type is bolus, input=INPUT_KA, amount=93, doseTime=[0] }
 	admin17 : {type is bolus, input=INPUT_KA, amount=110, doseTime=[0] }
 	admin18 : {type is bolus, input=INPUT_KA, amount=115, doseTime=[0] }
 	admin19 : {type is bolus, input=INPUT_KA, amount=153, doseTime=[0] }
 	admin20 : {type is bolus, input=INPUT_KA, amount=125, doseTime=[0] }
  }
  SAMPLING{
	winPK : {type is simple, outcome=CP_obs, sampleTime = [0.0001, 24, 36, 48, 72, 96, 120] }
    winPD : { type is simple, outcome=PCA_obs, sampleTime = [0.0001, 24, 36, 48, 72, 96, 120] }
    # implies concurrent start, both start 0 unless define times
    sampPKPD : {type is combi, combination=[winPK,winPD] }
  }
  STUDY_DESIGN{
    set armSize=1
	arm3 : {
		interventionSequence=[{
			admin=admin3,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
	arm1 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin1,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
# Copy arm3 changing nb of subjects & sampling schedule
#	arm1 = arm3
#	arm1.armSize=3
#	arm1.interventionSequence.admin=admin1
	arm2 : {
		armSize = 4,
		interventionSequence=[{
			admin=admin2,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm2 = arm3
#	arm2.armSize=4
#	arm2.interventionSequence.admin=admin2
	arm4 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin4,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm4 = arm3
#	arm4.armSize=3
#	arm4.interventionSequence.admin=admin4
	arm5 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin5,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm5 = arm3
#	arm5.interventionSequence.admin=admin5
	arm6 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin6,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm6 = arm3
#	arm6.interventionSequence.admin=admin6
	arm7 : {
		armSize = 2,
		interventionSequence=[{
			admin=admin7,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm7 = arm3
#	arm7.armSize=2
#	arm7.interventionSequence.admin=admin7
	arm8 : {
		armSize = 2,
		interventionSequence=[{
			admin=admin8,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm8 = arm3
#	arm8.armSize=2
#	arm8.interventionSequence.admin=admin8
	arm9 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin9,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm9 = arm3
#	arm9.interventionSequence.admin=admin9
	arm10 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin10,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm10 = arm3
#	arm10.interventionSequence.admin=admin10
	arm11 : {
		armSize = 2,
		interventionSequence=[{
			admin=admin11,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm11 = arm3
#	arm11.armSize=2
#	arm11.interventionSequence.admin=admin11
	arm12 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin12,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm12 = arm3
#	arm12.interventionSequence.admin=admin12
	arm13 : {
		armSize = 2,
		interventionSequence=[{
			admin=admin13,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm13 = arm3
#	arm13.armSize=2
#	arm13.interventionSequence.admin=admin13
	arm14 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin14,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm14 = arm3
#	arm14.interventionSequence.admin=admin14
	arm15 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin15,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm15 = arm3
#	arm15.interventionSequence.admin=admin15
	arm16 : {
		armSize = 2,
		interventionSequence=[{
			admin=admin16,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm16 = arm3
#	arm16.armSize=2
#	arm16.interventionSequence.admin=admin16
	arm17 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin17,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm17 = arm3
#	arm17.interventionSequence.admin=admin17
	arm18 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin18,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm18 = arm3
#	arm18.interventionSequence.admin=admin18
	arm19 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin19,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm19 = arm3
#	arm19.interventionSequence.admin=admin19
	arm20 : {
		armSize = 3,
		interventionSequence=[{
			admin=admin20,
			start=0
		}],
		samplingSequence=[{
			sample=sampPKPD,
			start=0
		}]
	}
#	arm20 = arm3
#	arm20.interventionSequence.admin=admin20

# Alternative - define each arm as arm3 above with appropriate armSize & admin values	
  }
}

warfarin_PKPD_turnover_par = parObj{
   STRUCTURAL{
      POP_CL : {value=0.1,lo=0.01,hi=1} # L/h/70kg
      POP_V : {value=8,lo=0.01,hi=20} # L/70kg
      POP_KA : {value=0.362,lo=0.01,hi=24} # h
      POP_TLAG : {value=1,lo=0.01,hi=24} # h
      POP_PCA0 : {value=96.7,lo=0.01,hi=200}
      POP_EMAX : {value=1, lo=0, fix=true}
      POP_C50 : {value=1.2,lo=0.01,hi=10}
      POP_TEQ : {value=12.9,lo=0.01,hi=100}
	  BETA_CL_WT : { value = 0.75, fix = true }
	  BETA_V_WT : { value = 1, fix = true }
   }# end STRUCTURAL

   VARIABILITY{
	  PPV_CL : { value = 0.1 }
	  PPV_V : { value = 0.1 }
	  PPV_KA : { value = 0.1 }
	  PPV_TLAG : { value = 0.1, fix=true }
	  CORR_CL_V : { value = 0.01 }
      RUV_PROP : {value=0.05, lo=0 }
      RUV_ADD : {value=0.3, lo=0.0001 } # mg/L

      PPV_PCA0 : { value=0.1 }
      PPV_EMAX : { value=0, fix=true }
      PPV_C50 : { value=0.1 }
      PPV_TEQ : { value=0.1 }
      RUV_FX :  { value=4, lo=0 }
   }# end VARIABILITY

} # end of parameter object

warfarin_PKPD_turnover_mdl = mdlObj{
	IDV { T }

	VARIABILITY_LEVELS(reference=ID){
		ID : { level=2, type is parameter }
		DV : { level=1, type is observation }	
	}

	COVARIATES{
		WT
		logtWT = ln(WT/70)
	}
	
# The STRUCTURAL_PARAMETERS Block defines the structural model parameters.
   STRUCTURAL_PARAMETERS{
      POP_CL
      POP_V
      POP_KA
      POP_TLAG
      POP_PCA0
      POP_EMAX
      POP_C50
      POP_TEQ
      BETA_CL_WT
      BETA_V_WT
    }# end STRUCTURAL_PARAMETERS

   VARIABILITY_PARAMETERS{
      PPV_CL
      PPV_V
      CORR_CL_V
      PPV_KA
      PPV_TLAG
      RUV_PROP
      RUV_ADD

      PPV_PCA0
      PPV_EMAX
      PPV_C50
      PPV_TEQ
      RUV_FX
   }# end VARIABILITY_PARAMETERS

   RANDOM_VARIABLE_DEFINITION(level=ID){
      ETA_CL ~ Normal( mean=0, sd=PPV_CL) 
      ETA_V ~ Normal( mean=0, sd=PPV_V)
      :: {type is correlation, rv1=ETA_CL, rv2=ETA_V, value=CORR_CL_V}
      ETA_KA ~ Normal( mean=0, sd=PPV_KA)
      ETA_TLAG ~ Normal( mean=0, sd=PPV_TLAG)
      ETA_PCA0 ~ Normal( mean=0, sd=PPV_PCA0)
      ETA_EMAX ~ Normal( mean=0, sd=PPV_EMAX)
      ETA_C50 ~ Normal( mean=0, sd=PPV_C50)
      ETA_TEQ ~ Normal( mean=0, sd=PPV_TEQ)
   }# end RANDOM_VARIABLE_DEFINITION

   INDIVIDUAL_VARIABLES{
	    CL : {type is linear, trans is ln, pop = POP_CL, fixEff = {coeff=BETA_CL_WT, cov=logtWT} , ranEff = ETA_CL }
	    V : {type is linear, trans is ln, pop = POP_V, fixEff = {coeff=BETA_V_WT, cov=logtWT} , ranEff = ETA_V }
	    KA : {type is linear, trans is ln, pop = POP_KA, ranEff = ETA_KA }
	    TLAG : {type is linear, trans is ln, pop = POP_TLAG, ranEff = ETA_TLAG }

	  PCA0 : {type is linear, trans is ln, pop = POP_PCA0, ranEff = ETA_PCA0 }
	  EMAX : {type is linear, pop = POP_EMAX, ranEff = ETA_EMAX }
	  C50 : {type is linear, trans is ln, pop = POP_C50, ranEff = ETA_C50 }
	  TEQ : {type is linear, trans is ln, pop = POP_TEQ, ranEff = ETA_TEQ }   
   }# end INDIVIDUAL_VARIABLES

   MODEL_PREDICTION{
	COMPARTMENT {
			INPUT_KA      : {type is depot, modelCmt=1, to=CENTRAL, ka=KA, tlag=TLAG}
#			INPUT_CENTRAL : {type is direct, to = CENTRAL}
			CENTRAL       : {type is compartment, modelCmt=2}
                     	 :: {type is elimination, modelCmt=2, from=CENTRAL, v=V, cl=CL}
   		}# end COMPARTMENT

 	KPCA=ln(2)/TEQ
    RPCA=PCA0*KPCA
	DEQ{
      CC=CENTRAL/V
      DPCA=PCA
      PD=1-EMAX*CC/(C50+CC)			
      PCA : {deriv=RPCA*PD - KPCA*DPCA, init = PCA0}
   	  }# end ODE
   }# end MODEL_PREDICTION

	RANDOM_VARIABLE_DEFINITION(level=DV){
      EPS_CP ~ Normal( mean=0, var=1)
      EPS_PCA ~ Normal( mean=0, var=1)
	}

   OBSERVATION{
   	 CP_obs : {type is combinedError1, additive = RUV_ADD, proportional = RUV_PROP, eps = EPS_CP, prediction = CC } 
     PCA_obs : {type is additiveError, additive = RUV_FX, eps = EPS_PCA, prediction = PCA }
   }# end OBSERVATION
} # end of model object

# EVALUATE task - implicit version
warfarin_PKPD_turnover_taskEval = taskObj {
	EVALUATE{ 
	}
} # end of task object

# EVALUATE task - explicit version
warfarin_PK_ODE_taskOpt_Explicit = taskObj{ # Here we define explicitly all options for optimisation with PFIM & FW algorithm
        OPTIMISE {
			set optAlgo is simplex,
			fim = "P",
			previousFim = "",
			
# Suggested names of common options (options that would make sense to different software) - Options for methods (what we want the software to compute, 
# and the settings/options for these computations)
        graphOnly = false,
        typeIError = 0.05,
        powerComparison = false,
        nSubjectsComparison = false,
        equivalenceRange = [ln(0.8),ln(1.25)],
        powerEquivalence = false,
        nSubjectsEquivalence = false,
        typeIIError = 0.9,
        
# Graphical options (can be moved to SETTINGS if they only make sense to PFIM)     - Options for graphs   
        graphLogical = true,
        logLogical = false, 
 		namesDataX = ["Time"],
 		namesDataY = ["Concentration"],
		graphInfA=[0],
		graphSupA=[360] /#,
        y.rangeA = NULL #/
        
#			FEATURES{
#				sampleTime,
#				numberTimes=4
#			}
        
		# Define tool specific settings in block
		TARGET_SETTINGS(target = "PFIM"){ # here we explicit all elements of the task
			set project = "warfarin_PK_ODE_optimise",
			output = "Stdout_warfarin_PK_ODE_optimise.R",
			outputFIM = "",			
			option = 1,
			subjectsInput = 1,
			covariateModel = false,
			covariate_occModel = false
			}
		}
}


#warfarin_PKPD_turnover_taskEval_Explicit = taskObj {
#	EVALUATE{ 
## Suggested names of common options (options that would make sense to different software)
#        set FIM = "P", # FIMtype ?
#		previous.FIM = "",
#        graphOnly = FALSE,
#        typeIerror = 0.05,
#        powerComparison = FALSE,
#        nsubjectsComparison = F,
#        equivalenceRange = c(log(0.8),log(1.25)),
#        powerEquivalence = FALSE,
#        nsubjectsEquivalence = FALSE,
#        typeIIerror = 0.9
#        
## Graphical options (can be moved to SETTINGS if they only make sense to PFIM)       
#        graph.logical = TRUE,
#        log.logical = FALSE, 
# 		names.datax = c("Time"),
# 		names.datay = c("Concentration"),
#        graph.infA = c(0),
#        graph.supA = c(360),
#        y.rangeA = NULL
#        
#		# Define tool specific settings in block
#		SETTINGS(target = "PFIM"){ # here we explicit all elements of the task
#			set project = "warfarin_PKPD_turnover",
#			output = "Stdout_warfarin_PKPD_turnover_eval.R",
#			outputFIM = "",			
#			option = 1,
#			subjects.input = 1,
#			covariate.model = FALSE,
#			covariate_occ.model = FALSE
#			}
#		}
#	}
#} # end of task object

# EVALUATE MOG

warfarin_PKPD_turnover_mog = mogObj {
    OBJECTS{
		warfarin_PKPD_turnover_design : { type is designObj }
		warfarin_PKPD_turnover_mdl : { type is mdlObj }
		warfarin_PKPD_turnover_par : { type is parObj }
		warfarin_PKPD_turnover_taskEval : { type is taskObj }
	}
}
