dleutnant / swmmr

R Interface for US EPA's SWMM
https://cran.r-project.org/package=swmmr
39 stars 15 forks source link

How can I use multicore and GPU processing in my code? #76

Open boram1024 opened 2 years ago

boram1024 commented 2 years ago

I'm doing parameter optimization using the SWMMR package and the SCE-UA package(hydromad). The problem is when i got the more variables(about 200 variables..), running time on my PC is so long(almost not working). From the googling, I found out that there are multicore or GPU processing. But, I don't know how to apply them to my code. For example, DEoptim package is serve parallel computing in control list(ex. parallelType = 1, 0). Is there any way like DEoptim pacakge or other.

library(swmmr) #Load package
library(DEoptim)
library(openxlsx)
library(readxl)
library(xts)
library(package='hydromad')
setwd("C:/Users/BORAM/Desktop/R_example/optimization/samho") #Assign working directory
#getwd() #Check working directory
inp_file<-file.path("C:/Users/BORAM/Desktop/R_example/optimization/samho/samho2.inp") #Read initial input file

swmm_file<-run_swmm(inp_file)

obs<-read_xlsx("obs2.xlsx")

obs2<-xts(obs$V1,order.by=obs$...1)

#write.csv(as.data.frame(obs),"obs.csv")

inp<-read_inp(swmm_file$inp)

nse <- function(x){1-sum((x[,1]-x[,2])^2)/sum((x[,1]-mean(x[,1]))^2)}
obj_fun <- function(x, inp, obs2){
  inp$subcatchments <- within(inp$subcatchments,{
    Perc_Imperv<-x[1:107]
    Width<-x[108:214]
  })
  inp$subareas<-within(inp$subareas,{
    "N-Imperv"<-x[215]
    "N-Perv"<-x[216]
    "S-Imperv"<-x[217]
    "S-Perv"<-x[218]
    PctZero<-x[219]
  })
  inp$infiltration<-within(inp$infiltration,{
    MaxRate<-x[220]
    MinRate<-x[221]
    Decay<-x[222]
  })
  inp$conduits<-within(inp$conduits,{
    Roughness<-x[223]
  })
#  inp$infiltration<-within(inp$infiltration,{
#    DryTime<-x[224]
#  })
#  inp$subcatchments <- within(inp$subcatchments,{
#    Perc_Slope<-x[225]
#  }) 

  tmp_inp<-file.path("C:/Users/BORAM/Desktop/R_example/optimization/samho/inp/sh2.inp")
#  tmp_inp<-tempfile()
  write_inp(inp, tmp_inp)
  swmm_file<-suppressMessages(run_swmm(tmp_inp, stdout = NULL))
#  on.exit(file.remove(unlist(swmm_file)))
  sim<-read_out(swmm_file$out,
                iType = 2,
                object_name = "1036-7500",
                vIndex = 0)[["1036-7500"]]$flow_rate
  nse(merge(obs2,sim))*-1
}

set.seed(1234)

system.time({sce<-SCEoptim(
  obj_fun,
  par=c(inp[["subcatchments"]][["Perc_Imperv"]], #Perc_Imperv
        inp[["subcatchments"]][["Width"]], #Perc_Imperv
        0.03, #N-Imperv
        0.3, #N-Perv
        2.5, #S-Imperv
        5.0, #S-Perv
        10, #PctZero
        254, #Max_R
        25.4, #Min_R
        2, #Decay
        0.015),
  lower=c(inp[["subcatchments"]][["Perc_Imperv"]]*0.5, #Perc_Imperv
          inp[["subcatchments"]][["Width"]]*0.5, #Perc_Imperv
          0.011, #N-Imperv
          0.1, #N-Perv
          1.6, #S-Imperv
          3.8, #S-Perv
          10, #PctZero
          76, #Max_R
          2.5, #Min_R
          1, #Decay
          0.011 ),  #Manning
  upper=c(inp[["subcatchments"]][["Perc_Imperv"]]*1.5,
          inp[["subcatchments"]][["Width"]]*1.5,
          0.02, #N-Imperv
          0.35, #N-Perv
          3.8, #S-Imperv
          6.4, #S-Perv
          30, #PctZero
          254, #Max_R
          25.4, #Min_R
          4, #Decay
          0.02),  #Manning
#  lower = c(0.011,0.1,1.6,3.8,10,76,2.5,1,0.011),
#  upper = c(0.02,0.35,3.8,6.4,30,254,25.4,4,0.02),
  control = list(
    trace=1,
    ncomplex=20,
    maxit=10),
  inp=inp,
  obs=obs2
  )})