Skip to content

Commit

Permalink
several updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sbednarz authored Oct 10, 2020
1 parent cf847aa commit 4fd3074
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 21 deletions.
68 changes: 62 additions & 6 deletions slimmc/kmc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,24 @@ proc initSimulation() =
nD = int(cD0*V_MC*N_A)


proc printPostInitMsg() =
echo "simulation initialized"

proc printInitialState() =
echo "initial state:"
echo "nI=", nI
echo "nRx=", nRx
echo "nM=", nM
echo "nPx=", nPx
echo "nD=", nD
echo "N=", nI+nRx+nM+nPx+nD
echo "simulation is running..."

proc printPostInitMsg() =
echo "simulation initialized"
echo "simulation time from 0 to ", breakpoints[breakpoints.len-1].time,"s, ",breakpoints.len," of breakpoints"
if flags["listinitialstate"]==1:
printInitialState()
echo "simulation is running ..."




proc gotoTime(t0: float, t_step: float) =
Expand Down Expand Up @@ -172,24 +180,72 @@ proc runSimulation() =
var time1 = 0.0
var step = 0
var nSteps = breakpoints.len
var timeEnd = breakpoints[breakpoints.len-1].time

var realtime0 = now()
var realtime1 = now()
var duration = realtime1 - realtime0
var s = 0

var percent = 0
var percent_flag = 0

if breakpoints[step].time == 0:
runActions(step)
echo "t=", time1, "s (", step+1,"/",nSteps,")"
if flags["outputeverystep"]==1:
echo &"t={time1:.12f}s ({step+1}/{nSteps}) {duration.inSeconds()}s"
elif flags["outputpercent"]==1:
echo &"Progress/simulation time (s)/real time (s)"

while true:
inc (step)
if step == nSteps:
break

time1 = breakpoints[step].time
gotoTime(time0, time1)
runActions(step)
time0 = time1



if flags["outputeverystep"]==1:
realtime1 = now()
duration = realtime1 - realtime0
echo &"t={time1:.12f}s ({step+1}/{nSteps}) {duration.inSeconds()}s"

elif flags["outputpercent"]==1:
var progress = time1/timeEnd
if progress > 0.9 and percent_flag==0 and percent == 8:
percent_flag = 1
elif progress > 0.8 and percent_flag==0 and percent == 7:
percent_flag = 1
elif progress > 0.7 and percent_flag==0 and percent == 6:
percent_flag = 1
elif progress > 0.6 and percent_flag==0 and percent == 5:
percent_flag = 1
elif progress > 0.5 and percent_flag==0 and percent == 4:
percent_flag = 1
elif progress > 0.4 and percent_flag==0 and percent == 3:
percent_flag = 1
elif progress > 0.3 and percent_flag==0 and percent == 2:
percent_flag = 1
elif progress > 0.2 and percent_flag==0 and percent == 1:
percent_flag = 1
elif progress > 0.1 and percent_flag==0 and percent == 0:
percent_flag = 1

if percent_flag==1:
percent_flag = 0
inc percent
realtime1 = now()
duration = realtime1 - realtime0
s = ((int)duration.inSeconds())
echo &"{progress*100:.1f}%\t{time1:.12e}s\t{s}s"

if flags["outputeverystep"]==1:
echo "t=", time1, "s (", step+1,"/",nSteps,")"
elif flags["outputpercent"]==1:
realtime1 = now()
duration = realtime1 - realtime0
echo "t=", time0, "s (", step+1,$"/",nSteps,") ", duration.inSeconds(),"s (~",duration.inHours(),"min)"
s = ((int)duration.inSeconds())
echo &"100.0%\t{time1:.12e}s\t{s}s"
102 changes: 89 additions & 13 deletions slimmc/model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ var m: RegexMatch
let valid_keyword_parameters = toHashSet(["kd", "f", "ki", "kp", "ktc", "ktd",
"cI0", "cM0", "cRx0", "cPx0", "cD0", "V_MC", "MwM"])


var parameters_list = initOrderedTable[string, string]()
var breakpoints_list = initOrderedTable[float, seq[string]]()
var breakpoints_list = initOrderedTable[string, seq[string]]()

type
Breakpoint = tuple
Expand All @@ -52,12 +53,15 @@ proc loadModel(filename: string) =
for line in splitLines(data):
inc i

# a comment
if match(line, re"^(\#).*", m):
continue

# an empty line
if match(line, re"^\s*$", m):
continue

# parameter = val
if match(line, re"\s*([a-zA-Z_0-9]+)\s*\=\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\s*", m):
if valid_keyword_parameters.contains(line[m.group(0)[0]]):
parameters_list[line[m.group(0)[0]]] = line[m.group(1)[0]]
Expand All @@ -67,10 +71,48 @@ proc loadModel(filename: string) =
" : parameter ", m.group(0)
quit(-1)

if match(line, re"\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\:(.*)", m):
# command arg1
if match(line, re"\s*([a-zA-Z_0-9]+)\s+([a-zA-Z_0-9]+)", m):
var cmd = line[m.group(0)[0]]
var arg = line[m.group(1)[0]]

if cmd=="list":
if arg=="parameters":
flags["listparameters"]=1
elif arg=="breakpoints":
flags["listbreakpoints"]=1
elif arg=="initialstate":
flags["listinitialstate"]=1
else:
echo "slimmc: error in model file, check ", filename, " line ", i
quit(-1)
continue

elif cmd=="output":
if arg=="everystep":
flags["outputeverystep"]=1
elif arg=="percent":
flags["outputpercent"]=1
else:
echo "slimmc: error in model file, check ", filename, " line ", i,
" : command ", m.group(0)
quit(-1)
continue
else:
echo "slimmc: error in model file, check ", filename, " line ", i,
" : parameter ", m.group(0)
quit(-1)



# a series of breakpoints (loop)
if match(line, re"\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\s*\:\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\s*\:\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\s*\:\s*(.*)", m):

var actions: seq[string]
var time = parseFloat(line[m.group(0)[0]])
var actions_str = split(line[m.group(2)[0]], ",")
var t0 = parseFloat(line[m.group(0)[0]])
var dt = parseFloat(line[m.group(2)[0]])
var N = ((int)parseFloat(line[m.group(4)[0]]))
var actions_str = split(line[m.group(6)[0]], ",")
for a in actions_str:
if match(a, re"^\s*(conc)\s*$"):
actions.add(cmdconc)
Expand All @@ -87,15 +129,50 @@ proc loadModel(filename: string) =
echo "slimmc: error in model file \'", filename, "\' line ", i,
": syntax error in command \'", a, "\'"
quit(-1)

var i = 0
while i < N:
var time = t0 + dt*((float)i)
var time_str = &"{time:.12f}"
if breakpoints_list.hasKey(time_str):
breakpoints_list[time_str].add(actions)
else:
breakpoints_list[time_str]=actions
inc i
continue


# a breakpoint
if match(line, re"\s*([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\:(.*)", m):
var actions: seq[string]
var time = parseFloat(line[m.group(0)[0]])
var actions_str = split(line[m.group(2)[0]], ",")
for a in actions_str:
if match(a, re"^\s*(conc)\s*$"):
actions.add(cmdconc)
continue
if match(a, re"^\s*(poly)\s*$"):
actions.add(cmdpoly)
continue
if match(a, re"^\s*(dc)\s+(I|M|Rx|Px|D)\s+([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\s*$", m):
actions.add(cmddc)
actions.add(a[m.group(1)[0]])
actions.add(a[m.group(2)[0]])
continue

if breakpoints_list.hasKey(time):
echo "noplp"
echo "slimmc: error in model file \'", filename, "\' line ", i,
": redefinition of breakpoint at ", time
": syntax error in command \'", a, "\'"
quit(-1)

breakpoints_list[time] = actions
var time_str = &"{time:.12f}"
if breakpoints_list.hasKey(time_str):
breakpoints_list[time_str].add(actions)
else:
breakpoints_list[time_str]=actions
continue


echo "slimmc: error in model file \'", filename, "\' line ", i
quit(-1)

Expand Down Expand Up @@ -156,12 +233,11 @@ proc printVariables() =

proc initBreakpoints() =
if breakpoints_list.len == 0:
echo "error: breakpoints not defined, check the model file"
echo "error: breakpoint(s) must be defined, check the model file"
quit(-1)

breakpoints_list.sort do (a, b: (float, seq[string])) -> int: cmp(a[0], b[0])
breakpoints_list.sort do (a, b: (string, seq[string])) -> int: cmp(a[0], b[0])
for t, a in breakpoints_list.pairs:
breakpoints.add((time: t, actions: a))
breakpoints.add((time: parseFloat(t), actions: a))


proc printBreakpoints() =
Expand All @@ -170,14 +246,14 @@ proc printBreakpoints() =
var t = b.time
var a = b.actions
var j = 0
var str = &"{i+1} t={t}s => "
var str = &"{i+1} t={t:.12f}s => "
while j < a.len:
if a[j] == cmdconc:
str = str & "conc"
elif a[j] == cmdpoly:
str = str & "poly"
elif a[j] == cmddc:
str = str & &"dc {a[j+1]} {a[j+2]} "
str = str & &"dc {a[j+1]} {a[j+2]}"
j = j+2
if j < a.len-1:
str = str & ", "
Expand Down
6 changes: 4 additions & 2 deletions slimmc/slimmc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ printWelcomeMsg(modelfile)
loadModel(modelfile)

initVariables()
printVariables()
if flags["listparameters"]==1:
printVariables()

initBreakpoints()
printBreakpoints()
if flags["listbreakpoints"]==1:
printBreakpoints()

runSimulation()
8 changes: 8 additions & 0 deletions slimmc/variables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import random/mersenne
import tables

# Avogadro const
const N_A = 6.023e23
Expand Down Expand Up @@ -73,3 +74,10 @@ var
# homopolymerization
const monomerunit = 1

var flags = initTable[string, int]()
flags["listparameters"]=0
flags["listbreakpoints"]=0
flags["listinitialstate"]=0
flags["outputeverystep"]=0
flags["outputpercent"]=0

5 changes: 5 additions & 0 deletions slimmc/version.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var gtag="v1.0-2-gcf84-dirty"
var ghash="cf847aacf5b3133c9b03db60ad2902826e46a101"
var build="Sat, Oct 10, 2020 11:35:51 AM"
var sys="MINGW64_NT-10.0-18362"
var nimv="Nim Compiler Version 1.2.6 [Windows: amd64]"

0 comments on commit 4fd3074

Please sign in to comment.