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
//! IF97: The High-speed Package of IAPWS-IF97
//!    (p,t) (p,h) (p,s) (h,s) (p,x) (t,x)
#![allow(warnings)]
//#![warn(missing_docs)]
mod algo;
mod common;
mod r1;
mod r2;
mod r3;
mod r4;
mod r5;

pub use common::propertry_id::*;
use common::*;
use r1::*;
use r2::*;
use r3::*;
use r4::*;
use r5::*;

/// pt_thermal(p,t,o_id) - the propertry of o_id (thermodynamic)
fn pt_thermal(p: f64, t: f64, o_id: i32) -> f64 {
    let sub_region: i32 = pT_sub_region(p, t + K);
    if o_id == OR {
        return sub_region as f64;
    };
    if o_id == OX {
        match sub_region {
            1 => return 0.0,
            2 | 3 | 5 => return 1.0,
            _ => return INVALID_VALUE as f64,
        }
    }
    match sub_region {
        1 => pt_reg1(p, t, o_id),
        2 => pt_reg2(p, t, o_id),
        3 => pt_reg3(p, t, o_id),
        4 => {
            if (t + 273.15) == TC_WATER && p == PC_WATER {
                return td_reg3(t, DC_WATER, o_id);
            } else {
                return sub_region as f64;
            }
        }
        5 => pt_reg5(p, t, o_id),
        _ => sub_region as f64,
    }
}

///  pt(p,t,o_id) - the propertry of o_id (thermodynamic,transport,etc)
/// 
/// # Examples
///
/// ```
/// use if97::*;
///
/// let p:f64 = 3.0;
/// let t:f64= 300.0-273.15;
/// let h=pt(p,t,OH);
/// let s=pt(p,t,OS);
/// let v=pt(p,t,OV);
/// println!("p={p:.6} t={t:.6} h={h:.6} s={s:.6} v={v:.6}");    
/// ```
///
pub fn pt(p: f64, t: f64, o_id: i32) -> f64 {
    match o_id {
        OP => return p,
        OT => return t,
        OV | OD | OH | OS | OU | OCP | OCV | OW | OR | OX => pt_thermal(p, t, o_id),
        OST => return surface_tension(t + K),
        ODV | OKV | OTC | OSDC => {
            let d: f64 = pt_thermal(p, t, OD);
            let mut value: f64 = 0.0;
            if o_id == ODV || o_id == OKV {
                value = viscosity(d, t + 273.15);
                if o_id == OKV {
                    value /= d; // Kinematic viscosity=Dynamic viscosity/density
                }
            } else {
                if o_id == OTC {
                    value = thcond(d, t + 273.15)
                } else {
                    if o_id == OSDC {
                        value = static_dielectric(d, t + 273.15);
                    };
                }
            };
            value
        }
        OPR | OTD => {
            let d: f64 = pt_thermal(p, t, OD);
            let cp: f64 = pt_thermal(p, t, OCP);
            let tc: f64 = thcond(d, t + 273.15);
            let mut value: f64 = 0.0;
            if o_id == OTD {
                value = thermal_diffusivity(tc, cp, d);
            } else if o_id == OPR {
                let dv: f64 = viscosity(d, t + 273.15);
                value = prandtl_number(dv, cp, tc);
            }
            value
        }
        _ => INVALID_VALUE as f64,
    }
}

/// ph_thermal(p,h,o_id) - the propertry of o_id(thermodynamic)
fn ph_thermal(p: f64, h: f64, o_id: i32) -> f64 {
    let sub_region: i32 = ph_sub_region(p, h);
    if o_id == OR {
        return sub_region as f64;
    };
    if o_id == OX {
        match sub_region {
            1 => return 0.0,
            2 | 3 | 5 => return 1.0,
            4 => return ph_reg4(p, h, OX),
            _ => return INVALID_VALUE as f64,
        }
    }
    match sub_region {
        1 => ph_reg1(p, h, o_id),
        2 => ph_reg2(p, h, o_id),
        3 => ph_reg3(p, h, o_id),
        4 => ph_reg4(p, h, o_id),
        5 => ph_reg5(p, h, o_id),
        _ => sub_region as f64,
    }
}

///  ph(p,h,o_id) -  the propertry of o_id (thermodynamic,transport,etc)
///
/// # Examples
///
/// ```
/// use if97::*;
///
/// let p:f64 = 3.0;
/// let h:f64= 0.115331273e+3;
/// let t=ph(p,h,OT);
/// println!("p={p:.6} h={h:.6} t={t:.6}");    
/// ```
///
pub fn ph(p: f64, h: f64, o_id: i32) -> f64 {
    match o_id {
        OP => return p,
        OH => return h,
        OT | OV | OD | OS | OU | OCP | OCV | OW | OR | OX => ph_thermal(p, h, o_id),
        OST => {
            let t: f64 = ph_thermal(p, h, OT);
            return surface_tension(t + K);
        }
        ODV | OKV | OTC | OSDC => {
            let d: f64 = ph_thermal(p, h, OD);
            let t: f64 = ph_thermal(p, h, OT);
            let mut value: f64 = 0.0;
            if o_id == ODV || o_id == OKV {
                value = viscosity(d, t + 273.15);
                if o_id == OKV {
                    value /= d; // Kinematic viscosity=Dynamic viscosity/density
                }
            } else {
                if o_id == OTC {
                    value = thcond(d, t + 273.15)
                } else {
                    if o_id == OSDC {
                        value = static_dielectric(d, t + 273.15);
                    };
                }
            };
            value
        }
        OPR | OTD => {
            let d: f64 = ph_thermal(p, h, OD);
            let t: f64 = ph_thermal(p, h, OT);

            let cp: f64 = ph_thermal(p, h, OCP);
            let tc: f64 = thcond(d, t + 273.15);

            let mut value: f64 = 0.0;
            if o_id == OTD {
                value = thermal_diffusivity(tc, cp, d);
            } else if o_id == OPR {
                let dv: f64 = viscosity(d, t + 273.15);
                value = prandtl_number(dv, cp, tc);
            }
            value
        }
        _ => INVALID_VALUE as f64,
    }
}

/// ps_thermal(p,s,o_id) - the propertry of o_id(thermodynamic)
fn ps_thermal(p: f64, s: f64, o_id: i32) -> f64 {
    let sub_region: i32 = ps_sub_region(p, s);
    if o_id == OR {
        return sub_region as f64;
    };
    if o_id == OX {
        match sub_region {
            1 => return 0.0,
            2 | 3 | 5 => return 1.0,
            4 => return ps_reg4(p, s, OX),
            _ => return INVALID_VALUE as f64,
        }
    }
    match sub_region {
        1 => ps_reg1(p, s, o_id),
        2 => ps_reg2(p, s, o_id),
        3 => ps_reg3(p, s, o_id),
        4 => ps_reg4(p, s, o_id),
        5 => ps_reg5(p, s, o_id),
        _ => sub_region as f64,
    }
}

///  ps(p,s,o_id) - the propertry of o_id (thermodynamic,transport,etc)
///
/// # Examples
///
///```
/// use if97::*;
///
/// let p:f64= 3.0;
/// let s:f64= 0.392294792;
/// let t=ps(p,s,OT);
/// println!("p={p:.6} s={s:.6} t={t:.6}");    
/// ```
pub fn ps(p: f64, s: f64, o_id: i32) -> f64 {
    match o_id {
        OP => return p,
        OS => return s,
        OT | OV | OD | OH | OU | OCP | OCV | OW | OR | OX => ps_thermal(p, s, o_id),
        OST => {
            let t: f64 = ps_thermal(p, s, OT);
            return surface_tension(t + K);
        }
        ODV | OKV | OTC | OSDC => {
            let d: f64 = ps_thermal(p, s, OD);
            let t: f64 = ps_thermal(p, s, OT);
            let mut value: f64 = 0.0;
            if o_id == ODV || o_id == OKV {
                value = viscosity(d, t + 273.15);
                if o_id == OKV {
                    value /= d; // Kinematic viscosity=Dynamic viscosity/density
                }
            } else {
                if o_id == OTC {
                    value = thcond(d, t + 273.15)
                } else {
                    if o_id == OSDC {
                        value = static_dielectric(d, t + 273.15);
                    };
                };
            };
            value
        }
        OPR | OTD => {
            let d: f64 = ps_thermal(p, s, OD);
            let t: f64 = ps_thermal(p, s, OT);

            let cp: f64 = ps_thermal(p, s, OCP);
            let tc: f64 = thcond(d, t + 273.15);
            let mut value: f64 = 0.0;
            if o_id == OTD {
                value = thermal_diffusivity(tc, cp, d);
            } else if o_id == OPR {
                let dv: f64 = viscosity(d, t + 273.15);
                value = prandtl_number(dv, cp, tc);
            }
            value
        }
        _ => INVALID_VALUE as f64,
    }
}

/// hs_thermal(h,s,o_id) - the propertry of o_id(thermodynamic)
fn hs_thermal(h: f64, s: f64, o_id: i32) -> f64 {
    let sub_region: i32 = hs_sub_region(h, s);
    if o_id == OR {
        return sub_region as f64;
    };
    if o_id == OX {
        match sub_region {
            1 => return 0.0,
            2 | 3 | 5 => return 1.0,
            4 => return hs_reg4(h, s, OX),
            _ => return INVALID_VALUE as f64,
        }
    }
    match sub_region {
        1 => hs_reg1(h, s, o_id),
        2 => hs_reg2(h, s, o_id),
        3 => hs_reg3(h, s, o_id),
        4 => hs_reg4(h, s, o_id),
        5 => hs_reg5(h, s, o_id),
        _ => sub_region as f64,
    }
}

/// hs(h,s,o_id) - the propertry of o_id (thermodynamic,transport,etc)
///
/// # Examples
///
///```
/// use if97::*;
///
/// let h:f64= 0.115331273e+3;;
/// let s:f64= 0.392294792;
/// let p=hs(h,s,OP);
/// let t=hs(h,s,OT);
/// println!("h={h:.6} s={s:.6} p={p:.6} t={t:.6}");
/// ```
///   
pub fn hs(h: f64, s: f64, o_id: i32) -> f64 {
    match o_id {
        OH => return h,
        OS => return s,
        OP | OT | OV | OD | OU | OCP | OCV | OW | OR | OX => hs_thermal(h, s, o_id),
        OST => {
            let t: f64 = hs_thermal(h, s, OT);
            return surface_tension(t + K);
        }
        ODV | OKV | OTC | OSDC => {
            let d: f64 = hs_thermal(h, s, OD);
            let t: f64 = hs_thermal(h, s, OT);
            let mut value: f64 = 0.0;
            if o_id == ODV || o_id == OKV {
                value = viscosity(d, t + 273.15);
                if o_id == OKV {
                    value /= d; // Kinematic viscosity=Dynamic viscosity/density
                }
            } else {
                if o_id == OTC {
                    value = thcond(d, t + 273.15)
                } else {
                    if o_id == OSDC {
                        value = static_dielectric(d, t + 273.15);
                    };
                };
            };
            value
        }
        OPR | OTD => {
            let d: f64 = hs_thermal(h, s, OD);
            let t: f64 = hs_thermal(h, s, OT);

            let cp: f64 = hs_thermal(h, s, OCP);
            let tc: f64 = thcond(d, t + 273.15);
            let mut value: f64 = 0.0;
            if o_id == OTD {
                value = thermal_diffusivity(tc, cp, d);
            } else if o_id == OPR {
                let dv: f64 = viscosity(d, t + 273.15);
                value = prandtl_number(dv, cp, tc);
            }
            value
        }

        _ => INVALID_VALUE as f64,
    }
}

///  px(p,x,o_id) - the propertry of o_id (thermodynamic)
///
/// # Examples
///
///```
///  use if97::*;
/// 
///  let p: f64 = 0.1;
///  let x: f64 = 0.0; // x= 0.0 saturation water ,x=1.0 saturation steam
///  let t: f64 = px(p, x, OT);
///  let h: f64 = px(p, x, OH);
///  println!("px: p={p:.6} x={x:.6} t={t:.6} h={h:.6}");
///```
/// 
pub fn px(p: f64, x: f64, o_id: i32) -> f64 {
    if p > P_MAX4 || p < P_MIN4 || x > 1.0 || x < 0.0 {
        return INVALID_VALUE as f64;
    }
    px_reg4(p, x, o_id)
}

///  tx(t,x,o_id) - the propertry of o_id (thermodynamic)
///
/// # Examples
///
///```
///  use if97::*;
/// 
///  let t: f64 =372.755919-273.15;
///  let x: f64 = 0.0; // x= 0.0 saturation water ,x=1.0 saturation steam
///  let p: f64 = tx(t, x, OP);
///  let h: f64 = tx(t, x, OH);
///  println!("tx: p={p:.6} x={x:.6} t={t:.6} h={h:.6}");
/// `
pub fn tx(t: f64, x: f64, o_id: i32) -> f64 {
    let T:f64=t+273.15;
    if T > T_MAX4 || T< T_MIN4 || x > 1.0 || x < 0.0 {
        return INVALID_VALUE as f64;
    }
    Tx_reg4(T, x, o_id)
}