-
Notifications
You must be signed in to change notification settings - Fork 13
/
OMJulia.jl
1042 lines (987 loc) · 38.6 KB
/
OMJulia.jl
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#=
This file is part of OpenModelica.
Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
c/o Linköpings universitet, Department of Computer and Information Science,
SE-58183 Linköping, Sweden.
All rights reserved.
THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
ACCORDING TO RECIPIENTS CHOICE.
The OpenModelica software and the OSMC (Open Source Modelica Consortium)
Public License (OSMC-PL) are obtained from OSMC, either from the above
address, from the URLs: http://www.openmodelica.org or
http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
distribution. GNU version 3 is obtained from:
http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
http://www.opensource.org/licenses/BSD-3-Clause.
This program is distributed WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
CONDITIONS OF OSMC-PL.
=#
module OMJulia
global IS_FILE_OMJULIA = false
using ZMQ
using Compat
using DataStructures
using LightXML
using DataFrames
if(VERSION >=v"1.0")
using Random
end
export SendExpression
include("parser.jl")
mutable struct OMCSession
sendExpression::Function
ModelicaSystem::Function
xmlparse::Function
createcsvdata::Function
getQuantities::Function
showQuantities::Function
getParameters::Function
getSimulationOptions::Function
getSolutions::Function
getInputs::Function
getOutputs::Function
getContinuous::Function
setParameters::Function
setSimulationOptions::Function
setInputs::Function
simulate::Function
linearize::Function
getLinearizationOptions::Function
setLinearizationOptions::Function
getLinearInputs::Function
getLinearOutputs::Function
getLinearStates::Function
sensitivity::Function
convertMo2FMU::Function
convertFmu2Mo::Function
buildModel::Function
simulationFlag
inputFlag
simulateOptions
overridevariables
simoptoverride
tempdir
currentdir
resultfile
filepath
modelname
xmlfile
csvfile
quantitieslist
parameterlist
inputlist
outputlist
continuouslist
linearOptions
linearfile
linearFlag
linearmodelname
linearinputs
linearoutputs
linearstates
linearquantitylist
context
socket
function OMCSession(omc = nothing)
this = new()
this.overridevariables=Dict()
this.simoptoverride=Dict()
this.quantitieslist=Any[]
this.parameterlist=Dict()
this.simulateOptions=Dict()
this.inputlist=Dict()
this.outputlist=Dict()
this.continuouslist=Dict()
this.currentdir=pwd()
this.filepath=""
this.modelname=""
this.resultfile=""
this.simulationFlag=""
this.inputFlag="false"
this.csvfile=""
this.tempdir=""
this.linearfile=""
this.linearFlag="false"
this.linearmodelname=""
this.linearOptions=Dict("startTime"=>"0.0", "stopTime"=>"1.0", "numberOfIntervals"=>"500", "stepSize"=>"0.002", "tolerance"=> "1e-6")
args2="--interactive=zmq"
args3="+z=julia."
if(VERSION >= v"1.0")
args4=Random.randstring(10)
else
args4=randstring(10)
end
if (Compat.Sys.iswindows())
#@assert omc == nothing "A Custom omc path for windows is not supported"
if(omc != nothing)
ompath=replace(omc,r"[/\\]+" => "/")
dirpath=dirname(dirname(omc))
## create a omc process with OPENMODELICAHOME set to custom directory
@info("Setting environment variable OPENMODELICAHOME=\"$ompath\" for this session.")
withenv("OPENMODELICAHOME"=>dirpath) do
open(pipeline(`$omc $args2 $args3$args4`))
end
else
omhome=""
try
omhome=ENV["OPENMODELICAHOME"]
catch Exception
println(Exception, "is not set, Please set the environment Variable")
return
end
ompath=replace(joinpath(omhome,"bin","omc.exe"),r"[/\\]+" => "/")
#ompath=joinpath(omhome,"bin")
## create a omc process with default OPENMODELICAHOME set in environment variable
withenv("OPENMODELICAHOME"=>omhome) do
open(pipeline(`$ompath $args2 $args3$args4`))
end
end
portfile=join(["openmodelica.port.julia.",args4])
else
if (Compat.Sys.isapple())
#add omc to path if not exist
ENV["PATH"]=ENV["PATH"]*"/opt/openmodelica/bin"
if (omc != nothing)
open(pipeline(`$omc $args2 $args3$args4`))
else
open(pipeline(`omc $args2 $args3$args4`))
end
else
if (omc != nothing)
open(pipeline(`$omc $args2 $args3$args4`))
else
open(pipeline(`omc $args2 $args3$args4`))
end
end
portfile=join(["openmodelica.",ENV["USER"],".port.julia.",args4])
end
#sleep(5)
fullpath=joinpath(tempdir(),portfile)
## Try to find better approach if possible, as sleep does not work properly across different platform
filedata=""
while true
# Necessary or Julia might optimize away checking isfile every iteration
global IS_FILE_OMJULIA = isfile(fullpath)
if(IS_FILE_OMJULIA)
filedata=read(fullpath,String)
break
end
end
this.context=ZMQ.Context()
this.socket =ZMQ.Socket(this.context, REQ)
ZMQ.connect(this.socket, filedata)
this.sendExpression = function (expr)
ZMQ.send(this.socket,expr)
message=ZMQ.recv(this.socket)
return (unsafe_string(message))
end
this.ModelicaSystem = function (filename, modelname, library=nothing)
this.filepath=filename
this.modelname=modelname
filepath=replace(abspath(filename),r"[/\\]+" => "/")
if(isfile(filepath))
loadmsg=this.sendExpression("loadFile(\""*filepath*"\")")
if(!Base.Meta.parse(loadmsg))
return this.sendExpression("getErrorString()")
end
else
return println(filename, "! NotFound")
end
#this.tempdir=replace(joinpath(pwd(),join(["zz_",Random.randstring(5),".tmp"])),r"[/\\]+" => "/")
#mkdir(this.tempdir)
this.tempdir=replace(mktempdir(),r"[/\\]+" => "/")
if(!isdir(this.tempdir))
return println(this.tempdir, " cannot be created")
end
this.sendExpression("cd(\""*this.tempdir*"\")")
if(library!=nothing)
if(isa(library,String))
if(isfile(library))
libfile=replace(abspath(library),r"[/\\]+" => "/")
libfilemsg=this.sendExpression("loadFile(\""*libfile*"\")")
if(!Base.Meta.parse(libfilemsg))
return this.sendExpression("getErrorString()")
end
else
libname=join(["loadModel(",library,")"])
this.sendExpression(libname)
end
elseif (isa(library,Array))
for i in library
if(isfile(i))
libfile=replace(abspath(i),r"[/\\]+" => "/")
libfilemsg=this.sendExpression("loadFile(\""*libfile*"\")")
if(!Base.Meta.parse(libfilemsg))
return this.sendExpression("getErrorString()")
end
else
libname=join(["loadModel(",i,")"])
this.sendExpression(libname)
end
end
end
end
this.buildModel()
end
this.buildModel = function()
buildmodelexpr=join(["buildModel(",this.modelname,")"])
buildModelmsg=this.sendExpression(buildmodelexpr)
parsebuilexp=Base.Meta.parse(buildModelmsg)
if(!isempty(parsebuilexp.args[2]))
this.xmlfile=replace(joinpath(this.tempdir,parsebuilexp.args[2]),r"[/\\]+" => "/")
xmlparse(this)
else
return this.sendExpression("getErrorString()")
end
end
this.getQuantities = function(name=nothing)
if(name==nothing)
return this.quantitieslist
elseif(isa(name,String))
return [x for x in this.quantitieslist if x["name"] == name]
elseif (isa(name,Array))
return [x for y in name for x in this.quantitieslist if x["name"]==y]
# qlist=Any[]
# for n in name
# for i in this.quantitieslist
# if(i["name"]==n)
# push!(qlist,i)
# end
# end
# end
# return qlist
end
end
## helper function to return getQuantities as table
function df_from_dicts(arr::AbstractArray; missing_value="missing")
cols = Set{Symbol}()
for di in arr union!(cols, keys(di)) end
df = DataFrame()
for col=cols
df[col] = [get(di, col, missing_value) for di=arr]
end
return df
end
## function which returns getQuantities as table
this.showQuantities = function(name=nothing)
q = this.getQuantities(name);
# assuming that the keys of the first dictionary is representative for them all
sym = map(Symbol,collect(keys(q[1])))
arr = []
for d in q
push!(arr,Dict(zip(sym,values(d))))
end
return df_from_dicts(arr)
end
this.getParameters = function (name=nothing)
if(name==nothing)
return this.parameterlist
elseif(isa(name,String))
return get(this.parameterlist,name,0)
elseif (isa(name,Array))
return [get(this.parameterlist,x,0) for x in name]
end
end
this.getSimulationOptions = function (name=nothing)
if (name==nothing)
return this.simulateOptions
elseif(isa(name,String))
return get(this.simulateOptions,name,0)
elseif(isa(name,Array))
return [get(this.simulateOptions,x,0) for x in name]
end
end
this.getContinuous = function (name=nothing)
if(this.simulationFlag=="")
if (name==nothing)
return this.continuouslist
elseif(isa(name,String))
return get(this.continuouslist,name,0)
elseif(isa(name,Array))
return [get(this.continuouslist,x,0) for x in name]
end
end
if(this.simulationFlag=="True")
if (name==nothing)
for name in keys(this.continuouslist)
## failing for variables with $ sign
## println(name)
try
value=this.getSolutions(name)
value1=value[1]
this.continuouslist[name]=value1[end]
catch Exception
println(Exception)
end
end
return this.continuouslist
elseif(isa(name,String))
if(haskey(this.continuouslist,name))
value=this.getSolutions(name)
value1=value[1]
this.continuouslist[name]=value1[end]
return get(this.continuouslist,name,0)
else
return println(name, " is not continuous")
end
elseif(isa(name,Array))
continuousvaluelist=Any[]
for x in name
if(haskey(this.continuouslist,x))
value=this.getSolutions(x)
value1=value[1]
this.continuouslist[x]=value1[end]
push!(continuousvaluelist,value1[end])
else
return println(x, " is not continuous")
end
end
return continuousvaluelist
end
end
end
this.getInputs = function (name=nothing)
if (name==nothing)
return this.inputlist
elseif(isa(name,String))
return get(this.inputlist,name,0)
elseif(isa(name,Array))
return [get(this.inputlist,x,0) for x in name]
end
end
this.getOutputs = function (name=nothing)
if(this.simulationFlag=="")
if (name==nothing)
return this.outputlist
elseif(isa(name,String))
return get(this.outputlist,name,0)
elseif(isa(name,Array))
return [get(this.outputlist,x,0) for x in name]
end
end
if(this.simulationFlag=="True")
if (name==nothing)
for name in keys(this.outputlist)
value=this.getSolutions(name)
value1=value[1]
this.outputlist[name]=value1[end]
end
return this.outputlist
elseif(isa(name,String))
if(haskey(this.outputlist,name))
value=this.getSolutions(name)
value1=value[1]
this.outputlist[name]=value1[end]
return get(this.outputlist,name,0)
else
return println(name, "is not Output")
end
elseif(isa(name,Array))
valuelist=Any[]
for x in name
if(haskey(this.outputlist,x))
value=this.getSolutions(x)
value1=value[1]
this.outputlist[x]=value1[end]
push!(valuelist,value1[end])
else
return println(x, "is not Output")
end
end
return valuelist
end
end
end
this.simulate = function(;resultfile=nothing)
#println(this.xmlfile)
if(resultfile == nothing)
r=""
this.resultfile=replace(joinpath(this.tempdir,join([this.modelname,"_res.mat"])),r"[/\\]+" => "/")
else
r=join(["-r=",resultfile])
this.resultfile=replace(joinpath(this.tempdir,resultfile),r"[/\\]+" => "/")
end
if(isfile(this.xmlfile))
if (Compat.Sys.iswindows())
getexefile=replace(joinpath(this.tempdir,join([this.modelname,".exe"])),r"[/\\]+" => "/")
else
getexefile=replace(joinpath(this.tempdir,this.modelname),r"[/\\]+" => "/")
end
if(isfile(getexefile))
## change to tempdir
cd(this.tempdir)
if(!isempty(this.overridevariables) | !isempty(this.simoptoverride))
overridelist=Any[]
tmpdict=merge(this.overridevariables,this.simoptoverride)
for k in keys(tmpdict)
val=join([k,"=",tmpdict[k]])
push!(overridelist,val)
end
overridevar=join(["-override=",join(overridelist,",")])
else
overridevar=""
end
if (this.inputFlag=="true")
createcsvdata(this)
csvinput=join(["-csvInput=",this.csvfile])
#run(pipeline(`$getexefile $overridevar $csvinput`,stdout="log.txt",stderr="error.txt"))
else
csvinput=""
#run(pipeline(`$getexefile $overridevar`,stdout="log.txt",stderr="error.txt"))
end
#remove empty args in cmd objects
cmd=filter!(e->e≠"",[getexefile,overridevar,csvinput,r])
#println(cmd)
run(pipeline(`$cmd`,stdout="log.txt",stderr="error.txt"))
#this.resultfile=replace(joinpath(this.tempdir,join([this.modelname,"_res.mat"])),r"[/\\]+" => "/")
this.simulationFlag="True"
else
return println("! Simulation Failed")
end
## change to currentworkingdirectory
cd(this.currentdir)
end
end
this.convertMo2FMU=function()
if(!isempty(this.modelname))
fmuexpression=join(["translateModelFMU(",this.modelname,")"])
Base.Meta.parse(this.sendExpression(fmuexpression))
else
println(this.sendExpression("getErrorString()"))
end
end
this.convertFmu2Mo=function(fmupath)
fmupath=replace(abspath(fmupath),r"[/\\]+" => "/")
if(isfile(fmupath))
result=this.sendExpression("importFMU(\"" *fmupath* "\")")
return joinpath(this.tempdir,Base.Meta.parse(result))
else
println(fmupath, " ! Fmu not Found")
end
end
this.sensitivity=function(Vp,Vv,Ve=[1e-2])
"""
Method for computing numeric sensitivity of OpenModelica object
Arguments:
----------
1st arg: Vp # Array of strings of Modelica Parameter names
2nd arg: Vv # Array of strings of Modelica Variable names
3rd arg: Ve # Array of float Excitations of parameters; defaults to scalar 1e-2
Returns:
--------
1st return: VSname # Vector of Sensitivity names
2nd return: Sarray # Array of sensitivies: vector of elements per parameter,
each element containing time series per variable
"""
# Production quality code should check type and form of input arguments
#
Ve = map(Float64,Ve) # converting eVements of excitation to floats
nVp = length(Vp) # number of parameter names
nVe = length(Ve) # number of excitations in parameters
# Adjusting size of Ve to that of Vp
if nVe < nVp
push!(Ve,Ve[end]*ones(nVp-nVe)...) # extends Ve by adding last eVement of Ve
elseif nVe > nVp
Ve = Ve[1:nVp] # truncates Ve to same length as Vp
end
# Nominal parameters p0
par0 = [Base.parse(Float64,pp) for pp in this.getParameters(Vp)]
# eXcitation parameters parX
parX = [par0[i]*(1+Ve[i]) for i in 1:nVp]
# Combine parameter names and parameter values into vector of strings
Vpar0 = [Vp[i]*"=$(par0[i])" for i in 1:nVp]
VparX = [Vp[i]*"=$(parX[i])" for i in 1:nVp]
# Simulate nominal system
this.simulate()
# Get nominal SOLutions of variabVes of interest (Vv), converted to 2D array
sol0 = this.getSolutions(Vv)
# Get vector of eXcited SOLutions (2D arrays), one for each parameter (Vp)
solX = Vector{Array{Array{Float64,1},1}}()
#
for p in VparX
# change to excited parameter
this.setParameters(p)
# simulate perturbed system
this.simulate()
# get eXcited SOLutions (Vv) as 2D array, and append to list
push!(solX,this.getSolutions(Vv))
# reset parameters to nominal values
this.setParameters(Vpar0)
end
#
# Compute sensitivities and add to vector, one 2D array per parameter (Vp)
VSname = Vector{Vector{String}}()
VSarray = Vector{Array{Array{Float64,1},1}}() # same shape as solX
for (i,sol) in enumerate(solX)
push!(VSarray, ((sol-sol0)/(par0[i]*Ve[i])))
vsname = Vector{String}()
for j in 1:nVp
push!(vsname, "Sensitivity."*Vp[i]*"."*Vv[j])
end
push!(VSname,vsname)
end
return VSname, VSarray
end
this.getSolutions = function(name=nothing; resultfile=nothing)
if(resultfile == nothing)
resfile=this.resultfile
else
resfile=resultfile
end
if(!isfile(resfile))
println("ResultFile does not exist !", abspath(resfile))
return
end
if(!isempty(resfile))
if(name==nothing)
simresultvars=this.sendExpression("readSimulationResultVars(\"" * resfile * "\")")
parsesimresultvars=Base.Meta.parse(simresultvars)
return parsesimresultvars.args
elseif(isa(name,String))
resultvar=join(["{",name,"}"])
simres=this.sendExpression("readSimulationResult(\""* resfile * "\","* resultvar *")")
data=Base.Meta.parse(simres)
this.sendExpression("closeSimulationResultFile()")
return [convert(Array{Float64,1},plotdata.args) for plotdata in data.args]
elseif(isa(name,Array))
resultvar=join(["{",join(name,","),"}"])
#println(resultvar)
simres=this.sendExpression("readSimulationResult(\""* resfile * "\","* resultvar *")")
data=Base.Meta.parse(simres)
plotdata=Array{Float64,1}[]
for item in data.args
push!(plotdata,convert(Array{Float64,1},item.args))
end
this.sendExpression("closeSimulationResultFile()")
return plotdata
end
else
return println("Model not Simulated, Simulate the model to get the results")
end
end
this.setParameters = function (name)
if(isa(name,String))
name=strip_space(name)
value=split(name,"=")
#setxmlfileexpr="setInitXmlStartValue(\""* this.xmlfile * "\",\""* value[1]* "\",\""*value[2]*"\",\""*this.xmlfile*"\")"
#println(haskey(this.parameterlist, value[1]))
if(haskey(this.parameterlist,value[1]))
this.parameterlist[value[1]]=value[2]
this.overridevariables[value[1]]=value[2]
else
return println(value[1], "is not a parameter")
end
#this.sendExpression(setxmlfileexpr)
elseif(isa(name,Array))
name=strip_space(name)
for var in name
value=split(var,"=")
if(haskey(this.parameterlist,value[1]))
this.parameterlist[value[1]]=value[2]
this.overridevariables[value[1]]=value[2]
else
return println(value[1], "is not a parameter")
end
end
end
end
this.setSimulationOptions = function (name)
if(isa(name,String))
name=strip_space(name)
value=split(name,"=")
if(haskey(this.simulateOptions,value[1]))
this.simulateOptions[value[1]]=value[2]
this.simoptoverride[value[1]]=value[2]
else
return println(value[1], " is not a SimulationOption")
end
elseif(isa(name,Array))
name=strip_space(name)
for var in name
value=split(var,"=")
if(haskey(this.simulateOptions,value[1]))
this.simulateOptions[value[1]]=value[2]
this.simoptoverride[value[1]]=value[2]
else
return println(value[1], " is not a SimulationOption")
end
end
end
end
this.setInputs = function (name)
if(isa(name,String))
name=strip_space(name)
value=split(name,"=")
if(haskey(this.inputlist,value[1]))
newval=Base.Meta.parse(value[2])
if(isa(newval, Expr))
this.inputlist[value[1]]=[v.args for v in newval.args]
else
this.inputlist[value[1]]=value[2]
end
this.inputFlag="true"
else
return println(value[1], " is not a Input")
end
elseif(isa(name,Array))
name=strip_space(name)
for var in name
value=split(var,"=")
if(haskey(this.inputlist,value[1]))
newval=Base.Meta.parse(value[2])
if(isa(newval, Expr))
this.inputlist[value[1]]=[v.args for v in newval.args]
else
this.inputlist[value[1]]=value[2]
end
#this.overridevariables[value[1]]=value[2]
this.inputFlag="true"
else
return println(value[1], " is not a Input")
end
end
end
end
function strip_space(name)
if (isa(name,String))
return filter(x->!isspace(x),name)
elseif(isa(name,Array))
return [filter(x->!isspace(x),s) for s in name]
end
end
function createcsvdata(this)
this.csvfile=joinpath(this.tempdir,join([this.modelname,".csv"]))
file = open(this.csvfile,"w")
write(file,join(["time",",",join(keys(this.inputlist),","),",","end","\n"]))
csvdata=deepcopy(this.inputlist)
value=values(csvdata)
time=Any[]
for val in value
if(isa(val,Array))
checkflag="true"
for v in val
push!(time,v[1])
end
end
end
if(length(time)==0)
push!(time,this.simulateOptions["startTime"])
push!(time,this.simulateOptions["stopTime"])
end
previousvalue=Dict()
for i in sort(time)
if(isa(i,SubString{String}))
write(file,i,",")
else
write(file,join(i,","),",")
end
listcount=1
for val in value
if(isa(val,Array))
newval=val
count=1
found="false"
for v in newval
if(i==v[1])
data=eval(v[2])
write(file,join(data,","),",")
previousvalue[listcount]=data
deleteat!(newval,count)
found="true"
break
end
count=count+1
end
if(found=="false")
write(file,join(previousvalue[listcount],","),",")
end
end
if(isa(val,String))
if(val=="None")
val="0"
else
val=val
end
write(file,val,",")
previousvalue[listcount]=val
end
if(isa(val,SubString{String}))
if(val=="None")
val="0"
else
val=val
end
write(file,val,",")
previousvalue[listcount]=val
end
listcount=listcount+1
end
write(file,"0","\n")
end
close(file)
end
function writecsvdata(value,csv_file)
for i in value
write(csv_file,i,",")
for j in values(this.inputlist)
if(j=="None")
write(csv_file,"0",",")
else
write(csv_file,j,",")
end
end
write(csv_file,"0","\n")
end
#close(csv_file)
end
this.linearize = function()
this.sendExpression("setCommandLineOptions(\"+generateSymbolicLinearization\")")
overridelist=Any[]
for k in keys(this.overridevariables)
val=join([k,"=",this.overridevariables[k]])
push!(overridelist,val)
end
overridelinear=Any[]
for t in keys(this.linearOptions)
val=join([t,"=",this.linearOptions[t]])
push!(overridelinear,val)
end
if (this.inputFlag=="true")
createcsvdata(this)
csvinput=join(["-csvInput=",this.csvfile])
else
csvinput="";
end
if (length(overridelist)>0)
overridevar=join(["-override=",join(overridelist,",")])
else
overridevar="";
end
linearexpr=join(["linearize(",this.modelname,",",join(overridelinear,","),",","simflags=","\"",csvinput," ",overridevar,"\"",")"])
#println(linearexpr)
this.sendExpression(linearexpr)
this.resultfile=replace(joinpath(this.tempdir,join([this.modelname,"_res.mat"])),r"[/\\]+" => "/")
this.linearmodelname=join(["linear_",this.modelname])
this.linearfile=joinpath(this.tempdir,join([this.linearmodelname,".mo"]))
if(isfile(this.linearfile))
loadmsg=this.sendExpression("loadFile(\""*this.linearfile*"\")")
if(!Base.Meta.parse(loadmsg))
return this.sendExpression("getErrorString()")
end
cNames =this.sendExpression("getClassNames()")
linearmodelname=Base.Meta.parse(cNames)
#println(linearmodelname.args[1])
buildmodelexpr=join(["buildModel(",linearmodelname.args[1],")"])
buildModelmsg=this.sendExpression(buildmodelexpr)
parsebuilexp=Base.Meta.parse(buildModelmsg)
if(!isempty(parsebuilexp.args[2]))
this.linearFlag="true"
this.xmlfile=replace(joinpath(this.tempdir,parsebuilexp.args[2]),r"[/\\]+" => "/")
this.linearquantitylist=Any[]
this.linearinputs=Any[]
this.linearoutputs=Any[]
this.linearstates=Any[]
xmlparse(this)
linearMatrix = getLinearMatrix(this)
return linearMatrix
else
return this.sendExpression("getErrorString()")
end
else
errormsg=this.sendExpression("getErrorString()")
println(errormsg)
end
end
function getLinearMatrix(this)
matrix_A=OrderedDict()
matrix_B=OrderedDict()
matrix_C=OrderedDict()
matrix_D=OrderedDict()
for i in this.linearquantitylist
name=i["name"]
value=i["value"]
if(i["variability"]=="parameter")
if(name[1]=='A')
matrix_A[name]=value
end
if(name[1]=='B')
matrix_B[name]=value
end
if(name[1]=='C')
matrix_C[name]=value
end
if(name[1]=='D')
matrix_D[name]=value
end
end
end
FullLinearMatrix=Array{Float64,2}[]
tmpMatrix_A=getLinearMatrixValues(matrix_A)
tmpMatrix_B=getLinearMatrixValues(matrix_B)
tmpMatrix_C=getLinearMatrixValues(matrix_C)
tmpMatrix_D=getLinearMatrixValues(matrix_D)
push!(FullLinearMatrix,tmpMatrix_A)
push!(FullLinearMatrix,tmpMatrix_B)
push!(FullLinearMatrix,tmpMatrix_C)
push!(FullLinearMatrix,tmpMatrix_D)
return FullLinearMatrix
end
function getLinearMatrixValues(matrix_name)
if (!isempty(matrix_name))
v=[i for i in keys(matrix_name)]
dim=Base.Meta.parse(v[end])
rowcount=dim.args[2]
colcount=dim.args[3]
tmpMatrix=Matrix(undef,rowcount,colcount)
for j in keys(matrix_name)
val=Base.Meta.parse(j);
row=val.args[2];
col=val.args[3];
tmpMatrix[row,col]=Base.parse(Float64,matrix_name[j])
end
return tmpMatrix
else
return Matrix(undef,0,0)
end
end
this.getLinearizationOptions = function()
return this.linearOptions
end
this.getLinearInputs = function()
if(this.linearFlag=="true")
return this.linearinputs
else
println("Model is not Linearized")
end
end
this.getLinearOutputs = function()
if(this.linearFlag=="true")
return this.linearoutputs
else
println("Model is not Linearized")
end
end
this.getLinearStates = function()
if(this.linearFlag=="true")
return this.linearstates
else
println("Model is not Linearized")
end
end
this.setLinearizationOptions = function (name)
if(isa(name,String))
name=strip_space(name)
value=split(name,"=")
if(haskey(this.linearOptions,value[1]))
this.linearOptions[value[1]]=value[2]
else
return println(value[1], " is not a LinearizationOption")
end
elseif(isa(name,Array))
name=strip_space(name)
for var in name
value=split(var,"=")
if(haskey(this.linearOptions,value[1]))
this.linearOptions[value[1]]=value[2]
else
return println(value[1], " is not a LinearizationOption")
end
end
end
end
function xmlparse(this)
if(isfile(this.xmlfile))
xdoc = parse_file(this.xmlfile)
# get the root element
xroot = root(xdoc) # an instance of XMLElement
for c in child_nodes(xroot) # c is an instance of XMLNode
if is_elementnode(c)
e = XMLElement(c) # this makes an XMLElement instance
if(name(e)=="DefaultExperiment")
this.simulateOptions["startTime"]=attribute(e, "startTime")
this.simulateOptions["stopTime"]=attribute(e, "stopTime")
this.simulateOptions["stepSize"]=attribute(e, "stepSize")
this.simulateOptions["tolerance"]=attribute(e, "tolerance")
this.simulateOptions["solver"]=attribute(e, "solver")
end
if(name(e)=="ModelVariables")
for r in child_elements(e)
scalar = Dict()
scalar["name"] = attribute(r, "name")
scalar["changeable"] = attribute(r,"isValueChangeable")
scalar["description"] = attribute(r,"description")
scalar["variability"] = attribute(r, "variability")
scalar["causality"] = attribute(r,"causality")
scalar["alias"] = attribute(r,"alias")
scalar["aliasvariable"] = attribute(r,"aliasVariable")
subchild=child_elements(r)
for s in subchild
value = attribute(s, "start")
if(value!=nothing)
scalar["value"]=value
else
scalar["value"]="None"
end
end
if(this.linearFlag=="false")
if(scalar["variability"]=="parameter")
this.parameterlist[scalar["name"]]=scalar["value"]
end
if(scalar["variability"]=="continuous")
this.continuouslist[scalar["name"]]=scalar["value"]
end
if(scalar["causality"]=="input")
this.inputlist[scalar["name"]]=scalar["value"]
end
if(scalar["causality"]=="output")
this.outputlist[scalar["name"]]=scalar["value"]
end
end
if(this.linearFlag=="true")
if(scalar["alias"]=="alias")
name=scalar["name"]
if (name[2] == 'x')