library(ggplot2) library(deSolve) library(gridExtra) library(cowplot) #signal <- data.frame(times = times, import = rep(0, length(times))) Text=read.csv("input.csv") #head(signal) #signal$import <- ifelse((trunc(signal$times) %% 2 == 0), 0, 1) #signal[8:12,] #head(signal) df <- read.csv("dataJUL.csv",col.names=c("DateTime", "DB", "RH","DP","P","WIND","WIND-DIR","SUN","RAIN","START")) df=df[complete.cases(df), ] df$DateTime=as.Date(df$DateTime) df$DB=(df$DB/10) input <- approxfun(df$DB, rule = 2) model <- function(t, y, parms) { with(as.list(c(parms, y)), { TE <- input(t) # <---- here dT1 = (-H1*(T1-T2)- (Hce+Hri)*(T1-TE))/K1 dT2 = (H2*(T2-T3)+ H1*(T1-T2))/K2 dT3 = (H3*(T3-T4)+ H2*(T2-T3))/K3 dT4 = (H4*(T4-T5)+ H3*(T3-T4))/K4 dT5 = ((Hci+Hri)*(T5-TA)+ H4*(T4-T5))/K5 dTA = -(A*(Hci+Hri)*(TA-T5))/KA # dy1 <- -((qv * pa * Ca * (y[1] - Text)) / (V * pa * Ca))+max(0,min(1,(20-y[1])*1))*((q2 * pa * Ca * (40-y[1])) / (V * pa * Ca)) # dy2 = 1 # dy2 <- y[2] list(c(dT1,dT2,dT3,dT4,dT5,dTA),TE=TE) # list(c(dy1),Text = Text,max(0,(min(1,(20-y[1])*1))),max(0,min(1,(20-y[1])*1))*((q2 * pa * Ca * (40-y[1]))),qv*pa*Ca*(20-Text)) }) } U=20 K=150 parms = c(H1=U/6,H2=U/3,H3=U/3,H4=U/6,K1=K/8,K2=K/4,K3=K/4,K4=K/4,K5=K/8,KA=5,Hce=9,Hre=9,Hci=9,Hri=9,A=10) out <- ode(y = c(T1=20,T2=20,T3=20,T4=20,T5=20,TA=20), times = seq(0,nrow(df),1), func = model, parms) out=as.data.frame(out) head(out,n=50) #parms = c(qv = 54, pa = 1.204, Ca = 1006, V = 3000) #model <- function(time, Tr,Text, parms) { #df <- read.csv("dataWEEK.csv",col.names=c("DateTime", "DB", "RH","DP","P","WIND","WIND-DIR","SUN","RAIN","START")) #df=df[complete.cases(df), ] #df$DateTime=as.Date(df$DateTime) #df$DB=(df$DB/10) # #Text=parms[5] #dTr <- -(parms[1] * parms[2] * parms[3] * (Tr - Text)) / (parms[4] * parms[2] * parms[3]) #dText = df$DB #list(dTr,Text) #}# #solution <- ode(y = 20, times =as.numeric(rownames(df)), func = model, parms = parms) #print(solution) #solution=as.data.frame(solution) #names(out) <- c("time", "Tr","Text","Sig","Heating","Loss") #out$Total <- cumsum((out$Heating/2)) #head(out) ##head(solution) # # Sample data for plot1 # Create ggplot objects plot1 <- ggplot(out, aes(x=time, y=TA)) + geom_line(color="red")+ geom_line(data=out, aes(x=time,y=TE),color="blue")+ #geom_hline(yintercept = 18)+ #geom_hline(yintercept = 20) #plot2 = ggplot(out, aes(x=time,y=Sig))+geom_line() #plot3 = ggplot(out, aes(x=time,y=Heating))+geom_line()+ #geom_line(data=out,aes(x=time,y=Loss),color="red") #plot4 <- ggplot(out, aes(x=time, y=Loss)) + geom_line() #plot5 = ggplot(out, aes(x=time, y=Error))+geom_line() # Open a PDF device to save the plots pdf("tx.pdf", width = 40, height = 20) # Arrange and print the plots using gridExtra grid.arrange(plot1,nrow = 1) # Or arrange and print the plots using cowplot # plot_grid(plot1, plot2, nrow = 1) # Close the PDF device to save the plots to the PDF file dev.off() #gg_plot <- ggplot(data = out, aes(x = time, y = Total)) + # geom_line() + # geom_line(aes(x = time, y = Text), color = "blue")+ # geom_line(aes(x=time,y=Sig),color="red")+ # labs(x = "Time Steps", y = "Room Temperature (Tr)") + # ggtitle("Room Temperature Over Time") ## #ggsave(filename = "room_temperature_plot.pdf", plot = gg_plot)