I am having a problem with gams python api.
I have a gams file that runs correctly.
When I import to use the interface with python through spyder, it gives compilation error: GamsException: gamslib return code not 0 (2)
The code in python to run gams:
if name == “main”:
if len(sys.argv) > 1:
ws = GamsWorkspace(system_directory = sys.argv[1])
else:
ws = GamsWorkspace()
ws.gamslib(“Artigo_LJ1”)
t1 = ws.add_job_from_file(“Artigo_LJ1.gms”)
t1.run()
print(“Ran with Default:”)
for rec in t1.out_db[“x”]:
print(“x(” + rec.key(0) + “,” + rec.key(1) + “): level=” + str(rec.level) + " marginal=" + str(rec.marginal))
opt = ws.add_options()
opt.all_model_types = “xpress”
t1.run(opt)
print(“Ran with XPRESS:”)
for rec in t1.out_db[“x”]:
print(“x(” + rec.key(0) + “,” + rec.key(1) + “): level=” + str(rec.level) + " marginal=" + str(rec.marginal))
file = open(os.path.join(ws.working_directory, “xpress.opt”), “w”)
file.write(“algorithm=barrier”)
file.close()
opt.optfile = 1
t1.run(opt)
print(“Ran with XPRESS with non-default option:”)
for rec in t1.out_db[“x”]:
print(“x(” + rec.key(0) + “,” + rec.key(1) + “): level=” + str(rec.level) + " marginal=" + str(rec.marginal))
The gams file:
Sets
i source /REF/
j demand /NHT1, NHT2, NHT3,LNIS, GHT/
k purifiers /PSA1/;
Parameters
*source
FH2Smin(i) MIN FLOW SOURCE [Nm3h-1]
/REF 1/
FH2Smax(i) MAX FLOW SOURCE [Nm3h-1]
/REF 400000/
YH2S(i) PURITY SOURCE
/REF 0.67/
PH2S(i) PRESSURE SOURCE [MPa]
/REF 2.4/
CH2S(i) COST SOURCE [pounds per Nm3h-1]
/REF 0.11183/
*demand
FH2C(j) FLOW DEMAND INLET [Nm3h-1]
/NHT1 10.600
NHT2 9.500
NHT3 2.500
LNIS 48.500
GHT 62.200/
YH2C(j) PURITY DEMAND INLET
/NHT1 0.67
NHT2 0.999
NHT3 0.67
LNIS 0.67
GHT 0.805/
PH2C(j) PRESSURE INLET [MPa]
/NHT1 2.4
NHT2 2.4
NHT3 2.4
LNIS 3.5
GHT 5.0/
FPpoc(j) FLOW OUTLET PURGE [Nm3h-1]
/NHT1 0.700
NHT2 0
NHT3 0
LNIS 51.900
GHT 5.700/
YPpoc(j) PURITY OUTLET PURGE
/NHT1 0.461
NHT2 0.45
NHT3 0.46
LNIS 0.456
GHT 0.454/
PP(j) PRESSURE OUTLET [MPa]
/NHT1 2.0
NHT2 2.0
NHT3 2.0
LNIS 3.0
GHT 3.9/
PPCMU(j) Power rate of make-up compressors of hydrogen consumers [kW Nm3-1]
/NHT1 0.1333
NHT2 0.1333
NHT3 0.1333
LNIS 0.1333
GHT 0.1333/
*Purifiers
Pmax(k) MAX FLOW PURIFICATION INLET
/PSA1 115.000/
PPO(k) PRESSURE PURIFICATION OUTLET MPa
/PSA1 2.4/
REC(k) RECOVERY OF PURIFICATION
/PSA1 0.55/
YH2P(k) PURITY OF PURIFIED STREAM
/PSA1 0.999/
YLOZP(k) PURITY OF WASTE STREAM
/PSA1 0.402/
CH2P(k) COST OF PURIFICATION [pound per Nm3h-1]
/PSA1 0.12183/
fator percntual perdido; fator=0.0822;
;
Scalar YIKL “H2 PURITY WASTE STREAN FROM SOURCE” /0.336/ ;
Scalar HLPV “H2 PRICE IN FUEL GAS” /0.05/;
Scalar MLPV “METANE PRICE IN FUEL GAS” /0.16/ ;
Scalar PWR “power rate (compressor)” /0.01/ ;
Table z(i,j) “COMPRESSOR I-J”
NHT1 NHT2 NHT3 LNIS GHT
REF 0 0 0 1 1;
Table z2(k,j) “COMPRESSOR K-J”
NHT1 NHT2 NHT3 LNIS GHT
PSA1 0 0 0 1 1
;
Table z3(j,k) “COMPRESSOR J-K”
PSA1
NHT1 1
NHT2 1
NHT3 1
LNIS 0
GHT 0 ;
Variables
COST
;
Positive variables
FI(i) Total current flow rich in hydrogen produced at source [Nm3.h-1]
FIKL(i,k) Waste gas from source to purifier
FC(j) Inlet flow of hydrogen reactor
YC(j) Purity of the inlet flow of hydrogen
FP(j) exit flow hydrogen reactor
YP(j) Purity of the exit flow of hydrogen
PHL(j) Balance of hydrogen at the consumer’s entrance
DPHLJ(j) Hydrogen consumption in the reactor - from pure hydrogen
PHLJO(j) the pure hydrogen load on the sink outlet
FLOZI(i) unused waste gas from the source
FLOZ(j) Unused Waste Gas from Consumers - The Unit Limiting Capacity of the Unit purification
YLOZ(j) Waste gas purity
FLOZP(k) Waste gas from the condensing unit additionally due to the satisfaction of the output balance hydrogen
FLOZPR(k) Waste gas from the purification unit is solely due to recovery factor [residue]
FIJ(i,j) Flow from hydrogen sources i to consumers j [Nm3.h-1]
FIK(i,k) Flow from the source i of hydrogen to the purification unit k[Nm3.h-1]
FJK(j,k) Flow from hydrogen consumers j to the purification unit k [Nm3.h-1]
FKJ(k,j) Flow rate of the purifier k to the hydrogen consuming j [Nm3.h-1]
FH2I(i) Production of hydrogen from the source
CH2I The cost of producing hydrogen from the source
FH2K(k) Production of hydrogen by purifiers
CH2K(k) Price of hydrogen production by purifier (operating costs)
CH2KP(k) The price of hydrogen production by purifier
CH2KU Total price of hydrogen production by purifier
TKIJ(j) Compression Price Source-Consumer
TKIU Total cost of compression source-consumer
TKKJ(j) The price of compression-consuming compression
TKKU Total cost of compression-consuming compression
TKJK(j) The cost of compression-consumption
TKJKU Total cost of compression-consumption
TKU Total compression costs
LOZIVIPLIN Total gas flow rate
VLPHM The value of the fuel gas (hydrogen and methane)
;
Equations
MaxFI(i) Limit - maximum current source flow i
MinFI(i) Limit - minimum flow of source current i
CIYmin(j) Limit - the minimum required hydrogen concentration at the input to the consumer j
CIYmax(j) Limit - the maximum required hydrogen concentration at the input to the consumer j
Production(i) Production of hydrogen at source
LOZizvora(i) Production of hydrogen at the source waste stream
PHLJ(j) The balance of hydrogen at the consumer input
HMBJ(j) Consumption hydrogen balance
MBJ(j) Balance of flows at the consumers input
HMBJO(j) The balance of hydrogen at the exit to the consumer
SID(j) Defining inputs into a reactor j consumer
C(j) Consumption in the reactor
SC(j) Output of the reactor
COY(j) Limit - hydrogen concentration at exit from the consumer
CYLOZ(j) Limit - hydrogen concentration at exit from consumer - waste stream
SOD(j) Define the output from the reactor
PIC(k) Limit - Input Flow into purification Unit
MBP(k) Balance of flows around the purifier unit
HMBP(k) Hydrogen balance in the purifier unit
LOZREC(k) Waste gas extraction from the Recovery Factor due to Recovery Factor
LOZukupno total leaking gas
PH2I(i) Production of hydrogen from sources
H2I Cost of hydrogen production from sources
PH2K(k) Production of hydrogen purifier
H2K(k) Cost of hydrogen production by individual purifier for each unit
H2KU Cost of hydrogen production by total purifier
TKI(j) Costs of consumer compression by consumers
TKIsuma Costs of compression from sources to consumers in total
TKK(j) Compression Costs from Procurers to Consumers
TKKsuma Costs of compression from consumables to consumers in total
TKJ(j) Costs of compression from consumers to procurers
TKJsuma Costs of compression from consumers to total costs
TK Total compression costs
VLP The value of the leaking gas
OBJ Objective function min. costs [FUNTE_ Nm3h-1]
;
*FI.up(i)= FH2Smax(i);
*SOURCES
*MaxFI(i) Limit - maximum current source flow i
MaxFI(i)… FI(i) =l= FH2Smax(i);
*MinFI(i) Limit - minimum flow of source current i
MinFI(i)… FI(i) =g= FH2Smin(i);
*CIYmin(j) Limit - the minimum required hydrogen concentration at the input to the consumer j
CIYmin(j)… YC(j) =g= YH2C(j);
*CIYmax(j) Limit - the maximum required hydrogen concentration at the input to the consumer j
CIYmax(j)… YC(j) =l= 1;
*Production(i) Production of hydrogen at source
Production(i)… FI(i) =e= sum(j,FIJ(i,j));
*Production(i)… FI(i) =e= sum(j,FIJ(i,j))+ sum(k,FIK(i,k));
LOZizvora(i) Production of hydrogen at the source waste stream
LOZizvora(i)… sum(k, FIKL(i,k))+ FLOZI(i)=e= fatorFH2I(i);
*CONSUMERS
*HMBJ(j) Consumption hydrogen balance
HMBJ(j)… sum(i,(FIJ(i,j)*YH2S(i)))+ sum(k,(FKJ(k,j)*YH2P(k))) =e= PHL(j);
*PHLJ(j) The balance of hydrogen at the consumer input
PHLJ(j)… PHL(j) =e= FH2C(j)*YH2C(j);
*MBJ(j) Balance of flows at the consumers input
MBJ(j)… sum(i, FIJ(i,j))+ sum(k, FKJ(k,j))=e= FC(j);
*SID(j) Defining inputs into a reactor j consumer
SID(j)… PHL(j) =e= FC(j)*YC(j);
*C(j) Consumption in the reactor
C(j)… DPHLJ(j)=e= (FH2C(j)*YH2C(j))- (FPpoc(j)*YPpoc(j));
*SC(j) Output of the reactor
SC(j)… PHLJO(j)=e= PHL(j)-DPHLJ(j);
*COY(j) Limit - hydrogen concentration at exit from the consumer
COY(j)… YP(j) =e= YPpoc(j);
*CYLOZ(j) Limit - hydrogen concentration at exit from consumer - waste stream
CYLOZ(j)… YLOZ(j) =e= YPpoc(j);
*HMBJO(j) The balance of hydrogen at the exit to the consumer
HMBJO(j)… PHLJO(j) =e= FP(j)*YP(j);
*SOD(j) Define the output from the reactor
SOD(j)… FP(j)=e= sum(k, FJK(j,k))+ FLOZ(j);
*PIC(k) Limit - Input Flow into purification Unit
*PURIFIER
PIC(k)… sum(i, FIK(i,k)) + sum(i, FIKL(i,k)) + sum(j, FJK(j,k))=l= Pmax(k);
*MBP(k) Balance of flows around the purifier unit
MBP(k)… sum(i, FIK(i,k)) + sum(i, FIKL(i,k)) + sum(j, FJK(j,k)) =e= sum(j,FKJ(k,j))+
FLOZP(k)+FLOZPR(k);
*HMBP(k) Hydrogen balance in the purifier unit
HMBP(k)… ((sum(i, FIK(i,k)*YH2S(i))+ sum(i, FIKL(i,k)*YIKL)+ sum(j, FJK(j,k)*YP(j)))) =e= (sum(j,
FKJ(k,j)*YH2P(k)))+ FLOZP(k)*YLOZP(k)+ FLOZPR(k)*YLOZP(k);
*LOZREC(k) Waste gas extraction from the Recovery Factor due to Recovery Factor
LOZREC(k)… FLOZPR(k)YLOZP(k)=e=(1-REC(k))((sum(i, FIK(i,k)*YH2S(i))+ sum(i, FIKL(i,k)*YIKL)+
sum(j, FJK(j,k)*YP(j))));
*LOZukupno total leaking gas
LOZukupno… LOZIVIPLIN =e= sum(i, FLOZI(i))+ sum(j, FLOZ(j))+ sum(k, FLOZP(k))+sum(k,
FLOZPR(k));
*COSTS
*PH2I(i) Production of hydrogen from sources
PH2I(i)… FH2I(i) =e= sum(j,FIJ(i,j))+ sum(k, FIK(i,k));
*H2I Cost of hydrogen production from sources
H2I… CH2I=e= sum(i,FH2I(i)*CH2S(i));
*PH2K(k) Production of hydrogen purifier
PH2K(k)… FH2K(k) =e= sum(j, FKJ(k,j));
*H2K(k) Cost of hydrogen production by individual purifier for each unit
H2K(k)… CH2K(k) =e= FH2K(k)*CH2P(k);
H2KU Cost of hydrogen production by total purifier
H2KU… CH2KU=e= sum(k, CH2K(k));
TKI(j) Costs of consumer compression by consumers
TKI(j)… TKIJ(j) =e= 0.6PWRsum(i,FIJ(i,j)*z(i,j));
TKIsuma Costs of compression from sources to consumers in total
TKIsuma… TKIU =e= sum(j, TKIJ(j));
TKK(j) Compression Costs from Procurers to Consumers
TKK(j)… TKKJ(j) =e= 0.6PWRsum(k,FKJ(k,j)*z2(k,j));
TKKsuma Costs of compression from consumables to consumers in total
TKKsuma… TKKU =e= sum(j,TKKJ(j));
TKJ(j) Costs of compression from consumers to procurers
TKJ(j)… TKJK(j) =e= 0.6PWRsum(k,FJK(j,k)*z3(j,k));
*TKJsuma Costs of compression from consumers to total costs
TKJsuma… TKJKU =e= sum(j,TKJK(j));
*TK Total compression costs
TK… TKU =e= TKIU+ TKKU+TKJKU;
*VLP The value of the leaking gas
VLP… VLPHM=e=(sum(i, FLOZI(i)*YIKL)+sum(j, FLOZ(j)YLOZ(j))+ sum(k,
FLOZP(k)YLOZP(k))+sum(k, FLOZPR(k)YLOZP(k)))HLPV +(sum(i, FLOZI(i)(1-YIKL))+sum(j, FLOZ(j)(1-
YLOZ(j)))+ sum(k, FLOZP(k)(1-YLOZP(k)))+sum(k, FLOZPR(k)(1-YLOZP(k))))*MLPV ;
*OBJ Objective function min. costs [FUNTE_ Nm3h-1]
OBJ… COST =e= CH2I + CH2KU + TKU - VLPHM;
model M /all/;
solve M using NLP minimizing COST;
display COST.L, FH2I.l, CH2I.l, FH2K.l, CH2K.l, CH2KU.l, TKU.L, TKIJ.l, TKIU.l, TKKJ.l, TKKU.l, TKJK.L,
TKJKU.L, LOZIVIPLIN.L, VLPHM.l, FI.l, FIK.L, FIKL.l, FLOZI.l, FC.l, YC.l, FIJ.L, FKJ.l, FP.l, YP.l, FJK.L, FLOZ.l,
FLOZP.l, FLOZPR.l, YLOZP;
parameter FJC(j) Total quantity at the entrance to the unit;
FJC(j)= sum(i,FIJ.l(i,j)) + sum(k,FKJ.l(k,j));
display FJC;
parameter YJC(j) the purity at the entrance to the unit;
YJC(j)= (sum(i,FIJ.l(i,j)*YH2S(i)) + sum(k,FKJ.l(k,j)*YH2P(k)))/(sum(i,FIJ.l(i,j)) + sum(k,FKJ.l(k,j)));
display YJC;
parameter KFulaz(k) The total flow rate on the purifier;
KFulaz(k) = sum(i, FIK.l(i,k)) +sum(j, FJK.l(j,k))+ sum(i, FIKL.L(i,k));
display KFulaz;
parameter KYulaz(k) The purity of the current on the insertion in the purifier;
KYulaz(k)= (sum(i, FIK.l(i,k)*YH2S(i))+ sum(j, FJK.l(j,k)*YP.l(j))+ sum(i, FIKL.L(i,k)*YIKL))/KFulaz(k);
display KYulaz;
parameter KFprocisceno Total flow of pure hydrogen after processing;
KFprocisceno= sum(k,sum(j,FKJ.l(k,j)));
display KFprocisceno;
parameter KFresidue(k) Residue flowrate;
KFresidue(k)=FLOZP.l(k)+FLOZPR.l(k);
display KFresidue;
parameter YLOZ_izlaz The purity of the total fuel gas;
YLOZ_izlaz= (sum(i, FLOZI.l(i)*YIKL)+sum(j, FLOZ.l(j)*YLOZ.l(j))+ sum(k, FLOZP.l(k)*YLOZP(k))+sum(k,
FLOZPR.l(k)*YLOZP(k)))/ LOZIVIPLIN.L;
display YLOZ_izlaz;