-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
1676 lines (1473 loc) · 82.3 KB
/
main.py
File metadata and controls
1676 lines (1473 loc) · 82.3 KB
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
from kivy.config import Config
Config.set('input', 'mouse', 'mouse, multitouch_on_demand')
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ListProperty
from kivy.properties import NumericProperty
from kivy.graphics.vertex_instructions import Rectangle
from kivy.graphics.vertex_instructions import Line
from kivy.uix.bubble import Bubble,BubbleButton
from kivy.uix.popup import Popup
from kivy.uix.modalview import ModalView
from functools import partial
from kivy.properties import ObjectProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.slider import Slider
from kivy.uix.gridlayout import GridLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics.context_instructions import Color
from kivy.graphics.instructions import InstructionGroup
from kivy.uix.actionbar import ActionBar
from kivy.uix.actionbar import ActionView
from kivy.uix.actionbar import ActionButton
from kivy.uix.actionbar import ActionPrevious
from kivy.uix.dropdown import DropDown
from kivy.uix.progressbar import ProgressBar
from kivy.lang import Builder
from kivy.lang import Builder
from textwrap import dedent
from kivy.garden.graph import Graph, MeshLinePlot
from math import sin
import os
import random
import StaticUO
from OMPython import OMCSession
import UnitOP
from error import Error
import time
Thermodynamic_models = ['Peng-Robinson','SRK','NRTL','UNIQUAC']
# File_options = ['New Steady-state Simulation','Open','Save','Save As','Close Active Simulation','Exit Openmodellica']
File_options = ['open','save']
Edit_options = ['Undo','Redo','Cut Selected Objects','Copy Selected Objects','Paste Objects','Remove selected objects','Clone selected objects','Recalculate object','Export data to Clipboard','Simulation settings','General settings']
Insert_options = ['Flowsheet Object','Property Table','Master Property Table','Linked Spreadsheet Table','Image','Text','Rectangle']
Tools_options = ['Petroleum Characterization (Bulk C7+)','Petroleum Characterization (Distillation Curves)','Petroleum Array Manager','Reactions Manager','Pure Compound Property Viewer/Editor','User Database Manager','CAPE-OPEN Component Registration' ]
Utilities_options = ['Binary Plotter']
Optimization_options = ['Sensitivity Analysis','Multivariate Optimizer']
Scripts_options = ['Script Manager']
Results_options = ['Create Report']
Plugins_options = ['CAPE-OPEN Plugins','Natural Gas Properties']
Windows_options = ['Set Canvas Size']
View_options = ['Show Toolstrip','Console Output','calculation Queue','Watch panel','CAPE-OPEN Objects Reports','Flowsheet Toolstrip','Unit Systems Toolstrip',"Restore Docking Panels' Layout",'Close Opened Object Editors']
Help_options = ['Show Help','Documention','Openmodellica on the web','Donate!','About OpenModellica']
# Custom Widgets ------------------------------------------------------ #
class MenuButton(Button):
pass
class CompButton(Button):
pass
class Remove_Bubble(Bubble):
pass
class SPopUp(ModalView):
pass
class SSPopUp(ModalView):
pass
class UtilityPopUp(ModalView):
pass
class BinaryEnvelope(ModalView):
pass
class CompPop(ModalView):
pass
class ThermoPop(ModalView):
pass
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
class ResizePop(ModalView):
pass
class SaveDialog(FloatLayout):
save = ObjectProperty(None)
text_input = ObjectProperty(None)
cancel = ObjectProperty(None)
class MyTextInput(TextInput):
def __init__(self, **kw):
self.drop_down = DropDown(dismiss_on_select=True)
self.drop_down.bind(on_select=self.on_select)
super(MyTextInput, self).__init__(**kw)
def on_select(self, *args):
self.text = args[1]
def on_touch_up(self, touch):
if touch.grab_current == self:
self.drop_down.open(self)
return super(MyTextInput, self).on_touch_up(touch)
# ------------------------------------------------------------------- #
class OmWidget(FloatLayout):
"""
Main widget class containing the root widget.
"""
lines = {}
Unit_Operations = []
Unit_Operations_Labels = []
data = []
loadfile = ObjectProperty(None)
savefile = ObjectProperty(None)
text_input = ObjectProperty(None)
word_list = []
def __init__(self,**kwargs):
super(OmWidget,self).__init__(**kwargs)
# Lists all the compounds
fo = open("compounds.txt", "r+")
self.word_list = fo.read().splitlines()
# Custom error pop-up
self.error_popup = Error()
self.error = self.error_popup.ids.error_message
self.start = ''
self.endt = ''
self.compo = ""
self.plot = None
self.utility_pop_up = ''
self.binary_pop_up = ''
self.rect= False
self.rect_enable = True
self.ids.scroll.do_scroll_x = False
self.ids.scroll.do_scroll_y = False
self.rect_start = []
self.select_rect = ''
self.Selected_Unit_Operations = []
self.grab_w = ''
self.select_box = InstructionGroup()
self.tp = DropDown()
self.resize_popup = ''
self.current_grab_unit = None
self.addedcomp = []
self.comp_dropdown = DropDown()
self.dropdown = DropDown()
self.op_count = 1
self.Selected_thermo_model = 'No Model Selected'
self.data.append('model Flowsheet\n')
self.multiselect = False;
UnitOP.UnitOP.size_limit = self.ids.b1.size
self.ids.hand_toggle.background_color = 0.5, 0.5, 0.5, 1
self.ids.cursor_toggle.background_color = 1, 1, 1, 1
# Adds all the buttons to the file menu ----------------------------------------------------#
self.filedropdown = DropDown(auto_width=False, width=300)
# for model in File_options:
# btn = MenuButton(text=model, width=300)
# btn.text_size = btn.size
# self.filedropdown.add_widget(btn)
btn = MenuButton(text="Open", width=300, on_press=self.show_load)
btn.text_size = btn.size
self.filedropdown.add_widget(btn)
btn2 = MenuButton(text="Save as",width=300, on_press=self.show_save)
btn2.text_size = btn2.size
self.filedropdown.add_widget(btn2)
self.editdropdown = DropDown(auto_width=False, width=300)
for model in Edit_options:
btn = MenuButton(text=model, width=200)
btn.text_size = btn.size
self.editdropdown.add_widget(btn)
self.insertdropdown = DropDown(auto_width=False, width=300)
for model in Insert_options:
btn = MenuButton(text=model, width=200)
btn.text_size = btn.size
self.insertdropdown.add_widget(btn)
self.toolsdropdown = DropDown(auto_width=False, width=400)
for model in Tools_options:
btn = MenuButton(text=model, width=350)
btn.text_size = btn.size
self.toolsdropdown.add_widget(btn)
self.utilitiesdropdown = DropDown(auto_width=False, width=300)
for model in Utilities_options:
btn = MenuButton(text=model, width=100, on_press=self.add_utility)
btn.text_size = btn.size
self.utilitiesdropdown.add_widget(btn)
self.optimizationdropdown = DropDown(auto_width=False, width=300)
for model in Optimization_options:
btn = MenuButton(text=model, width=200)
btn.text_size = btn.size
self.optimizationdropdown.add_widget(btn)
self.scriptsdropdown = DropDown(auto_width=False, width=300)
for model in Scripts_options:
btn = MenuButton(text=model, width=150)
btn.text_size = btn.size
self.scriptsdropdown.add_widget(btn)
self.resultsdropdown = DropDown(auto_width=False, width=300)
for model in Results_options:
btn = MenuButton(text=model, width=150)
btn.text_size = btn.size
self.resultsdropdown.add_widget(btn)
self.pluginsdropdown = DropDown(auto_width=False, width=300)
for model in Plugins_options:
btn = MenuButton(text=model, width=200)
btn.text_size = btn.size
self.pluginsdropdown.add_widget(btn)
self.windowsdropdown = DropDown(auto_width=False, width=300)
for model in Windows_options:
btn = MenuButton(text=model, width=150, on_press=self.change_canvas_size_menu)
btn.text_size = btn.size
self.windowsdropdown.add_widget(btn)
self.viewdropdown = DropDown(auto_width=False, width=300)
for model in View_options:
btn = MenuButton(text=model, width=250)
btn.text_size = btn.size
self.viewdropdown.add_widget(btn)
self.helpdropdown = DropDown(auto_width=False, width=300)
for model in Help_options:
btn = MenuButton(text=model, width=200)
btn.text_size = btn.size
self.helpdropdown.add_widget(btn)
# ------------------------------------------------------------------------------------------#
def change_canvas_size_menu(self,*args):
"""
Popup for changing canvas size
"""
self.resize_popup = ResizePop()
self.resize_popup.ids.canvas_width.text = str(self.ids.b1.size[0])
self.resize_popup.ids.canvas_height.text = str(self.ids.b1.size[1])
self.resize_popup.ids.submit_size.bind(on_press=self.change_canvas_size)
self.resize_popup.open()
def change_canvas_size(self,*args):
"""
Method to change canvas size
"""
self.ids.b1.size = [float(self.resize_popup.ids.canvas_width.text),float(self.resize_popup.ids.canvas_height.text)]
self.resize_popup.dismiss()
# Save/Load functions ------------------------------------------------------------------------#
def dismiss_popup(self):
self._popup.dismiss()
def show_load(self,*args):
content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
self._popup = Popup(title="Load file", content=content,
size_hint=(0.9, 0.9))
self._popup.open()
def show_save(self,*args):
content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
self._popup = Popup(title="Save file", content=content,
size_hint=(0.9, 0.9))
self._popup.open()
def load(self, path, filename):
fo = open(os.path.join(path, filename[0]), "r+")
k = []
for i in self.Unit_Operations_Labels:
k.append(i)
for i in k:
self.unit_op = i
self.remove_unit_op()
self.unit_op = ''
self.ids.b1.clear_widgets()
self.ids.b1.canvas.clear()
# ins = InstructionGroup()
# ins.add(Color(0.9, 0.9, 0.9, 1))
# ins.add(Rectangle(size=self.size,pos=self.pos))
# self.ids.b1.canvas.add(ins)
saved_unit_op = fo.read().splitlines()
for i in saved_unit_op:
up = i.split("^")
pos = (float(up[2]), float(up[3]));
if up[1] == '0':
self.add_unit_op(pos, StaticUO.SMatStrm.UO(), 0, up[6])
elif up[1] == '1':
self.add_unit_op(pos, StaticUO.SMixer.UO(), 0, up[11])
elif up[1] == '2':
self.add_unit_op(pos, StaticUO.SFlash.UO(), 0, up[8])
elif up[1] == '3':
self.add_unit_op(pos, StaticUO.SSplitter.UO(), 0, up[8])
elif up[1] == '4':
self.add_unit_op(pos, StaticUO.SValve.UO(), 0, up[6])
for i in saved_unit_op:
up = i.split("^")
if up[1] == '0':
pass
elif up[1] == '1':
for i in range(4, 10):
up[0] = int(up[0])
if up[i] != '-1':
self.Unit_Operations[up[0]].input_streams[i - 3] = self.Unit_Operations[int(up[i])]
else:
self.Unit_Operations[up[0]].input_streams[i - 3] = None
if up[10] != '-1':
self.Unit_Operations[up[0]].output_streams[1] = self.Unit_Operations[int(up[10])]
else:
self.Unit_Operations[up[0]].output_streams[1] = None
self.Unit_Operations_Labels[up[0]].child.Update_Conn_Pnts()
self.Unit_Operations_Labels[up[0]].child.connect += 1
elif up[1] == '2':
for i in range(4, 6):
up[0] = int(up[0])
if up[i] != '-1':
self.Unit_Operations[up[0]].input_streams[i - 3] = self.Unit_Operations[int(up[i])]
else:
self.Unit_Operations[up[0]].input_streams[i - 3] = None
for i in range(6, 8):
up[0] = int(up[0])
if up[i] != '-1':
self.Unit_Operations[up[0]].output_streams[i - 5] = self.Unit_Operations[int(up[i])]
else:
self.Unit_Operations[up[0]].output_streams[i - 5] = None
self.Unit_Operations_Labels[up[0]].child.Update_Conn_Pnts()
self.Unit_Operations_Labels[up[0]].child.connect += 1
elif up[1] == '3':
for i in range(4, 5):
up[0] = int(up[0])
if up[i] != '-1':
self.Unit_Operations[up[0]].input_streams[i - 3] = self.Unit_Operations[int(up[i])]
else:
self.Unit_Operations[up[0]].input_streams[i - 3] = None
for i in range(5, 8):
up[0] = int(up[0])
if up[i] != '-1':
self.Unit_Operations[up[0]].output_streams[i - 4] = self.Unit_Operations[int(up[i])]
else:
self.Unit_Operations[up[0]].output_streams[i - 4] = None
self.Unit_Operations_Labels[up[0]].child.Update_Conn_Pnts()
self.Unit_Operations_Labels[up[0]].child.connect += 1
elif up[1] == '4':
for i in range(4, 5):
up[0] = int(up[0])
if up[i] != '-1':
self.Unit_Operations[up[0]].input_streams[i - 3] = self.Unit_Operations[int(up[i])]
else:
self.Unit_Operations[up[0]].input_streams[i - 3] = None
for i in range(5, 6):
up[0] = int(up[0])
if up[i] != '-1':
self.Unit_Operations[up[0]].output_streams[i - 4] = self.Unit_Operations[int(up[i])]
else:
self.Unit_Operations[up[0]].output_streams[i - 4] = None
self.Unit_Operations_Labels[up[0]].child.Update_Conn_Pnts()
self.Unit_Operations_Labels[up[0]].child.connect += 1
self.dismiss_popup()
def save(self, path, filename):
fo = open(os.path.join(path, filename), "wb")
v = 0
for i in self.Unit_Operations:
fo.write(str(v) + "^")
fo.write(str(i.type) + "^")
fo.write(str(i.center[0]) + "^")
fo.write(str(i.center[1]) + "^")
for j in i.input_streams:
if i.input_streams[j]:
fo.write(str(self.Unit_Operations.index(i.input_streams[j])) + "^")
else:
fo.write("-1" + "^")
for j in i.output_streams:
if i.output_streams[j]:
fo.write(str(self.Unit_Operations.index(i.output_streams[j])) + "^")
else:
fo.write("-1" + "^")
fo.write(i.name)
fo.write("\n")
v = v + 1
fo.close()
self.dismiss_popup()
# ----------------------------------------------------------------------------------------------#
# Binary Envelope Feature ----------------------------------------------------------------------#
def add_popup(self,*args):
interval_down = DropDown()
for i in self.addedcomp:
btn = Button(text=i, size_hint_y=None, height=25, background_normal='',
background_color=(0.4, 0.4, 0.4, 1))
btn.bind(on_release=lambda btn: interval_down.select(btn.text))
interval_down.add_widget(btn)
interval_down.bind(on_select=lambda instance, x: setattr(args[0], 'text', x))
interval_down.open(args[0])
def add_popup2(self, *args):
interval_down = DropDown()
for i in self.addedcomp:
if i != self.binary_pop_up.ids.compound_1.text:
btn = Button(text=i, size_hint_y=None, height=25, background_normal='',
background_color=(0.4, 0.4, 0.4, 1))
btn.bind(on_release=lambda btn: interval_down.select(btn.text))
interval_down.add_widget(btn)
interval_down.bind(on_select=lambda instance, x: setattr(args[0], 'text', x))
interval_down.open(args[0])
def add_popup3(self, *args):
models = ['Peng-Robinson', 'SRK', 'NRTL', 'UNIQUAC']
interval_down = DropDown()
for i in models:
if i != args[0].text:
btn = Button(text=i, size_hint_y=None, height=25, background_normal='',
background_color=(0.4, 0.4, 0.4, 1))
btn.bind(on_release=lambda btn: interval_down.select(btn.text))
interval_down.add_widget(btn)
interval_down.bind(on_select=lambda instance, x: setattr(args[0], 'text', x))
interval_down.open(args[0])
def PropPack(self, *args):
comp1 = self.binary_pop_up.ids.compound_1.text
comp2 = self.binary_pop_up.ids.compound_2.text
pressure = self.binary_pop_up.ids.Pressure.text
model = self.binary_pop_up.ids.property_package.text
with open('PropPack.mo', 'w') as txtfile:
txtfile.write('model PropPack\n')
txtfile.write('parameter Chemsep_Database.' + comp1 + ' C1;\n')
txtfile.write('parameter Chemsep_Database.' + comp2 + ' C2;\n')
txtfile.write('parameter Real Pressure = ' + pressure + ';\n')
txtfile.write('extends Thermodynamic_Packages.bubblepnt;\n')
txtfile.write('extends Thermodynamic_Packages.' + model + '(NOC = 2, Comp = {C1,C2}, P = Pressure);\n')
txtfile.write('end PropPack;\n')
omc = OMCSession()
omc.sendExpression("loadFile(\"Chemsep_Database.mo\")")
omc.sendExpression("loadFile(\"Thermodynamic_Functions.mo\")")
omc.sendExpression("loadFile(\"Thermodynamic_Packages.mo\")")
omc.sendExpression("loadFile(\"PropPack.mo\")")
resultval = omc.sendExpression("simulate(PropPack, stopTime=1.0, numberOfIntervals=50)")
if self.plot != None:
self.binary_pop_up.ids.graph.remove_plot(self.plot)
self.plot = None
self.plot = MeshLinePlot(color=[1, 0, 0,1])
plot_points = []
i = 0.01
max = -1000000
min = 1000000
while i < 1:
val_r = str(omc.sendExpression("val(T," + str(i) + ")"))
plot_points.append((i, float(val_r)))
if float(val_r)>max:
max =float(val_r)
if float(val_r)<min:
min = float(val_r)
i += 0.02
self.plot.points = plot_points
graph = self.binary_pop_up.ids.graph
graph.y_ticks_major = (max-min)/10
graph.ymin = min-10
graph.ymax = max+10
graph.add_plot(self.plot)
def add_utility(self,*args):
self.binary_pop_up = BinaryEnvelope()
interval = self.binary_pop_up.ids.compound_1
interval.bind(on_release=self.add_popup)
self.binary_pop_up.ids.compound_2.bind(on_release=self.add_popup2)
self.binary_pop_up.ids.property_package.bind(on_release=self.add_popup3)
self.binary_pop_up.open()
self.binary_pop_up.ids.calculate.bind(on_release=self.PropPack)
# ----------------------------------------------------------------------------------------------------------#
# (Move/Select) Multiple canvas options --------------------------------------------------------------------#
def select_hand(self):
self.ids.hand_toggle.background_color = 1, 1, 1, 1
self.ids.cursor_toggle.background_color = 0.5, 0.5, 0.5, 1
self.rect_enable = False
self.ids.scroll.do_scroll_x = True
self.ids.scroll.do_scroll_y = True
def select_cursor(self):
self.ids.hand_toggle.background_color = 0.5, 0.5, 0.5, 1
self.ids.cursor_toggle.background_color = 1, 1, 1, 1
self.ids.scroll.do_scroll_x = False
self.ids.scroll.do_scroll_y = False
self.rect_enable = True
# ----------------------------------------------------------------------------------------------------------#
def Seq_Mod(self, instance):
start = time.time()
# for k in self.Unit_Operations:
# if k.check_stm == 0:
# if k.name == m.
# if k.popup_check == 1:
# i=0
# # 1(NOC = 3, comp = {meth, eth, wat},P = 202650,T = 373)
# self.data.append("Simulator.Streams.Mat_Stm_RL " + k.name +"(NOC = " + str(comp_count))
# self.data.append(",comp = {")
# i=0
# while i < comp_count:
# self.data.append("compound"+str(i))
# if i != comp_count-1:
# self.data.append(",")
# i += 1
# self.data.append("});\n")
mixcount = 0
for m in self.Unit_Operations:
if m.check_mixer == 0:
comp_count = 0
self.data = []
self.data.append("model Flowsheet\n")
for c in self.addedcomp:
self.data.append(
"parameter Simulator.Files.Chemsep_Database." + c + " compound" + str(comp_count) + "; \n")
comp_count += 1
count = 0
for strm in m.InputStrNames:
self.data.append("Simulator.Streams.Mat_Stm_RL " + strm + "(NOC = " + str(comp_count))
self.data.append(",comp = {")
i = 0
while i < comp_count:
self.data.append("compound" + str(i))
if i != comp_count - 1:
self.data.append(",")
i += 1
self.data.append("});\n")
self.data.append("Simulator.Streams.Mat_Stm_RL " + m.OutputStrNames + "(NOC = " + str(comp_count))
self.data.append(",comp = {")
i = 0
while i < comp_count:
self.data.append("compound" + str(i))
if i != comp_count - 1:
self.data.append(",")
i += 1
self.data.append("});\n")
if m.popup_check == 1:
self.data.append("Simulator.Unit_Operations.Mixer " + m.name + "(NOC = " + str(comp_count))
self.data.append(",comp = {")
i = 0
while i < comp_count:
self.data.append("compound" + str(i))
if i != comp_count - 1:
self.data.append(",")
i += 1
self.data.append("},")
self.data.append("outPress = \"Inlet_Average\",NI=2);\n")
self.data.append("equation\n")
i = 0
strcount = 1
for strname in m.InputStrNames:
self.data.append('connect(' + strname + '.outlet,' + m.name + '.inlet[' + str(strcount) + ']);\n')
strcount += 1
self.data.append('connect(' + m.name + '.outlet,' + m.OutputStrNames + '.inlet);\n')
for k in self.Unit_Operations:
if k.name in m.InputStrNames:
sumx = sum(k.mol_frac_mix)
sumX = sum(k.mass_frac_mix)
if k.prop_enable[0] == 1:
self.data.append(k.name + '.P=' + str(k.PropertyVal[1]) + ';\n')
if k.prop_enable[1] == 1:
self.data.append(k.name + '.T=' + str(k.PropertyVal[0]) + ';\n')
if k.prop_enable[4] == 1:
self.data.append(k.name + '.vapPhasMolFrac=' + str(k.PropertyVal[4]) + ';\n')
if k.current_comp_spec == 0:
if sumx != 0:
self.data.append(k.name + ".compMolFrac[1,:] = {")
count = 0
while count < comp_count:
self.data.append(str(k.compound_amounts_molar_frac_mix[count]))
if count != comp_count - 1:
self.data.append(",")
count += 1
self.data.append('};\n')
else:
if sumX != 0:
self.data.append(k.name + ".compMasFrac[1,:] = {")
count = 0
while count < comp_count:
self.data.append(str(k.compound_amounts_mass_frac_mix[count]))
if count != comp_count - 1:
self.data.append(",")
count += 1
self.data.append('};\n')
if k.prop_enable[2] == 1:
self.data.append(k.name + ".totMasFlo[1] = " + str(k.PropertyVal[2]) + ";\n")
if k.prop_enable[3] == 1:
self.data.append(k.name + ".totMolFlo[1] = " + str(k.PropertyVal[3]) + ";\n")
i += 1
with open('Flowsheet.mo', 'w') as txtfile:
for d in self.data:
txtfile.write(d)
txtfile.write('end Flowsheet;\n')
print "Simulating " + m.name
self.SeqModSimProgress(m)
endt = time.time()
print(endt - start)
def Eqn_Orin(self, instance):
self.start = time.time()
comp_count = 0
self.data = []
self.data.append("model Flowsheet\n")
for c in self.addedcomp:
self.data.append("parameter Simulator.Files.Chemsep_Database." + c + " compound" + str(comp_count) + "; \n")
comp_count += 1
count = 0
for k in self.Unit_Operations:
if k.check_stm == 0:
if k.popup_check == 1:
self.data.append("Simulator.Streams.Mat_Stm_RL " + k.name +"(NOC = " + str(comp_count))
self.data.append(",comp = {")
i=0
while i < comp_count:
self.data.append("compound"+str(i))
if i != comp_count-1:
self.data.append(",")
i += 1
self.data.append("});\n")
# for m in self.Unit_Operations:
if k.check_mixer==0:
if k.popup_check==1:
self.data.append("Simulator.Unit_Operations.Mixer "+k.name+"(NOC = " + str(comp_count))
self.data.append(",comp = {")
i = 0
while i < comp_count:
self.data.append("compound" + str(i))
if i != comp_count - 1:
self.data.append(",")
i += 1
self.data.append("},")
self.data.append("outPress = \"Inlet_Average\",NI=2);\n")
if k.check_valve==0:
if k.popup_check==1:
self.data.append("Simulator.Unit_Operations.Valve "+k.name+"(NOC = " + str(comp_count))
self.data.append(",comp = {")
i = 0
while i < comp_count:
self.data.append("compound" + str(i))
if i != comp_count - 1:
self.data.append(",")
i += 1
self.data.append("},")
self.data.append(k.SelCalcParam + '=' + str(k.CalcMethValNo)+");\n")
self.data.append("equation\n")
i = 0
# Connect Equations
for m in self.Unit_Operations:
strcount = 1
if m.check_mixer == 0:
for strname in m.InputStrNames:
self.data.append('connect('+strname+'.outlet,'+m.name+'.inlet['+str(strcount)+']);\n')
strcount+=1
self.data.append('connect('+m.name+'.outlet,'+m.OutputStrNames+'.inlet);\n')
if m.check_valve == 0:
for strname in m.InputStrNames:
self.data.append('connect('+strname+'.outlet,'+m.name+'.inlet);\n')
for strname in m.OutputStrNames:
self.data.append('connect('+m.name+'.outlet,'+strname+'.inlet);\n')
# Stream Properties
for k in self.Unit_Operations:
if k.check_stm == 0:
if k.popup_check == 1:
sumx = sum(k.mol_frac_mix)
sumX = sum(k.mass_frac_mix)
if k.prop_enable[0] == 1:
self.data.append(k.name + '.P=' + str(k.PropertyVal[1]) + ';\n')
if k.prop_enable[1] == 1:
self.data.append(k.name + '.T=' + str(k.PropertyVal[0]) + ';\n')
if k.prop_enable[4] == 1:
self.data.append(k.name + '.vapPhasMolFrac=' + str(k.PropertyVal[4]) + ';\n')
if k.current_comp_spec ==0:
if sumx !=0:
self.data.append(k.name + ".compMolFrac[1,:] = {")
count = 0
while count < comp_count:
if k.compound_amounts_molar_frac_mix[count] != 0:
self.data.append(str(k.compound_amounts_molar_frac_mix[count]))
if count != comp_count-1:
self.data.append(",")
count += 1
self.data.append('};\n')
else:
if sumX !=0:
self.data.append(k.name + ".compMasFrac[1,:] = {")
count = 0
while count < comp_count:
self.data.append(str(k.compound_amounts_mass_frac_mix[count]))
if count != comp_count - 1:
self.data.append(",")
count += 1
self.data.append('};\n')
if k.prop_enable[2] == 1:
self.data.append(k.name + ".totMasFlo[1] = " + str(k.PropertyVal[2]) + ";\n")
if k.prop_enable[3] == 1:
self.data.append(k.name + ".totMolFlo[1] = " + str(k.PropertyVal[3]) + ";\n")
i += 1
with open('Flowsheet.mo', 'w') as txtfile:
for d in self.data:
txtfile.write(d)
txtfile.write('end Flowsheet;\n')
self.EqnOrinSimProgress()
def simulate(self,instance):
self.SSP.dismiss()
SimStatus = SPopUp()
SimStatus.bind(on_open=self.SimProgress)
SimStatus.open()
SimStatus.ids.sim_status.text = "Simulating...."
SimStatus.ids.dismiss_progress.bind(on_press=SimStatus.dismiss)
def select(self, *args):
try:
self.label.text = args[1][0]
except:
pass
def EqnOrinSimProgress(self):
omc = OMCSession()
omc.sendExpression("loadModel(Modelica)")
omc.sendExpression("loadFile(\"Simulator.mo\")")
omc.sendExpression("loadFile(\"Flowsheet.mo\")")
chek = omc.sendExpression("simulate(Flowsheet, stopTime=1.0,numberOfIntervals=1)")
# print chek
stm_count = 0
check = 1
for i in self.Unit_Operations:
if i.check_stm == 0:
try:
count = 0
for prop in i.PhasePropertyMix:
resultval = str(omc.sendExpression("val("+i.name+ "." + i.PhasePropertyMixDict[prop] + ", 0.5)"))
i.PhaseMixVal[count] = resultval
count += 1
count = 0
for prop in i.PhasePropertyVap:
resultval = str(omc.sendExpression(
"val(" + i.name+ "." + i.PhasePropertyVapDict[prop] + ", 0.5)"))
i.PhaseVapVal[count] = resultval
count += 1
count = 0
for comp in self.addedcomp:
i.compound_amounts_molar_frac_mix[count] = str(omc.sendExpression(
"val("+i.name + ".compMolFrac[1," + str(count+1) + "]" + ", 0.5)"))
i.compound_amounts_molar_frac_vap[count]= str(omc.sendExpression(
"val("+i.name + ".compMolFrac[3," + str(count + 1) + "]" + ", 0.5)"))
i.compound_amounts_mass_frac_mix[count] = str(omc.sendExpression(
"val("+i.name + ".compMasFrac[1," + str(count + 1) + "]" + ", 0.5)"))
i.compound_amounts_mass_frac_vap[count] = str(omc.sendExpression(
"val("+i.name + ".compMasFrac[3," + str(count + 1) + "]" + ", 0.5)"))
i.compound_amounts_molar_flow_mix[count] = str(omc.sendExpression(
"val("+i.name + ".compMolFlo[1," + str(count + 1) + "]" + ", 0.5)"))
i.compound_amounts_molar_flow_vap[count] = str(omc.sendExpression(
"val("+i.name + ".compMolFlo[3," + str(count + 1) + "]" + ", 0.5)"))
i.compound_amounts_mass_flow_mix[count] = str(omc.sendExpression(
"val("+i.name + ".compMasFlo[1," + str(count + 1) + "]" + ", 0.5)"))
i.compound_amounts_mass_flow_vap[count] = str(omc.sendExpression(
"val("+i.name + ".compMasFlo[3," + str(count + 1) + "]" + ", 0.5)"))
i.comp_prop_sph_value[count] = str(omc.sendExpression(
"val("+i.name + ".compMolSpHeat[3," + str(count + 1) + "]" + ", 0.5)"))
i.comp_prop_meh_value[count] = str(omc.sendExpression(
"val("+i.name + ".compMolEnth[3," + str(count + 1) + "]" + ", 0.5)"))
i.comp_prop_met_value[count] = str(omc.sendExpression(
"val("+i.name + ".compMolEntr[3," + str(count + 1) + "]" + ", 0.5)"))
count += 1
i.status = 1
except:
# instance.ids.sim_status.text = "Error in simulation!"
# instance.ids.ProgBar.value = 0
# check = 0
print "Failed"
stm_count += 1
if check != 0:
# instance.ids.ProgBar.value = 100
# instance.ids.sim_status.text = 'Completed Successfully'
print 'Completed Successfully'
self.endt = time.time()
print(self.endt-self.start)
def SeqModSimProgress(self,mod):
omc = OMCSession()
omc.sendExpression("loadModel(Modelica)")
omc.sendExpression("loadFile(\"Simulator.mo\")")
omc.sendExpression("loadFile(\"Flowsheet.mo\")")
chek = omc.sendExpression("simulate(Flowsheet, stopTime=1.0,numberOfIntervals=1)")
# print chek
stm_count = 0
check = 1
for i in self.Unit_Operations:
if i.check_stm==0:
if i.name in mod.InputStrNames or i.name==mod.OutputStrNames:
print i.name
try:
count = 0
resultvalT = str(omc.sendExpression("val("+i.name+ "." + "T" + ", 0.5)"))
i.PropertyVal[0] = resultvalT
resultvalP = str(omc.sendExpression("val(" + i.name + "." + "P" + ", 0.5)"))
i.PropertyVal[1] = resultvalP
resultvalMasF = str(omc.sendExpression("val(" + i.name + "." + "totMasFlo[1]" + ", 0.5)"))
i.PropertyVal[2] = resultvalMasF
resultvalMolF = str(omc.sendExpression("val(" + i.name + "." + "totMolFlo[1]" + ", 0.5)"))
i.PropertyVal[3] = resultvalMolF
resultvalVF = str(omc.sendExpression("val(" + i.name + "." + "vapPhasMolFrac" + ", 0.5)"))
i.PropertyVal[4] = resultvalVF
for prop in i.PhasePropertyMix:
resultval = str(omc.sendExpression("val("+i.name+ "." + i.PhasePropertyMixDict[prop] + ", 0.5)"))
i.PhaseMixVal[count] = resultval
count += 1
count = 0
for prop in i.PhasePropertyVap:
resultval = str(omc.sendExpression(
"val(" + i.name+ "." + i.PhasePropertyVapDict[prop] + ", 0.5)"))
i.PhaseVapVal[count] = resultval
count += 1
count = 0
i.prop_enable[0] = 1
i.prop_enable[1] = 1
i.prop_enable[3] = 1
for comp in self.addedcomp:
i.compound_amounts_molar_frac_mix[count] = str(omc.sendExpression(
"val("+i.name + ".compMolFrac[1," + str(count+1) + "]" + ", 0.5)"))
i.mol_frac_mix[count] = float(i.compound_amounts_molar_frac_mix[count])
i.compound_amounts_molar_frac_vap[count]= str(omc.sendExpression(
"val("+i.name + ".compMolFrac[3," + str(count + 1) + "]" + ", 0.5)"))
i.compound_amounts_mass_frac_mix[count] = str(omc.sendExpression(
"val("+i.name + ".compMasFrac[1," + str(count + 1) + "]" + ", 0.5)"))
i.mass_frac_mix[count] = float(i.compound_amounts_mass_frac_mix[count])
i.compound_amounts_mass_frac_vap[count] = str(omc.sendExpression(
"val("+i.name + ".compMasFrac[3," + str(count + 1) + "]" + ", 0.5)"))
i.compound_amounts_molar_flow_mix[count] = str(omc.sendExpression(
"val("+i.name + ".compMolFlo[1," + str(count + 1) + "]" + ", 0.5)"))
i.compound_amounts_molar_flow_vap[count] = str(omc.sendExpression(
"val("+i.name + ".compMolFlo[3," + str(count + 1) + "]" + ", 0.5)"))
i.compound_amounts_mass_flow_mix[count] = str(omc.sendExpression(
"val("+i.name + ".compMasFlo[1," + str(count + 1) + "]" + ", 0.5)"))
i.compound_amounts_mass_flow_vap[count] = str(omc.sendExpression(
"val("+i.name + ".compMasFlo[3," + str(count + 1) + "]" + ", 0.5)"))
i.comp_prop_sph_value[count] = str(omc.sendExpression(
"val("+i.name + ".compMolSpHeat[3," + str(count + 1) + "]" + ", 0.5)"))
i.comp_prop_meh_value[count] = str(omc.sendExpression(
"val("+i.name + ".compMolEnth[3," + str(count + 1) + "]" + ", 0.5)"))
i.comp_prop_met_value[count] = str(omc.sendExpression(
"val("+i.name + ".compMolEntr[3," + str(count + 1) + "]" + ", 0.5)"))
count += 1
i.status = 1
except:
print "Error in Simulation"
stm_count += 1
if check != 0:
print "completed Successfully"
def SimulationSettings(self, instance):
self.SSP = SSPopUp()
self.SSP.ids.Sim_But.bind(on_press=self.simulate)
values1 = ["Number of Intervals", "Interval"]
values2 = ["euler", "rungekutta", " dassl", "optimization", "radau5", "radau3", "impeuler", "trapezoid",
"lobatto4", "lobatto6", "symEuler", "symEulerSsc", "heun", "ida", "rungekutta_ssc", " qss "]
values3 = ["coloured Num", "internationalNumerical", "colouredSymbolical", "numerical", "symbolical",
"kluSparse"]
values4 = ["mat", "plt", "csv"]
interval = self.SSP.ids.interval_type
interval_down = DropDown()
for i in values1:
btn = Button(text=i, size_hint_y=None, height=25, background_normal='',
background_color=(0.4, 0.4, 0.4, 1))
btn.bind(on_release=lambda btn: interval_down.select(btn.text))
interval_down.add_widget(btn)
interval_down.bind(on_select=lambda instance, x: setattr(interval, 'text', x))
interval.bind(on_release=interval_down.open)
method = self.SSP.ids.method
method_down = DropDown()
for i in values2:
btn = Button(text=i, size_hint_y=None, height=25, background_normal='',
background_color=(0.4, 0.4, 0.4, 1))
btn.bind(on_release=lambda btn: method_down.select(btn.text))
method_down.add_widget(btn)
method_down.bind(on_select=lambda instance, x: setattr(method, 'text', x))
method.bind(on_release=method_down.open)
output = self.SSP.ids.output
output_down = DropDown()
for i in values3:
btn = Button(text=i, size_hint_y=None, height=25, background_normal='',
background_color=(0.4, 0.4, 0.4, 1))
btn.bind(on_release=lambda btn: output_down.select(btn.text))
output_down.add_widget(btn)
output_down.bind(on_select=lambda instance, x: setattr(output, 'text', x))
output.bind(on_release=output_down.open)
output2 = self.SSP.ids.output2
output2_down = DropDown()
for i in values4:
btn = Button(text=i, size_hint_y=None, height=25, background_normal='',
background_color=(0.4, 0.4, 0.4, 1))
btn.bind(on_release=lambda btn: output2_down.select(btn.text))
output2_down.add_widget(btn)
output2_down.bind(on_select=lambda instance, x: setattr(output2, 'text', x))
output2.bind(on_release=output2_down.open)
self.SSP.open()
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
k = True
if 'multitouch_sim' not in touch.profile and not self.current_grab_unit:
for i in self.Unit_Operations_Labels:
if i.collide_point(*self.compute_relative_position(touch)):
if touch.is_double_tap:
i.child.multi_touch += 1
else:
self.current_grab_unit = i;
if 'multitouch_sim' not in touch.profile:
for i in self.Unit_Operations_Labels:
i.canvas.before.clear()