Skip to content

Instantly share code, notes, and snippets.

@gufodotto
Created June 3, 2012 14:43
Show Gist options
  • Save gufodotto/2863784 to your computer and use it in GitHub Desktop.
Save gufodotto/2863784 to your computer and use it in GitHub Desktop.
Lorentz_Controls
# source("C:/Users/Luca/Documents/My Dropbox/Software/gWidgets.r")
rm(list=ls(all=TRUE))
library(ggplot2)
library(gWidgets)
options("guiToolkit"="RGtk2")
library(deSolve)
# from: http://wiki.stdout.org/rcookbook/Graphs/Multiple%20graphs%20on%20one%20page%20(ggplot2)/
multiplot <- function(..., plotlist=NULL, cols) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# Make the panel
plotCols = cols # Number of columns of plots
plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))
vplayout <- function(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)
# Make each plot, in the correct location
for (i in 1:numPlots) {
curRow = ceiling(i/plotCols)
curCol = (i-1) %% plotCols + 1
print(plots[[i]], vp = vplayout(curRow, curCol ))
}
}
# set up all functions...
Lorenz<-function(t, state, parameters) {
with(as.list(c(state, parameters)),{
# rate of change
dX <- a*X + Y*Z
dY <- b * (Y-Z)
dZ <- -X*Y + c*Y - Z
# return the rate of change
list(c(dX, dY, dZ))
}
) # end with(as.list ...
}
PlotPars <- function(parameters, times) {
plot(x=NULL, y=NULL, xlim=c(-1,1), ylim=c(-1,1), bty='n', xlab='', ylab='', xaxt='n', yaxt='n')
text(0,0,labels=sprintf("a=%g b=%g c=%g",parameters['a'], parameters['b'], parameters['c']))
text(0,-1,labels=sprintf("times from %g to %g",times[1], times[length(times)]))
}
RunModel <- function(h, ...) {
parameters <- c(a = -8/3, b = as.numeric(svalue(Ber)), c = as.numeric(svalue(Cer)))
state <- c(X = 1, Y = 1, Z = 1)
times <- seq(0, svalue(timer), by = 0.001)
out<-ode(y = state, times = times, func = Lorenz, parms = parameters)
out<-cbind(out, newX=0, newY=0)
# assign('out', ode(y = state, times = times, func = Lorenz, parms = parameters), envir=.GlobalEnv)
# pXY<-ggplot(as.data.frame(out))+geom_path(aes(X, Y, col=time))
# pXZ<-ggplot(as.data.frame(out))+geom_path(aes(X, Z, col=time))
# pYZ<-ggplot(as.data.frame(out))+geom_path(aes(Y, Z, col=time))
# multiplot(pXY,pXZ,pYZ, cols=3)
pXY<-ggplot(as.data.frame(out)) +geom_path(aes(X, Y, col=time, alpha=Z)) + opts(legend.position = "none")
pZY<-ggplot(as.data.frame(out)) +geom_path(aes(Z, Y, col=time, alpha=X)) + opts(legend.position = "none")
pXZ<-ggplot(as.data.frame(out)) +geom_path(aes(X, Z, col=time, alpha=Y)) + opts(legend.position = c(1.1,0.5))
p3D<-ggplot(as.data.frame(out)) +theme_invisible() +geom_path(aes(X*Y, X*Z, col=time, alpha=Y*Z)) + scale_alpha(range = c(0.4, 0.8)) + opts(legend.position = 'none')
windows(width= 10, height= 10)
multiplot(pXY,pZY,pXZ,p3D, cols=2)
# plot(out)
# plot3d(out)
# PlotPars(parameters, times)
}
win_ctrls <- gwindow("Lorenz controls")
grp_ctrls <- ggroup(container = win_ctrls, horizontal = FALSE)
# if you click a button, execute a function which runs the model and hopefully plots it.
run_me <- gbutton("run me!!!",
container = grp_ctrls,
handler = RunModel
)
Aer<-gedit(-8/3, cont = grp_ctrls, handler = RunModel)
Ber<-gedit("-10", cont = grp_ctrls, handler = RunModel)
Cer<-gedit("28", cont = grp_ctrls, handler = RunModel)
timer<-gspinbutton(value=100, from = 1, to = 1000, by = 1, horizontal = T,
cont = grp_ctrls, handler = RunModel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment