11use crate :: equation_of_state:: { EquationOfState , HelmholtzEnergy , HelmholtzEnergyDual } ;
22use crate :: joback:: JobackRecord ;
3- use crate :: parameter:: { BinaryRecord , Identifier , Parameter , ParameterError , PureRecord } ;
3+ use crate :: parameter:: { Identifier , Parameter , ParameterError , PureRecord } ;
44use crate :: si:: { GRAM , MOL } ;
55use crate :: state:: StateHD ;
66use crate :: MolarWeight ;
7- use ndarray:: { Array , Array1 , Array2 } ;
7+ use ndarray:: { Array1 , Array2 } ;
88use num_dual:: DualNum ;
99use quantity:: si:: { SIArray1 , SIUnit } ;
1010use serde:: { Deserialize , Serialize } ;
1111use std:: collections:: HashMap ;
1212use std:: f64:: consts:: SQRT_2 ;
13+ use std:: rc:: Rc ;
1314
1415const KB_A3 : f64 = 13806490.0 ;
1516
@@ -33,7 +34,6 @@ impl std::fmt::Display for PengRobinsonRecord {
3334}
3435
3536/// Peng-Robinson parameters for one ore more substances.
36- #[ derive( Serialize , Deserialize , Debug , Clone , Default ) ]
3737pub struct PengRobinsonParameters {
3838 tc : Array1 < f64 > ,
3939 a : Array1 < f64 > ,
@@ -43,17 +43,6 @@ pub struct PengRobinsonParameters {
4343 molarweight : Array1 < f64 > ,
4444 pure_records : Vec < PureRecord < PengRobinsonRecord , JobackRecord > > ,
4545 joback_records : Option < Vec < JobackRecord > > ,
46- binary_records : Option < Vec < BinaryRecord < Identifier , f64 > > > ,
47- }
48-
49- impl PengRobinsonParameters {
50- pub fn subset ( & self , component_list : & [ usize ] ) -> Self {
51- let pure_records = component_list
52- . iter ( )
53- . map ( |& i| self . pure_records [ i] . clone ( ) )
54- . collect ( ) ;
55- Self :: from_records ( pure_records, self . binary_records . clone ( ) ) . unwrap ( )
56- }
5746}
5847
5948impl PengRobinsonParameters {
@@ -83,7 +72,10 @@ impl PengRobinsonParameters {
8372 PureRecord :: new ( id, molarweight[ i] , None , Some ( record) , None )
8473 } )
8574 . collect ( ) ;
86- PengRobinsonParameters :: from_records ( records, None )
75+ Ok ( PengRobinsonParameters :: from_records (
76+ records,
77+ Array2 :: zeros ( [ pc. len ( ) ; 2 ] ) ,
78+ ) )
8779 }
8880}
8981
@@ -94,8 +86,8 @@ impl Parameter for PengRobinsonParameters {
9486
9587 fn from_records (
9688 pure_records : Vec < PureRecord < Self :: Pure , Self :: IdealGas > > ,
97- binary_records : Option < Vec < crate :: parameter :: BinaryRecord < Identifier , Self :: Binary > > > ,
98- ) -> Result < Self , crate :: parameter :: ParameterError > {
89+ binary_records : Array2 < Self :: Binary > ,
90+ ) -> Self {
9991 let n = pure_records. len ( ) ;
10092
10193 let mut tc = Array1 :: zeros ( n) ;
@@ -123,44 +115,39 @@ impl Parameter for PengRobinsonParameters {
123115 } ;
124116 }
125117
126- let mut k_ij = Array :: zeros ( [ n, n] ) ;
127- match & binary_records {
128- Some ( bs) => bs. iter ( ) . for_each ( |record| {
129- let i = component_index. get ( & record. id1 ) ;
130- let j = component_index. get ( & record. id2 ) ;
131- if let ( Some ( i) , Some ( j) ) = ( i, j) {
132- k_ij[ [ * i, * j] ] = record. model_record ;
133- k_ij[ [ * j, * i] ] = record. model_record
134- }
135- } ) ,
136- None => ( ) ,
137- }
138-
139118 let joback_records = pure_records
140119 . iter ( )
141120 . map ( |r| r. ideal_gas_record . clone ( ) )
142121 . collect ( ) ;
143122
144- Ok ( Self {
123+ Self {
145124 tc,
146125 a,
147126 b,
148- k_ij,
127+ k_ij : binary_records ,
149128 kappa,
150129 molarweight,
151130 pure_records,
152131 joback_records,
153- binary_records,
154- } )
132+ }
133+ }
134+
135+ fn records (
136+ & self ,
137+ ) -> (
138+ & [ PureRecord < PengRobinsonRecord , JobackRecord > ] ,
139+ & Array2 < f64 > ,
140+ ) {
141+ ( & self . pure_records , & self . k_ij )
155142 }
156143}
157144
158145pub struct PengRobinson {
159- parameters : PengRobinsonParameters ,
146+ parameters : Rc < PengRobinsonParameters > ,
160147}
161148
162149impl PengRobinson {
163- pub fn new ( parameters : PengRobinsonParameters ) -> Self {
150+ pub fn new ( parameters : Rc < PengRobinsonParameters > ) -> Self {
164151 Self { parameters }
165152 }
166153}
@@ -171,7 +158,7 @@ impl EquationOfState for PengRobinson {
171158 }
172159
173160 fn subset ( & self , component_list : & [ usize ] ) -> Self {
174- Self :: new ( self . parameters . subset ( component_list) )
161+ Self :: new ( Rc :: new ( self . parameters . subset ( component_list) ) )
175162 }
176163
177164 fn compute_max_density ( & self , moles : & Array1 < f64 > ) -> f64 {
@@ -280,9 +267,9 @@ mod tests {
280267 let propane = mixture[ 0 ] . clone ( ) ;
281268 let tc = propane. model_record . clone ( ) . unwrap ( ) . tc ;
282269 let pc = propane. model_record . clone ( ) . unwrap ( ) . pc ;
283- let parameters = PengRobinsonParameters :: from_records ( vec ! [ propane . clone ( ) ] , None )
284- . expect ( "Error reading parameters!" ) ;
285- let pr = Rc :: new ( PengRobinson :: new ( parameters) ) ;
270+ let parameters =
271+ PengRobinsonParameters :: from_records ( vec ! [ propane . clone ( ) ] , Array2 :: zeros ( ( 1 , 1 ) ) ) ;
272+ let pr = Rc :: new ( PengRobinson :: new ( Rc :: new ( parameters) ) ) ;
286273 let cp = State :: critical_point ( & pr, None , None , VLEOptions :: default ( ) ) ?;
287274 println ! ( "{} {}" , cp. temperature, cp. pressure( Contributions :: Total ) ) ;
288275 assert_relative_eq ! ( cp. temperature, tc * KELVIN , max_relative = 1e-4 ) ;
0 commit comments