-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNewFunction.R
122 lines (112 loc) · 4.35 KB
/
NewFunction.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
113
114
115
116
117
118
119
120
121
122
Pop2Calib <- function(x, calib, range){
nx = length(x)
ret = calib
para.name = names(range)
para.id = which(range[1, ]!=0)
if(length(x) != length(para.id)){
cat('\nLenghth of parameter does not match number of parameters in range file')
cat('\nParameter in range file:', para.name[para.id], '\n')
cat('Population is: ', x, '\n')
stop('\nERROR')
}
cn = para.name[para.id]
names(x) = cn
for(i in 1:nx){
# message(i,'/', nx, '\t', cn[i])
y = calib[cn[i]] #the value
dy = x[cn[i]]
lim = range[ c('min', 'max'), cn[i]] #limits
ilog = range['log', cn[i] ] #if log
if(ilog > 0){
sig = diff(log10(lim) ) * 0.5
val = 10^(log10(y) + sig * dy )
}else{
sig = diff(lim )
val = y + sig * dy
}
# message(cn[i],'\t', y,'\t', sig, '\t', dy, '\t', val)
# readline('goon')
val = min(lim[2], max(val, lim[1]) )
ret[ cn[i] ] = val
}
ret
}
pre.files <- function(iGen, pop, CV,
dir.out = as.character(as.matrix(CV$method$PATH_OUT))
){
prjname=CV$prjname
calib=CV$calib
calibrange = CV$range
njob =ncol(pop)
# dir.png = file.path(dir.out, 'png')
dir.calib = file.path(dir.out, 'cfg.calib')
dir.log = file.path(dir.out, 'log')
dir.calib = file.path(dir.out, 'cfg.calib')
dir.gen = file.path(dir.out, 'Generations')
tmp=lapply(list(dir.out, dir.log, dir.calib, dir.gen), dir.create, showWarnings = FALSE, recursive = TRUE)
# dir.create(dir.png, showWarnings = FALSE, recursive = TRUE)
# dir.create(dir.calib, showWarnings = FALSE, recursive = TRUE)
fn.calib = file.path(dir.calib, paste0(prjname, '_', iGen, '-', 1:njob, '.cfg.calib'))
fn.log = file.path(dir.log, paste0(prjname, '_', iGen, '-', 1:njob, '.calib.log'))
# fn.sim = file.path(outpath, paste0(prjname, '_', 1:njob), paste0(prjname, '.', ext, '.dat') )
dir.modelout = file.path('output', paste0(prjname, '_', 1:njob) )
# fn.png = file.path(pngpath, paste0(prjname, '_Step', iGen, '_Job',1:njob,'.png') )
calibmat=NULL
for(i in 1:ncol(pop)){
calibmat=rbind(calibmat, Pop2Calib(pop[,i], calib, calibrange))
}
# calibmat = apply(pop, 2, function(x) Pop2Calib(x, calib, calibrange))
print(calibmat)
fn.mat = file.path(dir.gen, paste0('Generation_', iGen, '.csv') )
write.table(calibmat, file=fn.mat, quote = FALSE, append = FALSE, row.names = TRUE, col.names = FALSE, sep = '\t')
for(i in 1:njob){
icb = calibmat[i, ]
write.config(icb, file = fn.calib[i], backup = FALSE)
}
fn.pop = file.path(dir.gen, paste0('DataPop', '.csv') )
fn.cal = file.path(dir.gen, paste0('DataCalib', '.csv') )
# x1 = cbind(t(pop))
# x2 = cbind('step'=iGen, t(calibmat))
x1=pop
x2=calibmat
if(iGen <= 1){
write.df(x=x1, file=fn.pop, append = FALSE, header = iGen, quite = TRUE, backup = FALSE)
write.df(x=x2, file=fn.cal, append = FALSE, header = iGen, quite = TRUE, backup = FALSE)
}else{
write.df(x=x1, file=fn.pop, append = TRUE, header = iGen, quite = TRUE, backup = FALSE)
write.df(x=x2, file=fn.cal, append = TRUE, header = iGen, quite = TRUE, backup = FALSE)
}
ret <- list(att=
data.frame(
'prjname' = prjname,
'fn.calib' = fn.calib,
'fn.log'= fn.log,
'dir.in' = file.path('input', prjname),
'dir.out' = file.path('output', paste0(prjname, '.out'), paste0(prjname, 1:njob) )
),
'fn.cmat' = fn.mat,
'calibmat' = calibmat)
}
# pop=rbind(rep(1, 48), rep(1,48), rep(1,48))
# x=pre.files(1, pop, CV = CV)
# pre.files(2, pop, CV = CV)
EXEC <- function(CV, CMD.EXE,
calibfile=as.character(calibfile),
outpath=as.character(outpath),
fn.log=as.character(fn.log),
walltime= CV$method$WALLTIME){
CMD.EXE=as.character(CMD.EXE)
cmd = paste(paste0(CMD.EXE), paste('-c', calibfile), paste('-o', outpath), prjname)
message('Walltime: ',walltime)
if(grepl('Darwin', Sys.info()['sysname'])){
message(cmd)
sret = system(cmd, intern = TRUE, ignore.stdout = FALSE,
wait = TRUE, timeout=walltime)
}else{
message(cmd)
sret = system(cmd, intern = TRUE, ignore.stdout = FALSE,
wait = TRUE, timeout=walltime)
}
write(sret, file= fn.log)
message('Finish model, to output:', outpath)
}