genericCarbarylModel <- function(t,state,parameters){
  with(
    as.list(c(state,parameters)),{
      
      ########################################  Concentrations ######################################################################
      
      # ------------------------------LIVER
      
      CVL = AVL/(VTBC*VOLLIVER)             # Concentration of carbaryl in liver tissue; umole/L; VTBC is fraction of tissue blood 
      CL = AL /((1-VTBC)*VOLLIVER)          # Concentration of carbaryl in liver blood; umole/L 
      CLIV_uM=(AVL+AL)/VOLLIVER				    	# Carbaryl liver concentration (uM)												
      CLIV=(AVL+AL)*MW*0.001/VOLLIVER       # Carbaryl liver concentration (ug/mL) 
      
      #------------------------------ FAT
      
      CVF = AVF/(VTBC*VOLFAT)               # Concentration of carbaryl in fat tissue; umole/L; VTBC is fraction of tissue blood 
      CF= AF /((1-VTBC)*VOLFAT)             # Concentration of carbaryl in fat blood; umole/L 
      CFAT_uM=(AVF+AF)/VOLFAT					    	# carbaryl fat concentration (uM)												
      CFAT=(AVF+AF)*MW*0.001/VOLFAT         # carbaryl fat concentration (ug/mL)
      
      #------------------------------- REST OF THE BODY
      
      CVR = AVR/(VTBC*VOLREST)              # Concentration of carbaryl in rest of the body tissue; umole/L; VTBC is fraction of tissue blood
      CR= AR /((1-VTBC)*VOLREST)            # Concentration of carbaryl in rest of the body blood; umole/L
      CREST_uM=(AVR+AR)/VOLREST			      	# Carbaryl REST OF THE BODY concentration (uM) 													
      CREST=(AVR+AR)*MW*0.001/VOLREST       # Carbaryl REST OF THE BODY concentration (ug/mL)
      
      #-------------------------------- BRAIN
      
      CVB = AVB/(VTBC*VOLBRAIN)             # concentration of carbaryl in brain tissue; umole/L; VTBC is fraction of tissue blood 
      CB= AB /((1-VTBC)*VOLBRAIN)           # Concentration of carbaryl in brain blood; umole/L
      CBRN_uM=(AVB+AB)/VOLBRAIN			        # Carbaryl brain concentration (uM)														
      CBRN=(AVB+AB)*MW*0.001/VOLBRAIN       # Carbaryl brain concentration (ug/mL)
      PrcntBACh = 100*ABACh/(ABACh+ABAChi)  # % AChE remaining activity compared to control 
      
      
      #------------------------------- PlASMA
      
      CPLS = APLS/VOLPLS                                             # Carbaryl conc in plasma (umole/L) = uM 
      CV = (QLIV*CVL+QFAT*CVF+QREST*CVR+QBRAIN*CVB)/CARDOUTP         # mixed venous blood concentration (umole/L)
      CPL_um = CPLS                                                  # plasma concentration of carbaryl (umole/L) = uM
      CPL = CPLS*MW*0.001                                            # plamsa concentration of carbaryl (ug/mL)   
      
      #------------------------------- RBC
      
      CRBC = ARBC/VOLRBC                                             # RBC concentration of carbaryl (umole/L) = uM
      CRBC_um = CRBC                                                 # RBC concentration of carbaryl (umole/L) = uM
      CRB = CRBC*MW*0.001                                            # RBC concentration (ug/mL) 
      ABLD = APLS+ARBC                                               # carbaryl amount in Plasma and RBC
      PrcntRBCACh = 100*ARBCACh/(ARBCACh+ARBCAChi)                   # % AChE remaining activity compared to control
      
      ################################################ EXPOSURES ###############################################################################
      
      RINHSWTCH <- 0
      RDRMSWTCH <- 0
      
      # ---------------------- Oral Exposure
      
      RTOTODOSE <- 0                                # Daily oral dose (umol)
      RTOTDDOSE <- 0                                # Daily dinking dose (umol)
      
      avlbl_dose<- KA *(ODOSE+DDOSE)                # Amount of carbaryl available to the system based on KA
      
      RODOSE <- -KA*ODOSE                           # amount of oral dose remaining at the site of absorption i.e the GI tract
      RDDOSE <- -KA*DDOSE                           # amount of drinking dose remaining at the site of absorption i.e the GI tract
      
      # Rate of carbaryl in gut
      
      RANABSC <- (1-FA)*avlbl_dose                  # Rate of carbaryl dose not available in the GI tract for absorption  
      RABSC <- FA*avlbl_dose*(1-FM)                 # Rate of carbaryl dose available in the GI tract for absorption after metabolism (1-FM)
      RABSN<- FA*avlbl_dose*FM                      # Rate of carbaryl dose available in the GI tract for absorption as 1-naphthol
      
      #---------------------- Inhalation Exposure
      
      RINH <- INHSWTCH*QALV*CINH                    # Rate of inhalation; umole/hr
      IINH <- RINH                                  # Rate of inhalation; umole/hr
      REXH <- INHSWTCH*QALV*CPLS/PPA                # Rate of exhalation; umole/hr
      
      #----------------------- Dermal Exposure
      
      RDRM <- DRMSWTCH*(RDRMEXP*EXPAREA)            # Rate of dermal dose; umole/hr
      IDRM <- RDRM                                  # Rate of dermal dose; umole/hr  
      
      
      ####################################### MODEL Equations ((diffusion limited description for all tissues) ####################################
      
      #----------------------------------------- Rate of carbaryl in tissues----------------------------------------------------------------------
      
      #----------------------------------------- Liver
      
      LRAM =  VKM1*(CL*FuLIV)                                        # Rate of oxidative metabolism in liver (umole/hr)
      RAVL = QLIV*(CPLS - CVL) + RABSC  +PAL*(CL/PLIV - CVL)-KC*AVL  # Rate of carabryl in liver blood ; (umole/hr) 
      RAL =  PAL*(CVL-CL/PLIV)-LRAM -KC*AL                           # Rate of carbaryl in liver tissue; (umole/hr)
      
      #----------------------------------------- Fat
      
      RAVF = QFAT*(CPLS - CVF) + PAF*(CF/PFAT - CVF)-KC*AVF	        # Rate of carabryl in fat blood; (umole/hr)   
      RAF = PAF*(CVF-CF/PFAT)  -KC*AF                               # Rate of carabryl in fat tissue; (umole/hr)
      
      #----------------------------------------- Rest of the body
      
      RAVR = QREST*(CPLS - CVR) + PAR*(CR/PREST - CVR)-KC*AVR	       # Rate of carabryl in Rest of the body tissue blood; (umole/hr) 
      RAR = PAR*(CVR-CR/PREST)-KC*AR                                 # Rate of carabryl in Rest of the body tissue ; (umole/hr)   
      
      #---------------------------------------- Brain
      
      #Brain AcHE Depression
      
      RABI = KiAChB*ABACh*(CB*FuBRN)                                          # rate of carbaryl binding to brain AchE (umole/hr; bimolecular reaction)
      RABACh = KsAChB-ABACh*KdAChB - KiAChB*ABACh*(CB*FuBRN) + ABAChi*KrAChB  # Rate of change in brain AchE with carbaryl interaction (umole/hr)
      RABAChi = KiAChB*ABACh*(CB*FuBRN)- ABAChi*KrAChB                        # Rate of change in brain AchE active sites (umole/hr) 
      
      RAVB = QBRAIN*(CPLS - CVB) + PAB*(CB/PBRAIN - CVB)-KC*AVB	              # rate of carbaryl in brain tissue blood (umole/hr)  
      RAB = PAB*(CVB-CB/PBRAIN) -KC*AB -RABI                                  # brain tissue diffusion rate w/ Che interaction (umole/hr)   
      
      
      #---------------------------------------- Plasma
      
      RAPLSI = KiAChPLS*APLSACh*CPLS*FuPLS+KiBChPLS*APLSBCh*CPLS*FuPLS                             # rate of carbaryl binding to blood ChEs (umole/hr; bimolecular reaction)
      PLSRAM = VKM2*(CPLS*FuPLS)                                                                   # Rate of Carbaryl metabolism in plasma to 1-naphthol;(umole/hr); No metabolism in RBC
      RAPLS = CARDOUTP *(CV-CPLS)-PLSRAM-PARBC*(CPLS-CRBC/PRBC)-KC*APLS-RAPLSI +RINH-REXH+RDRM     # Rate of carbaryl in plasma; (umole/hr) 
      
      # PLASMA AChe related calculation; Human do not have AchE in plasma; thisis for rat model
      
      RAPLSACh = KsAChPLS - APLSACh*KdAChPLS - KiAChPLS*APLSACh*CPLS*FuPLS + APLSAChi*KrAChPLS    # Rate of change in plasma AchE with carbaryl interaction (umole/hr)
      RAPLSAChi = KiAChPLS*APLSACh*CPLS*FuPLS - APLSAChi*KrAChPLS                                 # Rate of change in plasma AchE active sites (umole/hr)
      
      # PLASMA BChe related calculation; Human has BuChE in plasma 
      
      RAPLSBCh = KsBChPLS - APLSBCh*KdBChPLS - KiBChPLS*APLSBCh*CPLS*FuPLS + APLSBChi*KrBChPLS    # Rate of change in plasma BchE with carbaryl interaction (umole/hr)
      RAPLSBChi = KiBChPLS*APLSBCh*CPLS*FuPLS - APLSBChi*KrBChPLS                                 # Rate of change in plasma BchE active sites (umole/hr)
      
      
      #--------------------------------------- RBC AChE related calculation
      
      RARBCI = KiAChRBC*ARBCACh*CRBC*FuRBC                                                        # Rate carbaryl bound to blood ChEs (umole/hr)
      RARBC = PARBC*(CPLS-CRBC/PRBC) -KC*ARBC-RARBCI                                              # Rate of carbaryl in RBC (umole/hr)
      
      
      RARBCACh = KsAChRBC - ARBCACh*KdAChRBC - KiAChRBC*ARBCACh*CRBC*FuRBC + ARBCAChi*KrAChRBC    # Rate of change in RBC AchE with carbaryl interaction (umole/hr)
      RARBCAChi = KiAChRBC*ARBCACh*CRBC*FuRBC - ARBCAChi*KrAChRBC                                 # Rate of change in RBC AchE active sites (umole/hr)
      
      #---------------------------------------- Blood
      
      RABLDI = RAPLSI+RARBCI                                                                      # Rate of carbaryl in blood (Plasma and RBC)
      
      ######################################## 1-Napthol Single compartment ############################################################################
      
      RAUN = UCLN*AAN                                                                             #Rate of urinary clearance for 1-napthol
      RAAN = KC*(AL+AVL+AF+AVF+AR+AVR+AVB+AB+APLS+ARBC)+PLSRAM+RABI+RAPLSI+RARBCI+RABSN-RAUN        #Rate of change for amount in common naphthol compartment
      
      ######################################## Area Under Curves ############################################################################
     
      RAUCBRAIN <- CBRN
      RAUCLIVER <- CLIV
      RAUCFATOSE <- CFAT
      RAUCBLOOD <- CPL
      RAUCRBC <- CRBC
      
      ######################################### MAss Balance #############################################################################################
      
      TOTDOSE<-AINH+ADRM+TOTODOSE+TOTDDOSE
      TOTC<-AVL+AL+AVF+AF+AVR+AR+AVB+AB+ABLD+ODOSE+DDOSE+ANABSC
      TOTN<-AAN
      TOTCLEAR<- LAM+AEXH+ AUN
      TMASS<- TOTDOSE-TOTC-TOTN-TOTCLEAR
      
      list(c(RINHSWTCH,RDRMSWTCH,RODOSE,RTOTODOSE,RDDOSE,RTOTDDOSE,RINH,REXH,RDRM,RANABSC,RABSC,RABSN,RAVL, RAL,LRAM, RAVF, RAF, RAVR, RAR,RABI,RABACh, RABAChi,RAVB, RAB, PLSRAM,RAPLS,RAPLSACh,RAPLSAChi,
             RAPLSBCh,RAPLSBChi,RARBC,RARBCACh,RARBCAChi,RABLDI,RAAN,RAUN,RAUCBRAIN, RAUCLIVER, RAUCFATOSE, RAUCBLOOD,RAUCRBC),
           "qalv"=QALV,"cvl"= CVL,"cl"=CL,"clivum"=CLIV_uM,"cliv"=CLIV,"cvf"= CVF,"cf"=CF,"cfatum"=CFAT_uM,"cfat"=CFAT,"cvr"=CVR,"cr"=CR,
           "crestum"=CREST_uM,"crest"=CREST,"cvb"=CVB,"cb"=CB,"cbrnum"=CBRN_uM,"cbrn"=CBRN,"cpls"=CPLS,"cv"=CV,"cplum"=CPL_um,
           "cpl"=CPL,"crbcum"=CRBC_um, "crbc" = CRB,"pctBACh" = PrcntBACh,"pctRBCACH"=PrcntRBCACh,"InstInhDose"=IINH,"InstDrmDose"=IDRM,
           "bal"=TMASS)
    }
  )
}