-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaux fun.R
112 lines (95 loc) · 3.85 KB
/
aux fun.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
### auxilary functions ###
### import SLV function ###
ru.import.slv<-function(modelName, outs=NULL, delTemp=T) ## create global variables params, initials, .dll with ODEs RHS and outputs
{
### part to read SLV and write files "RHS.temp.txt", "IV.temp.txt", "variables.temp.txt"
slv<-readLines(paste(modelName,".slv",sep=""))
counter<-vector(mode="numeric", length=length(slv))
ct<-0
vars_start<-NULL
vars_end<-NULL
for (i in 1:length(slv))
{
t1<-sum(attr(gregexpr("#", slv[i])[[1]],"match.length"))
if (t1<0) t1<-0
ct<-ct+t1
counter[i]<-ct
if (slv[i]==">Compound Names") vars_start<-i+1
if (slv[i]==">Mechanism Rate Low expression -Kinetic or Elementary constants") vars_end<-i-1
}
rhs<-slv[counter==38]
rhs<-gsub(";","#",rhs)
rhs<-gsub("//","#",rhs)
rhs<-gsub('[',"__",rhs,fixed=T)
rhs<-gsub("]","__",rhs)
par<-slv[counter==39]
par<-gsub(";","#",par)
par<-gsub("//","#",par)
vars<-slv[vars_start:vars_end]
vars<-gsub("#","",vars)
write.table(rhs, "RHS.temp.txt", quote =F, col.names =F,row.names =F)
write.table(par, "IV.temp.txt", quote =F, col.names =F,row.names =F)
write.table(vars, "variables.temp.txt", quote =F, col.names =F,row.names =F)
### end of reading SLV
################################ start second calculations from here ###################
### reading initials
initials0<-read.table("variables.temp.txt", sep="=", stringsAsFactors=F, comment.char="")
initials<-vector(mode="numeric", length=length(initials0[,1]))
names(initials)<-initials0[,1]
### reading parameters
params0<-read.table("IV.temp.txt", sep="=", stringsAsFactors=F, comment.char="#")
params1<-params0[!duplicated(params0[,1],fromLast=T),] #delete duplicated iv
params2<-params1[!(params1[,1] %in% names(initials)),] # exclude initials from iv
params<-params2[,2]
names(params)<-params2[,1]
### initializing variables
initials1<-params1[!(params1[,1] %in% names(params)),] # exclude not variables from iv
initials2<-initials1[,2]
names(initials2)<-initials1[,1]
initials<-ru.combine(initials,initials2)
### reading functions
fun<-read.table("RHS.temp.txt", sep="=", stringsAsFactors=F, comment.char="#")
######################### create .DLL from SLV ########################
### create .h file
mainLine1<- paste("/* file ", modelName, ".h */ \n", sep="")
mainLine1<- paste(mainLine1, "#define Npars ", length(params), "
#define Nout ", length(outs),"\n\n", sep="")
for (i in 1:length(params))
mainLine1<- paste(mainLine1, "#define ", names(params)[i] , " parms[",i-1,"]\n", sep="") #parameters
mainLine1<-paste(mainLine1,"\n")
for (i in 1:length(initials))
mainLine1<- paste(mainLine1, "#define ", names(initials)[i] , " y[",i-1,"]\n", sep="") #variables
mainLine1<-paste(mainLine1,"\n")
mainLine1<-paste(mainLine1,"\n")
#cat(mainLine1, file=paste(modelName,".h",sep=""))
### create .c file
mainLine2<- paste(mainLine1,"/* file pmn_ac4.c*/
#include <R.h>
static double parms[Npars];
/* initializer */
void initmod(void (* odeparms)(int *, double *))
\t {
\t int N=Npars;
\t odeparms(&N,parms);
\t} \n
/* Derivatives and output variables */
void derivs (int *neq, double *t, double *y, double *ydot, double *yout, int *ip)
\t{
\t if (ip[0] <Nout) error(\"nout should be at least Nout\");
")
for (i in 1:length(fun[,1]))
mainLine2<- paste(mainLine2, "\t double ", fun[i,1] , "=", fun[i,2], ";\n", sep="") #functions
for (i in 1:length(initials))
mainLine2<-paste(mainLine2," \t ydot[",i-1, "] = F__",i,"__; \n", sep="")
if (length(outs)>0)
for (i in 0:(length(outs)-1))
mainLine2<- paste(mainLine2, "\t yout[", i, "]=", outs[i], ";\n", sep="") #functions
mainLine2<-paste(mainLine2,"\t } \n", sep="")
cat(mainLine2, file=paste(modelName,".c",sep=""))
### create DLL
vvv <- paste("R CMD SHLIB ",modelName,".c", sep="")
system(vvv)
if (delTemp)
file.remove(c("RHS.temp.txt","IV.temp.txt","variables.temp.txt",paste(modelName,".o",sep="")))
list(name=modelName, params=params, initials=initials, dll=modelName, outs=outs)
}