chore: sync project state and current artifacts
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
//! typical thermodynamic ranges (P: 1e3–1e7 Pa, T: 200–600 K).
|
||||
|
||||
use crate::mixture::Mixture;
|
||||
use crate::types::{FluidId, Property, FluidState};
|
||||
use crate::types::{FluidId, FluidState, Property};
|
||||
use lru::LruCache;
|
||||
use std::cell::RefCell;
|
||||
use std::hash::{Hash, Hasher};
|
||||
@@ -27,7 +27,13 @@ const DEFAULT_CAP_NONZERO: NonZeroUsize = NonZeroUsize::new(DEFAULT_CACHE_CAPACI
|
||||
/// (v * 1e9).round() as i64 for Hash-compatible key.
|
||||
#[inline]
|
||||
fn quantize(v: f64) -> i64 {
|
||||
if v.is_nan() || v.is_infinite() {
|
||||
if v.is_nan() {
|
||||
#[cfg(debug_assertions)]
|
||||
eprintln!("[WARN] quantize: NaN value encountered, mapping to 0");
|
||||
0
|
||||
} else if v.is_infinite() {
|
||||
#[cfg(debug_assertions)]
|
||||
eprintln!("[WARN] quantize: Infinite value encountered, mapping to 0");
|
||||
0
|
||||
} else {
|
||||
(v * 1e9).round() as i64
|
||||
@@ -81,9 +87,7 @@ impl CacheKey {
|
||||
pub fn new(backend_id: usize, fluid: &FluidId, property: Property, state: &FluidState) -> Self {
|
||||
let (p, second, variant, mixture_hash) = match state {
|
||||
FluidState::PressureTemperature(p, t) => (p.to_pascals(), t.to_kelvin(), 0u8, None),
|
||||
FluidState::PressureEnthalpy(p, h) => {
|
||||
(p.to_pascals(), h.to_joules_per_kg(), 1u8, None)
|
||||
}
|
||||
FluidState::PressureEnthalpy(p, h) => (p.to_pascals(), h.to_joules_per_kg(), 1u8, None),
|
||||
FluidState::PressureEntropy(p, s) => {
|
||||
(p.to_pascals(), s.to_joules_per_kg_kelvin(), 2u8, None)
|
||||
}
|
||||
@@ -133,8 +137,9 @@ pub fn cache_get(
|
||||
) -> Option<f64> {
|
||||
let key = CacheKey::new(backend_id, fluid, property, state);
|
||||
CACHE.with(|c| {
|
||||
let mut cache = c.borrow_mut();
|
||||
cache.get(&key).copied()
|
||||
c.try_borrow_mut()
|
||||
.ok()
|
||||
.and_then(|mut cache| cache.get(&key).copied())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -148,24 +153,30 @@ pub fn cache_insert(
|
||||
) {
|
||||
let key = CacheKey::new(backend_id, fluid, property, state);
|
||||
CACHE.with(|c| {
|
||||
let mut cache = c.borrow_mut();
|
||||
cache.put(key, value);
|
||||
if let Ok(mut cache) = c.try_borrow_mut() {
|
||||
cache.put(key, value);
|
||||
}
|
||||
// Silently ignore if borrow fails (cache miss is acceptable)
|
||||
});
|
||||
}
|
||||
|
||||
/// Clear the thread-local cache (e.g. at solver iteration boundaries).
|
||||
pub fn cache_clear() {
|
||||
CACHE.with(|c| {
|
||||
let mut cache = c.borrow_mut();
|
||||
cache.clear();
|
||||
if let Ok(mut cache) = c.try_borrow_mut() {
|
||||
cache.clear();
|
||||
}
|
||||
// Silently ignore if borrow fails
|
||||
});
|
||||
}
|
||||
|
||||
/// Resize the thread-local cache capacity.
|
||||
pub fn cache_resize(capacity: NonZeroUsize) {
|
||||
CACHE.with(|c| {
|
||||
let mut cache = c.borrow_mut();
|
||||
cache.resize(capacity);
|
||||
if let Ok(mut cache) = c.try_borrow_mut() {
|
||||
cache.resize(capacity);
|
||||
}
|
||||
// Silently ignore if borrow fails
|
||||
});
|
||||
}
|
||||
|
||||
@@ -217,8 +228,14 @@ mod tests {
|
||||
cache_insert(0, &fluid, Property::Density, &state3, 1200.0);
|
||||
|
||||
assert!(cache_get(0, &fluid, Property::Density, &state1).is_none());
|
||||
assert_eq!(cache_get(0, &fluid, Property::Density, &state2), Some(1100.0));
|
||||
assert_eq!(cache_get(0, &fluid, Property::Density, &state3), Some(1200.0));
|
||||
assert_eq!(
|
||||
cache_get(0, &fluid, Property::Density, &state2),
|
||||
Some(1100.0)
|
||||
);
|
||||
assert_eq!(
|
||||
cache_get(0, &fluid, Property::Density, &state3),
|
||||
Some(1200.0)
|
||||
);
|
||||
|
||||
cache_resize(NonZeroUsize::new(DEFAULT_CACHE_CAPACITY).expect("capacity is non-zero"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user