-
Notifications
You must be signed in to change notification settings - Fork 301
/
VLERoutines.cpp
1871 lines (1637 loc) · 77 KB
/
VLERoutines.cpp
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
#include "HelmholtzEOSMixtureBackend.h"
#include "VLERoutines.h"
#include "MatrixMath.h"
#include "MixtureDerivatives.h"
#include "Configuration.h"
#include "FlashRoutines.h"
namespace CoolProp {
void SaturationSolvers::saturation_critical(HelmholtzEOSMixtureBackend &HEOS, parameters ykey, CoolPropDbl y){
class inner_resid : public FuncWrapper1D{
public:
HelmholtzEOSMixtureBackend *HEOS;
CoolPropDbl T, desired_p;
inner_resid(HelmholtzEOSMixtureBackend *HEOS, CoolPropDbl T, CoolPropDbl desired_p)
: HEOS(HEOS), T(T), desired_p(desired_p){};
double call(double rhomolar_liq){
HEOS->SatL->update(DmolarT_INPUTS, rhomolar_liq, T);
CoolPropDbl calc_p = HEOS->SatL->p();
std::cout << format("inner p: %0.16Lg; res: %0.16Lg", calc_p, calc_p - desired_p) << std::endl;
return calc_p - desired_p;
}
};
// Define the outer residual to be driven to zero - this is the equality of
// Gibbs function for both co-existing phases
class outer_resid : public FuncWrapper1D
{
public:
HelmholtzEOSMixtureBackend *HEOS;
parameters ykey;
CoolPropDbl y;
CoolPropDbl rhomolar_crit;
outer_resid(HelmholtzEOSMixtureBackend &HEOS, CoolProp::parameters ykey, CoolPropDbl y)
: HEOS(&HEOS), ykey(ykey), y(y), rhomolar_crit(HEOS.rhomolar_critical()) {};
double call(double rhomolar_vap){
// Calculate the other variable (T->p or p->T) for given vapor density
CoolPropDbl T, p, rhomolar_liq;
switch (ykey){
case iT: {
T = y;
HEOS->SatV->update(DmolarT_INPUTS, rhomolar_vap, y);
p = HEOS->SatV->p();
std::cout << format("outer p: %0.16Lg", p) << std::endl;
inner_resid inner(HEOS, T, p);
rhomolar_liq = Brent(inner, rhomolar_crit*1.5, rhomolar_crit*(1 + 1e-8), LDBL_EPSILON, 1e-10, 100);
break;
}
default:
throw ValueError("Wrong input for outer_resid");
}
HEOS->SatL->update(DmolarT_INPUTS, rhomolar_liq, T);
HEOS->SatV->update(DmolarT_INPUTS, rhomolar_vap, T);
// Calculate the Gibbs functions for liquid and vapor
//CoolPropDbl gL = HEOS->SatL->gibbsmolar();
//CoolPropDbl gV = HEOS->SatV->gibbsmolar();
// Residual is difference in Gibbs function
// r = gL - gV;
return p;
};
};
outer_resid resid(HEOS, iT, y);
double rhomolar_crit = HEOS.rhomolar_critical();
Brent(&resid, rhomolar_crit*(1-1e-8), rhomolar_crit*0.5, DBL_EPSILON, 1e-9, 20);
}
void SaturationSolvers::saturation_T_pure_1D_P(HelmholtzEOSMixtureBackend &HEOS, CoolPropDbl T, saturation_T_pure_options &options)
{
// Define the residual to be driven to zero
class solver_resid : public FuncWrapper1D
{
public:
HelmholtzEOSMixtureBackend *HEOS;
CoolPropDbl T, rhomolar_liq, rhomolar_vap;
solver_resid(HelmholtzEOSMixtureBackend &HEOS, CoolPropDbl T, CoolPropDbl rhomolar_liq_guess, CoolPropDbl rhomolar_vap_guess)
: HEOS(&HEOS), T(T), rhomolar_liq(rhomolar_liq_guess), rhomolar_vap(rhomolar_vap_guess){};
double call(double p){
// Recalculate the densities using the current guess values
HEOS->SatL->update_TP_guessrho(T, p, rhomolar_liq);
HEOS->SatV->update_TP_guessrho(T, p, rhomolar_vap);
// Calculate the Gibbs functions for liquid and vapor
CoolPropDbl gL = HEOS->SatL->gibbsmolar();
CoolPropDbl gV = HEOS->SatV->gibbsmolar();
// Residual is difference in Gibbs function
return gL - gV;
};
};
solver_resid resid(HEOS, T, options.rhoL, options.rhoV);
if (!ValidNumber(options.p)){throw ValueError(format("options.p is not valid in saturation_T_pure_1D_P for T = %Lg",T));};
if (!ValidNumber(options.rhoL)){throw ValueError(format("options.rhoL is not valid in saturation_T_pure_1D_P for T = %Lg",T));};
if (!ValidNumber(options.rhoV)){throw ValueError(format("options.rhoV is not valid in saturation_T_pure_1D_P for T = %Lg",T));};
try{
Secant(resid, options.p, options.p*1.1, 1e-10, 100);
}
catch(...){
CoolPropDbl pmax = std::min(options.p*1.03, static_cast<CoolPropDbl>(HEOS.p_critical()+1e-6));
CoolPropDbl pmin = std::max(options.p*0.97, static_cast<CoolPropDbl>(HEOS.p_triple()-1e-6));
Brent(resid, pmin, pmax, LDBL_EPSILON, 1e-8, 100);
}
}
void SaturationSolvers::saturation_P_pure_1D_T(HelmholtzEOSMixtureBackend &HEOS, CoolPropDbl p, saturation_PHSU_pure_options &options){
// Define the residual to be driven to zero
class solver_resid : public FuncWrapper1D
{
public:
HelmholtzEOSMixtureBackend *HEOS;
CoolPropDbl p, rhomolar_liq, rhomolar_vap;
solver_resid(HelmholtzEOSMixtureBackend &HEOS, CoolPropDbl p, CoolPropDbl rhomolar_liq_guess, CoolPropDbl rhomolar_vap_guess)
: HEOS(&HEOS), p(p), rhomolar_liq(rhomolar_liq_guess), rhomolar_vap(rhomolar_vap_guess){};
double call(double T){
// Recalculate the densities using the current guess values
HEOS->SatL->update_TP_guessrho(T, p, rhomolar_liq);
HEOS->SatV->update_TP_guessrho(T, p, rhomolar_vap);
// Calculate the Gibbs functions for liquid and vapor
CoolPropDbl gL = HEOS->SatL->gibbsmolar();
CoolPropDbl gV = HEOS->SatV->gibbsmolar();
// Residual is difference in Gibbs function
return gL - gV;
};
};
solver_resid resid(HEOS, p, options.rhoL, options.rhoV);
if (!ValidNumber(options.T)){throw ValueError("options.T is not valid in saturation_P_pure_1D_T");};
if (!ValidNumber(options.rhoL)){throw ValueError("options.rhoL is not valid in saturation_P_pure_1D_T");};
if (!ValidNumber(options.rhoV)){throw ValueError("options.rhoV is not valid in saturation_P_pure_1D_T");};
CoolPropDbl Tmax = std::min(options.T + 2, static_cast<CoolPropDbl>(HEOS.T_critical()-1e-6));
CoolPropDbl Tmin = std::max(options.T - 2, static_cast<CoolPropDbl>(HEOS.Ttriple()+1e-6));
Brent(resid, Tmin, Tmax, LDBL_EPSILON, 1e-11, 100);
}
void SaturationSolvers::saturation_PHSU_pure(HelmholtzEOSMixtureBackend &HEOS, CoolPropDbl specified_value, saturation_PHSU_pure_options &options)
{
/*
This function is inspired by the method of Akasaka:
R. Akasaka,"A Reliable and Useful Method to Determine the Saturation State from
Helmholtz Energy Equations of State",
Journal of Thermal Science and Technology v3 n3,2008
Ancillary equations are used to get a sensible starting point
*/
std::vector<CoolPropDbl> negativer(3,_HUGE), v;
std::vector<std::vector<CoolPropDbl> > J(3, std::vector<CoolPropDbl>(3,_HUGE));
HEOS.calc_reducing_state();
const SimpleState & reduce = HEOS.get_reducing_state();
CoolProp::SimpleState crit = HEOS.get_state("reducing");
shared_ptr<HelmholtzEOSMixtureBackend> SatL = HEOS.SatL,
SatV = HEOS.SatV;
CoolPropDbl T, rhoL, rhoV, pL, pV, hL, sL, hV, sV;
CoolPropDbl deltaL=0, deltaV=0, tau=0, error;
int iter=0, specified_parameter;
// Use the density ancillary function as the starting point for the solver
try
{
if (options.specified_variable == saturation_PHSU_pure_options::IMPOSED_PL || options.specified_variable == saturation_PHSU_pure_options::IMPOSED_PV)
{
// Invert liquid density ancillary to get temperature
// TODO: fit inverse ancillaries too
try{
T = HEOS.get_components()[0].ancillaries.pL.invert(specified_value);
}
catch(...)
{
throw ValueError("Unable to invert ancillary equation");
}
}
else if (options.specified_variable == saturation_PHSU_pure_options::IMPOSED_HL)
{
CoolProp::SimpleState hs_anchor = HEOS.get_state("hs_anchor");
// Ancillary is deltah = h - hs_anchor.h
try{ T = HEOS.get_components()[0].ancillaries.hL.invert(specified_value - hs_anchor.hmolar); }
catch(...){
throw ValueError("Unable to invert ancillary equation for hL");
}
}
else if (options.specified_variable == saturation_PHSU_pure_options::IMPOSED_HV)
{
class Residual : public FuncWrapper1D
{
public:
CoolPropFluid *component;
double h;
Residual(CoolPropFluid &component, double h){
this->component = &component;
this->h = h;
}
double call(double T){
CoolPropDbl h_liq = component->ancillaries.hL.evaluate(T) + component->EOS().hs_anchor.hmolar;
return h_liq + component->ancillaries.hLV.evaluate(T) - h;
};
};
Residual resid(HEOS.get_components()[0], HEOS.hmolar());
// Ancillary is deltah = h - hs_anchor.h
CoolPropDbl Tmin_satL, Tmin_satV;
HEOS.calc_Tmin_sat(Tmin_satL, Tmin_satV);
double Tmin = Tmin_satL;
double Tmax = HEOS.calc_Tmax_sat();
try{ T = Brent(resid, Tmin-3, Tmax + 1, DBL_EPSILON, 1e-10, 50); }
catch(...){
shared_ptr<HelmholtzEOSMixtureBackend> HEOS_copy(new HelmholtzEOSMixtureBackend(HEOS.get_components()));
HEOS_copy->update(QT_INPUTS, 1, Tmin); double hTmin = HEOS_copy->hmolar();
HEOS_copy->update(QT_INPUTS, 1, Tmax); double hTmax = HEOS_copy->hmolar();
T = (Tmax-Tmin)/(hTmax-hTmin)*(HEOS.hmolar()-hTmin) + Tmin;
}
}
else if (options.specified_variable == saturation_PHSU_pure_options::IMPOSED_SL)
{
CoolPropFluid &component = HEOS.get_components()[0];
CoolProp::SaturationAncillaryFunction &anc = component.ancillaries.sL;
CoolProp::SimpleState hs_anchor = HEOS.get_state("hs_anchor");
// If near the critical point, use a near critical guess value for T
if (std::abs(HEOS.smolar() - crit.smolar) < std::abs(component.ancillaries.sL.get_max_abs_error()))
{
T = std::max(0.99*crit.T, crit.T-0.1);
}
else{
CoolPropDbl Tmin, Tmax, Tmin_satV;
HEOS.calc_Tmin_sat(Tmin, Tmin_satV);
Tmax = HEOS.calc_Tmax_sat();
// Ancillary is deltas = s - hs_anchor.s
// First try a conventional call
try{
T = anc.invert(specified_value - hs_anchor.smolar, Tmin, Tmax);
}
catch(...){
try{
T = anc.invert(specified_value - hs_anchor.smolar, Tmin - 3, Tmax + 3);
}
catch(...){
double vmin = anc.evaluate(Tmin);
double vmax = anc.evaluate(Tmax);
if (std::abs(specified_value - hs_anchor.smolar) < std::abs(vmax)){
T = Tmax - 0.1;
}
else{
throw ValueError(format("Unable to invert ancillary equation for sL for value %Lg with Tminval %g and Tmaxval %g ", specified_value - hs_anchor.smolar, vmin, vmax));
}
}
}
}
}
else if (options.specified_variable == saturation_PHSU_pure_options::IMPOSED_SV)
{
CoolPropFluid &component = HEOS.get_components()[0];
CoolProp::SimpleState hs_anchor = HEOS.get_state("hs_anchor");
class Residual : public FuncWrapper1D
{
public:
CoolPropFluid *component;
double s;
Residual(CoolPropFluid &component, double s){
this->component = &component;
this->s = s;
}
double call(double T){
CoolPropDbl s_liq = component->ancillaries.sL.evaluate(T) + component->EOS().hs_anchor.smolar;
CoolPropDbl resid = s_liq + component->ancillaries.sLV.evaluate(T) - s;
return resid;
};
};
Residual resid(component, HEOS.smolar());
// Ancillary is deltas = s - hs_anchor.s
CoolPropDbl Tmin_satL, Tmin_satV;
HEOS.calc_Tmin_sat(Tmin_satL, Tmin_satV);
double Tmin = Tmin_satL;
double Tmax = HEOS.calc_Tmax_sat();
try{
T = Brent(resid, Tmin-3, Tmax, DBL_EPSILON, 1e-10, 50);
}
catch(...){
CoolPropDbl vmax = resid.call(Tmax);
// If near the critical point, use a near critical guess value for T
if (std::abs(specified_value - hs_anchor.smolar) < std::abs(vmax)){
T = std::max(0.99*crit.T, crit.T-0.1);
}
else{
shared_ptr<HelmholtzEOSMixtureBackend> HEOS_copy(new HelmholtzEOSMixtureBackend(HEOS.get_components()));
HEOS_copy->update(QT_INPUTS, 1, Tmin); double sTmin = HEOS_copy->smolar();
HEOS_copy->update(QT_INPUTS, 1, Tmax); double sTmax = HEOS_copy->smolar();
T = (Tmax-Tmin)/(sTmax-sTmin)*(HEOS.smolar()-sTmin) + Tmin;
}
}
}
else
{
throw ValueError(format("options.specified_variable to saturation_PHSU_pure [%d] is invalid",options.specified_variable));
}
// If T from the ancillaries is above the critical temp, this will cause failure
// in ancillaries for rhoV and rhoL, decrease if needed
T = std::min(T, static_cast<CoolPropDbl>(HEOS.T_critical()-0.1));
// Evaluate densities from the ancillary equations
rhoV = HEOS.get_components()[0].ancillaries.rhoV.evaluate(T);
rhoL = HEOS.get_components()[0].ancillaries.rhoL.evaluate(T);
// Apply a single step of Newton's method to improve guess value for liquid
// based on the error between the gas pressure (which is usually very close already)
// and the liquid pressure, which can sometimes (especially at low pressure),
// be way off, and often times negative
SatL->update(DmolarT_INPUTS, rhoL, T);
SatV->update(DmolarT_INPUTS, rhoV, T);
double rhoL_updated = rhoL -(SatL->p()-SatV->p())/SatL->first_partial_deriv(iP, iDmolar, iT);
// Accept the update if the liquid density is greater than the vapor density
if (rhoL_updated > rhoV){ rhoL = rhoL_updated; }
// Update the state again with the better guess for the liquid density
SatL->update(DmolarT_INPUTS, rhoL, T);
SatV->update(DmolarT_INPUTS, rhoV, T);
deltaL = rhoL/reduce.rhomolar;
deltaV = rhoV/reduce.rhomolar;
tau = reduce.T/T;
}
catch(NotImplementedError &)
{
throw; // ??? What is this try...catch for?
}
do{
/*if (get_debug_level()>8){
std::cout << format("%s:%d: right before the derivs with deltaL = %g deltaV = %g tau = %g\n",__FILE__,__LINE__,deltaL, deltaV, tau).c_str();
}*/
pL = SatL->p();
hL = SatL->hmolar();
sL = SatL->smolar();
pV = SatV->p();
hV = SatV->hmolar();
sV = SatV->smolar();
// These derivatives are needed for both cases
CoolPropDbl alpharL = SatL->alphar();
CoolPropDbl alpharV = SatV->alphar();
CoolPropDbl dalphar_dtauL = SatL->dalphar_dTau();
CoolPropDbl dalphar_dtauV = SatV->dalphar_dTau();
CoolPropDbl d2alphar_ddelta_dtauL = SatL->d2alphar_dDelta_dTau();
CoolPropDbl d2alphar_ddelta_dtauV = SatV->d2alphar_dDelta_dTau();
CoolPropDbl dalphar_ddeltaL = SatL->dalphar_dDelta();
CoolPropDbl dalphar_ddeltaV = SatV->dalphar_dDelta();
CoolPropDbl d2alphar_ddelta2L = SatL->d2alphar_dDelta2();
CoolPropDbl d2alphar_ddelta2V = SatV->d2alphar_dDelta2();
// -r_1 (equate the pressures)
negativer[0] = -(deltaV*(1+deltaV*dalphar_ddeltaV)-deltaL*(1+deltaL*dalphar_ddeltaL));
// -r_2 (equate the gibbs energy)
negativer[1] = -(deltaV*dalphar_ddeltaV+alpharV+log(deltaV)-deltaL*dalphar_ddeltaL-alpharL-log(deltaL));
switch (options.specified_variable){
case saturation_PHSU_pure_options::IMPOSED_PL:
// -r_3 (equate calculated pressure and specified liquid pressure)
negativer[2] = -(pL/specified_value - 1); break;
case saturation_PHSU_pure_options::IMPOSED_PV:
// -r_3 (equate calculated pressure and specified vapor pressure)
negativer[2] = -(pV/specified_value - 1); break;
case saturation_PHSU_pure_options::IMPOSED_HL:
// -r_3 (equate calculated liquid enthalpy and specified liquid enthalpy)
negativer[2] = -(hL - specified_value); break;
case saturation_PHSU_pure_options::IMPOSED_HV:
// -r_3 (equate calculated vapor enthalpy and specified vapor enthalpy)
negativer[2] = -(hV - specified_value); break;
case saturation_PHSU_pure_options::IMPOSED_SL:
// -r_3 (equate calculated liquid entropy and specified liquid entropy)
negativer[2] = -(sL - specified_value); break;
case saturation_PHSU_pure_options::IMPOSED_SV:
// -r_3 (equate calculated vapor entropy and specified vapor entropy)
negativer[2] = -(sV - specified_value); break;
default:
throw ValueError(format("options.specified_variable to saturation_PHSU_pure [%d] is invalid",options.specified_variable));
}
// dr1_dtau
J[0][0] = pow(deltaV,2)*d2alphar_ddelta_dtauV-pow(deltaL,2)*d2alphar_ddelta_dtauL;
// dr2_dtau
J[1][0] = deltaV*d2alphar_ddelta_dtauV+dalphar_dtauV-deltaL*d2alphar_ddelta_dtauL-dalphar_dtauL;
if (options.use_logdelta){
// dr_1/d_log(delta'')
J[0][1] = -deltaL-2*pow(deltaL,2)*dalphar_ddeltaL-pow(deltaL,3)*d2alphar_ddelta2L;
// dr_2/d_log(delta'')
J[1][1] = -pow(deltaL,2)*d2alphar_ddelta2L-2*deltaL*dalphar_ddeltaL-1;
}
else{
// dr_1/ddelta''
J[0][1] = -1-2*deltaL*dalphar_ddeltaL-pow(deltaL,2)*d2alphar_ddelta2L;
// dr_2/ddelta''
J[1][1] = -1/deltaL-2*dalphar_ddeltaL-deltaL*d2alphar_ddelta2L;
}
if (options.use_logdelta){
// dr_1/d_log(delta'')
J[0][2] = deltaV+2*pow(deltaV,2)*dalphar_ddeltaV+pow(deltaV,3)*d2alphar_ddelta2V;
// dr_2/d_log(delta'')
J[1][2] = 1+2*deltaV*dalphar_ddeltaV+1+pow(deltaV,2)*d2alphar_ddelta2V;
}
else{
// dr_1/ddelta''
J[0][2] = 1+2*deltaV*dalphar_ddeltaV+pow(deltaV,2)*d2alphar_ddelta2V;
// dr_2/ddelta''
J[1][2] = deltaV*d2alphar_ddelta2V+2*dalphar_ddeltaV+1/deltaV;
}
// Derivatives of the specification equation
switch (options.specified_variable){
case saturation_PHSU_pure_options::IMPOSED_PL:
// dr_3/dtau
J[2][0] = SatL->first_partial_deriv(iP,iTau,iDelta)/specified_value;
if (options.use_logdelta){
// dr_3/d(log(delta'))
J[2][1] = deltaL*SatL->first_partial_deriv(iP,iDelta,iTau)/specified_value;
}
else{
// dr_3/ddelta'
J[2][1] = SatL->first_partial_deriv(iP,iDelta,iTau)/specified_value;
}
// dr_3/ddelta'' (liquid pressure not a function of vapor density)
J[2][2] = 0;
specified_parameter = CoolProp::iP;
break;
case saturation_PHSU_pure_options::IMPOSED_PV:
// dr_3/dtau
J[2][0] = SatV->first_partial_deriv(iP,iTau,iDelta)/specified_value;
// dr_3/ddelta' (vapor pressure not a function of liquid density)
J[2][1] = 0;
if (options.use_logdelta){
// dr_3/d(log(delta'')
J[2][2] = deltaV*SatV->first_partial_deriv(iP,iDelta,iTau)/specified_value;
}
else{
// dr_3/ddelta''
J[2][2] = SatV->first_partial_deriv(iP,iDelta,iTau)/specified_value;
}
specified_parameter = CoolProp::iP;
break;
case saturation_PHSU_pure_options::IMPOSED_HL:
// dr_3/dtau
J[2][0] = SatL->first_partial_deriv(iHmolar,iTau,iDelta);
// dr_3/ddelta'
J[2][1] = SatL->first_partial_deriv(iHmolar,iDelta,iTau);
if (options.use_logdelta){ J[2][1]*=deltaL;}
// dr_3/ddelta''
J[2][2] = 0; //(liquid enthalpy not a function of vapor density)
specified_parameter = CoolProp::iHmolar;
break;
case saturation_PHSU_pure_options::IMPOSED_HV:
// dr_3/dtau
J[2][0] = SatV->first_partial_deriv(iHmolar,iTau,iDelta);
// dr_3/ddelta'
J[2][1] = 0; //(vapor enthalpy not a function of liquid density)
// dr_3/ddelta''
J[2][2] = SatV->first_partial_deriv(iHmolar,iDelta,iTau);
if (options.use_logdelta){ J[2][2]*=deltaV;}
specified_parameter = CoolProp::iHmolar;
break;
case saturation_PHSU_pure_options::IMPOSED_SL:
// dr_3/dtau
J[2][0] = SatL->first_partial_deriv(iSmolar,iTau,iDelta);
// dr_3/ddelta'
J[2][1] = SatL->first_partial_deriv(iSmolar,iDelta,iTau);
if (options.use_logdelta){ J[2][1] *= deltaL; }
// dr_3/ddelta''
J[2][2] = 0; //(liquid entropy not a function of vapor density)
specified_parameter = CoolProp::iSmolar;
break;
case saturation_PHSU_pure_options::IMPOSED_SV:
// dr_3/dtau
J[2][0] = SatV->first_partial_deriv(iSmolar,iTau,iDelta);
// dr_3/ddelta'
J[2][1] = 0; //(vapor enthalpy not a function of liquid density)
// dr_3/ddelta''
J[2][2] = SatV->first_partial_deriv(iSmolar,iDelta,iTau);
if (options.use_logdelta){ J[2][2]*=deltaV;}
specified_parameter = CoolProp::iSmolar;
break;
default:
throw ValueError(format("options.specified_variable to saturation_PHSU_pure [%d] is invalid",options.specified_variable));
}
v = linsolve(J, negativer);
// Conditions for an acceptable step are:
// a) tau > 1
// b) rhoL > rhoV or deltaL > deltaV
double tau0 = tau, deltaL0 = deltaL, deltaV0 = deltaV;
for (double omega_local = 1.0; omega_local > 0.1; omega_local /= 1.1)
{
tau = tau0 + omega_local*options.omega*v[0];
if (options.use_logdelta){
deltaL = exp(log(deltaL0)+omega_local*options.omega*v[1]);
deltaV = exp(log(deltaV0)+omega_local*options.omega*v[2]);
}
else{
deltaL = deltaL0 + omega_local*options.omega*v[1];
deltaV = deltaV0 + omega_local*options.omega*v[2];
}
if (tau > 1 && deltaL > deltaV){
break;
}
}
rhoL = deltaL*reduce.rhomolar;
rhoV = deltaV*reduce.rhomolar;
T = reduce.T/tau;
SatL->update(DmolarT_INPUTS, rhoL, T);
SatV->update(DmolarT_INPUTS, rhoV, T);
error = sqrt(pow(negativer[0], 2)+pow(negativer[1], 2)+pow(negativer[2], 2));
iter++;
if (T < 0)
{
throw SolutionError(format("saturation_PHSU_pure solver T < 0"));
}
// If the change is very small, stop
if (max_abs_value(v) < 1e-10){
break;
}
if (iter > 50){
// Set values back into the options structure for use in next solver
options.rhoL = rhoL; options.rhoV = rhoV; options.T = T;
// Error out
std::string info = get_parameter_information(specified_parameter, "short");
throw SolutionError(format("saturation_PHSU_pure solver did not converge after 50 iterations for %s=%Lg current error is %Lg", info.c_str(), specified_value, error));
}
}
while (error > 1e-9);
}
void SaturationSolvers::saturation_D_pure(HelmholtzEOSMixtureBackend &HEOS, CoolPropDbl rhomolar, saturation_D_pure_options &options)
{
/*
This function is inspired by the method of Akasaka:
R. Akasaka,"A Reliable and Useful Method to Determine the Saturation State from
Helmholtz Energy Equations of State",
Journal of Thermal Science and Technology v3 n3,2008
Ancillary equations are used to get a sensible starting point
*/
std::vector<CoolPropDbl> r(2,_HUGE), v;
std::vector<std::vector<CoolPropDbl> > J(2, std::vector<CoolPropDbl>(2,_HUGE));
HEOS.calc_reducing_state();
const SimpleState & reduce = HEOS.get_reducing_state();
shared_ptr<HelmholtzEOSMixtureBackend> SatL = HEOS.SatL,
SatV = HEOS.SatV;
CoolPropDbl T, rhoL,rhoV;
CoolPropDbl deltaL=0, deltaV=0, tau=0, error, p_error;
int iter=0;
// Use the density ancillary function as the starting point for the solver
try
{
if (options.imposed_rho == saturation_D_pure_options::IMPOSED_RHOL)
{
// Invert liquid density ancillary to get temperature
// TODO: fit inverse ancillaries too
T = HEOS.get_components()[0].ancillaries.rhoL.invert(rhomolar);
rhoV = HEOS.get_components()[0].ancillaries.rhoV.evaluate(T);
rhoL = rhomolar;
}
else if (options.imposed_rho == saturation_D_pure_options::IMPOSED_RHOV)
{
// Invert vapor density ancillary to get temperature
// TODO: fit inverse ancillaries too
T = HEOS.get_components()[0].ancillaries.rhoV.invert(rhomolar);
rhoL = HEOS.get_components()[0].ancillaries.rhoL.evaluate(T);
rhoV = rhomolar;
}
else
{
throw ValueError(format("imposed rho to saturation_D_pure [%d%] is invalid",options.imposed_rho));
}
deltaL = rhoL/reduce.rhomolar;
deltaV = rhoV/reduce.rhomolar;
tau = reduce.T/T;
}
catch(NotImplementedError &)
{
throw; // ??? What is this try...catch for?
}
do{
/*if (get_debug_level()>8){
std::cout << format("%s:%d: right before the derivs with deltaL = %g deltaV = %g tau = %g\n",__FILE__,__LINE__,deltaL, deltaV, tau).c_str();
}*/
// Calculate once to save on calls to EOS
SatL->update(DmolarT_INPUTS, rhoL, T);
SatV->update(DmolarT_INPUTS, rhoV, T);
CoolPropDbl pL = SatL->p();
CoolPropDbl pV = SatV->p();
// These derivatives are needed for both cases
CoolPropDbl dalphar_dtauL = SatL->dalphar_dTau();
CoolPropDbl dalphar_dtauV = SatV->dalphar_dTau();
CoolPropDbl d2alphar_ddelta_dtauL = SatL->d2alphar_dDelta_dTau();
CoolPropDbl d2alphar_ddelta_dtauV = SatV->d2alphar_dDelta_dTau();
CoolPropDbl alpharL = SatL->alphar();
CoolPropDbl alpharV = SatV->alphar();
CoolPropDbl dalphar_ddeltaL = SatL->dalphar_dDelta();
CoolPropDbl dalphar_ddeltaV = SatV->dalphar_dDelta();
// -r_1
r[0] = -(deltaV*(1+deltaV*dalphar_ddeltaV)-deltaL*(1+deltaL*dalphar_ddeltaL));
// -r_2
r[1] = -(deltaV*dalphar_ddeltaV+alpharV+log(deltaV)-deltaL*dalphar_ddeltaL-alpharL-log(deltaL));
// dr1_dtau
J[0][0] = pow(deltaV,2)*d2alphar_ddelta_dtauV-pow(deltaL,2)*d2alphar_ddelta_dtauL;
// dr2_dtau
J[1][0] = deltaV*d2alphar_ddelta_dtauV+dalphar_dtauV-deltaL*d2alphar_ddelta_dtauL-dalphar_dtauL;
if (options.imposed_rho == saturation_D_pure_options::IMPOSED_RHOL)
{
CoolPropDbl d2alphar_ddelta2V = SatV->d2alphar_dDelta2();
if (options.use_logdelta)
{
J[0][1] = deltaV+2*pow(deltaV,2)*dalphar_ddeltaV+pow(deltaV,3)*d2alphar_ddelta2V;
J[1][1] = pow(deltaV,2)*d2alphar_ddelta2V+2*deltaV*dalphar_ddeltaV+1;
}
else
{
J[0][1] = 1+2*deltaV*dalphar_ddeltaV+pow(deltaV,2)*d2alphar_ddelta2V;
J[1][1] = deltaV*d2alphar_ddelta2V+2*dalphar_ddeltaV+1/deltaV;
}
}
else if (options.imposed_rho == saturation_D_pure_options::IMPOSED_RHOV)
{
CoolPropDbl d2alphar_ddelta2L = SatL->d2alphar_dDelta2();
if (options.use_logdelta)
{
J[0][1] = -deltaL-2*pow(deltaL,2)*dalphar_ddeltaL-pow(deltaL,3)*d2alphar_ddelta2L;
J[1][1] = -pow(deltaL,2)*d2alphar_ddelta2L-2*deltaL*dalphar_ddeltaL-1;
}
else
{
J[0][1] = -1-2*deltaL*dalphar_ddeltaL-pow(deltaL,2)*d2alphar_ddelta2L;
J[1][1] = -deltaL*d2alphar_ddelta2L-2*dalphar_ddeltaL-1/deltaL;
}
}
//double DET = J[0][0]*J[1][1]-J[0][1]*J[1][0];
v = linsolve(J, r);
tau += options.omega*v[0];
if (options.imposed_rho == saturation_D_pure_options::IMPOSED_RHOL)
{
if (options.use_logdelta)
deltaV = exp(log(deltaV)+options.omega*v[1]);
else
deltaV += v[1];
}
else
{
if (options.use_logdelta)
deltaL = exp(log(deltaL)+options.omega*v[1]);
else
deltaL += v[1];
}
rhoL = deltaL*reduce.rhomolar;
rhoV = deltaV*reduce.rhomolar;
T = reduce.T/tau;
p_error = (pL-pV)/pL;
error = sqrt(pow(r[0], 2)+pow(r[1], 2));
iter++;
if (T < 0)
{
throw SolutionError(format("saturation_D_pure solver T < 0"));
}
if (iter > 200){
throw SolutionError(format("saturation_D_pure solver did not converge after 100 iterations with rho: %g mol/m^3",rhomolar));
}
}
while (error > 1e-9);
CoolPropDbl p_error_limit = 1e-3;
if (std::abs(p_error) > p_error_limit){
throw SolutionError(format("saturation_D_pure solver abs error on p [%Lg] > limit [%Lg]", p_error, p_error_limit));
}
}
void SaturationSolvers::saturation_T_pure(HelmholtzEOSMixtureBackend &HEOS, CoolPropDbl T, saturation_T_pure_options &options)
{
// Set some imput options
SaturationSolvers::saturation_T_pure_Akasaka_options _options(false);
_options.omega = 1.0;
try{
// Actually call the solver
SaturationSolvers::saturation_T_pure_Maxwell(HEOS, T, _options);
}
catch(...){
try{
// Actually call the solver
SaturationSolvers::saturation_T_pure_Akasaka(HEOS, T, _options);
}
catch(...){
// If there was an error, store values for use in later solvers
options.pL = _options.pL;
options.pV = _options.pV;
options.rhoL = _options.rhoL;
options.rhoV = _options.rhoV;
options.p = _options.pL;
SaturationSolvers::saturation_T_pure_1D_P(HEOS, T, options);
}
}
}
void SaturationSolvers::saturation_T_pure_Akasaka(HelmholtzEOSMixtureBackend &HEOS, CoolPropDbl T, saturation_T_pure_Akasaka_options &options)
{
// Start with the method of Akasaka
/*
This function implements the method of Akasaka
R. Akasaka,"A Reliable and Useful Method to Determine the Saturation State from
Helmholtz Energy Equations of State",
Journal of Thermal Science and Technology v3 n3,2008
Ancillary equations are used to get a sensible starting point
*/
HEOS.calc_reducing_state();
const SimpleState & reduce = HEOS.get_reducing_state();
CoolPropDbl R_u = HEOS.gas_constant();
shared_ptr<HelmholtzEOSMixtureBackend> SatL = HEOS.SatL,
SatV = HEOS.SatV;
CoolPropDbl rhoL = _HUGE, rhoV = _HUGE,JL,JV,KL,KV,dJL,dJV,dKL,dKV;
CoolPropDbl DELTA, deltaL=0, deltaV=0, error, PL, PV, stepL, stepV;
int iter=0;
try
{
if (options.use_guesses)
{
// Use the guesses provided in the options structure
rhoL = options.rhoL;
rhoV = options.rhoV;
}
else
{
// Use the density ancillary function as the starting point for the solver
// If very close to the critical temp, evaluate the ancillaries for a slightly lower temperature
if (T > 0.99*HEOS.get_reducing_state().T){
rhoL = HEOS.get_components()[0].ancillaries.rhoL.evaluate(T-0.1);
rhoV = HEOS.get_components()[0].ancillaries.rhoV.evaluate(T-0.1);
}
else{
rhoL = HEOS.get_components()[0].ancillaries.rhoL.evaluate(T);
rhoV = HEOS.get_components()[0].ancillaries.rhoV.evaluate(T);
// Apply a single step of Newton's method to improve guess value for liquid
// based on the error between the gas pressure (which is usually very close already)
// and the liquid pressure, which can sometimes (especially at low pressure),
// be way off, and often times negative
SatL->update(DmolarT_INPUTS, rhoL, T);
SatV->update(DmolarT_INPUTS, rhoV, T);
// Update the guess for liquid density using density solver with vapor pressure
// and liquid density guess from ancillaries
HEOS.specify_phase(iphase_liquid);
rhoL = HEOS.solver_rho_Tp(T, SatV->p(), rhoL);
HEOS.unspecify_phase();
}
}
deltaL = rhoL/reduce.rhomolar;
deltaV = rhoV/reduce.rhomolar;
}
catch(NotImplementedError &)
{
/*double Tc = crit.T;
double pc = crit.p.Pa;
double w = 6.67228479e-09*Tc*Tc*Tc-7.20464352e-06*Tc*Tc+3.16947758e-03*Tc-2.88760012e-01;
double q = -6.08930221451*w -5.42477887222;
double pt = exp(q*(Tc/T-1))*pc;*/
//double rhoL = density_Tp_Soave(T, pt, 0), rhoV = density_Tp_Soave(T, pt, 1);
//deltaL = rhoL/reduce.rhomolar;
//deltaV = rhoV/reduce.rhomolar;
//tau = reduce.T/T;
}
//if (get_debug_level()>5){
// std::cout << format("%s:%d: Akasaka guess values deltaL = %g deltaV = %g tau = %g\n",__FILE__,__LINE__,deltaL, deltaV, tau).c_str();
// }
do{
/*if (get_debug_level()>8){
std::cout << format("%s:%d: right before the derivs with deltaL = %g deltaV = %g tau = %g\n",__FILE__,__LINE__,deltaL, deltaV, tau).c_str();
}*/
// Calculate once to save on calls to EOS
SatL->update(DmolarT_INPUTS, rhoL, T);
SatV->update(DmolarT_INPUTS, rhoV, T);
CoolPropDbl alpharL = SatL->alphar();
CoolPropDbl alpharV = SatV->alphar();
CoolPropDbl dalphar_ddeltaL = SatL->dalphar_dDelta();
CoolPropDbl dalphar_ddeltaV = SatV->dalphar_dDelta();
CoolPropDbl d2alphar_ddelta2L = SatL->d2alphar_dDelta2();
CoolPropDbl d2alphar_ddelta2V = SatV->d2alphar_dDelta2();
JL = deltaL * (1 + deltaL*dalphar_ddeltaL);
JV = deltaV * (1 + deltaV*dalphar_ddeltaV);
KL = deltaL*dalphar_ddeltaL + alpharL + log(deltaL);
KV = deltaV*dalphar_ddeltaV + alpharV + log(deltaV);
PL = R_u*reduce.rhomolar*T*JL;
PV = R_u*reduce.rhomolar*T*JV;
// At low pressure, the magnitude of d2alphar_ddelta2L and d2alphar_ddelta2V are enormous, truncation problems arise for all the partials
dJL = 1 + 2*deltaL*dalphar_ddeltaL + deltaL*deltaL*d2alphar_ddelta2L;
dJV = 1 + 2*deltaV*dalphar_ddeltaV + deltaV*deltaV*d2alphar_ddelta2V;
dKL = 2*dalphar_ddeltaL + deltaL*d2alphar_ddelta2L + 1/deltaL;
dKV = 2*dalphar_ddeltaV + deltaV*d2alphar_ddelta2V + 1/deltaV;
DELTA = dJV*dKL-dJL*dKV;
error = sqrt((KL-KV)*(KL-KV)+(JL-JV)*(JL-JV));
// Get the predicted step
stepL = options.omega/DELTA*( (KV-KL)*dJV-(JV-JL)*dKV);
stepV = options.omega/DELTA*( (KV-KL)*dJL-(JV-JL)*dKL);
CoolPropDbl deltaL0 = deltaL, deltaV0 = deltaV;
// Conditions for an acceptable step are:
// a) rhoL > rhoV or deltaL > deltaV
for (double omega_local = 1.0; omega_local > 0.1; omega_local /= 1.1)
{
deltaL = deltaL0 + omega_local*stepL;
deltaV = deltaV0 + omega_local*stepV;
if (deltaL > 1 && deltaV < 1 && deltaV > 0){
break;
}
}
rhoL = deltaL*reduce.rhomolar;
rhoV = deltaV*reduce.rhomolar;
iter++;
if (iter > 100){
throw SolutionError(format("Akasaka solver did not converge after 100 iterations"));
}
}
while (error > 1e-10 && std::abs(stepL) > 10*DBL_EPSILON*std::abs(stepL) && std::abs(stepV) > 10*DBL_EPSILON*std::abs(stepV));
CoolPropDbl p_error_limit = 1e-3;
CoolPropDbl p_error = (PL - PV)/PL;
if (std::abs(p_error) > p_error_limit){
options.pL = PL;
options.pV = PV;
options.rhoL = rhoL;
options.rhoV = rhoV;
throw SolutionError(format("saturation_T_pure_Akasaka solver abs error on p [%g] > limit [%g]", std::abs(p_error), p_error_limit));
}
}
CoolPropDbl sign(CoolPropDbl x)
{
if (x > 0){
return 1;
}
else{
return -1;
}
}
void SaturationSolvers::saturation_T_pure_Maxwell(HelmholtzEOSMixtureBackend &HEOS, CoolPropDbl T, saturation_T_pure_Akasaka_options &options)
{
/*
This function implements the method of
Ancillary equations are used to get a sensible starting point
*/
HEOS.calc_reducing_state();
shared_ptr<HelmholtzEOSMixtureBackend> SatL = HEOS.SatL,
SatV = HEOS.SatV;
CoolProp::SimpleState &crit = HEOS.get_components()[0].crit;
CoolPropDbl rhoL = _HUGE, rhoV = _HUGE, error = 999, DeltavL, DeltavV, pL, pV, p, last_error;
int iter = 0,
small_step_count = 0,
backwards_step_count = 0; // Counter for the number of times you have taken a step that increases error
try
{
if (options.use_guesses)
{
// Use the guesses provided in the options structure
rhoL = options.rhoL;
rhoV = options.rhoV;
}
else
{
// Use the density ancillary function as the starting point for the solver
// If very close to the critical temp, evaluate the ancillaries for a slightly lower temperature
if (T > 0.9999*HEOS.get_reducing_state().T){
rhoL = HEOS.get_components()[0].ancillaries.rhoL.evaluate(T-0.1);
rhoV = HEOS.get_components()[0].ancillaries.rhoV.evaluate(T-0.1);
}
else{
rhoL = HEOS.get_components()[0].ancillaries.rhoL.evaluate(T);
rhoV = HEOS.get_components()[0].ancillaries.rhoV.evaluate(T);
p = HEOS.get_components()[0].ancillaries.pV.evaluate(T);
CoolProp::SimpleState &tripleL = HEOS.get_components()[0].triple_liquid;
CoolProp::SimpleState &tripleV = HEOS.get_components()[0].triple_vapor;
// If the guesses are terrible, apply a simple correction
// but only if the limits are being checked
if ((rhoL < crit.rhomolar*0.8 || rhoL > tripleL.rhomolar*1.2 ||
rhoV > crit.rhomolar*1.2 || rhoV < tripleV.rhomolar*0.8)
&&
!get_config_bool(DONT_CHECK_PROPERTY_LIMITS)
)
{
// Lets assume that liquid density is more or less linear with T
rhoL = (crit.rhomolar - tripleL.rhomolar)/(crit.T - tripleL.T)*(T-tripleL.T)+tripleL.rhomolar;
// Then we calculate pressure from this density
SatL->update_DmolarT_direct(rhoL, T);
// Then we assume vapor to be ideal gas
rhoV = SatL->p()/(SatL->gas_constant()*T);
// Update the vapor state
SatV->update_DmolarT_direct(rhoV, T);
}
else{
SatL->update_DmolarT_direct(rhoL, T);
SatV->update_DmolarT_direct(rhoV, T);
}
if (get_debug_level() > 5){ std::cout << format("[Maxwell] ancillaries T: %0.16Lg rhoL: %0.16Lg rhoV: %0.16Lg pL: %g pV: %g\n", T, rhoL, rhoV, SatL->p(), SatV->p());}
// Update the guess for liquid density using density solver with vapor pressure
// and liquid density guess from ancillaries, but only if the pressures are not
// close to each other
if (std::abs((SatL->p()-p)/p) > 0.1){
for (int iii = 0; iii < 6; ++iii){
// Use Halley's method to update the liquid density (http://en.wikipedia.org/wiki/Halley%27s_method)
CoolPropDbl f = SatL->p()-SatV->p();
CoolPropDbl dpdrho = SatL->first_partial_deriv(iP,iDmolar,iT);
CoolPropDbl d2pdrho2 = SatL->second_partial_deriv(iP,iDmolar,iT,iDmolar,iT);
CoolPropDbl deltarhoLHalley = -(2*f*dpdrho)/(2*POW2(dpdrho)-f*d2pdrho2);
rhoL += deltarhoLHalley;
if (std::abs(deltarhoLHalley/rhoL) < DBL_EPSILON){
break;
}
SatL->update_DmolarT_direct(rhoL, T);
}
}
SatL->update_DmolarT_direct(rhoL, T);
SatV->update_DmolarT_direct(rhoV, T);
// Update the guess for vapor density using density solver with vapor pressure
// and density guess from ancillaries, but only if the pressures are not
// close to each other
if (std::abs((SatV->p()-p)/p) > 0.1){
HEOS.specify_phase(iphase_gas);
rhoV = SatV->solver_rho_Tp(T, p, rhoV);
HEOS.unspecify_phase();
}
}
}
}