test and fix temperature handling

This commit is contained in:
Christof Petig
2022-05-25 22:36:06 +02:00
parent 59d876a654
commit 92ab095480

View File

@ -40,6 +40,7 @@ fn seed_from_pos(pos: Vec3<i32>) -> [u8; 32] {
] ]
} }
#[derive(Debug)]
struct FireplaceTiming { struct FireplaceTiming {
// all this assumes sunrise at 6am, sunset at 6pm // all this assumes sunrise at 6am, sunset at 6pm
breakfast: f32, // 5am to 7am breakfast: f32, // 5am to 7am
@ -62,6 +63,7 @@ const SMOKE_MAX_TEMP_VALUE: f32 = 1.0;
const SMOKE_TEMP_MULTIPLIER: f32 = 96.0; const SMOKE_TEMP_MULTIPLIER: f32 = 96.0;
const SMOKE_DAILY_VARIATION: f32 = 32.0; const SMOKE_DAILY_VARIATION: f32 = 32.0;
#[derive(Debug)]
struct FireplaceClimate { struct FireplaceClimate {
daily_strength: f32, // can be negative (offset) daily_strength: f32, // can be negative (offset)
day_start: f32, // seconds since breakfast for daily cycle day_start: f32, // seconds since breakfast for daily cycle
@ -100,7 +102,7 @@ fn create_climate(temperature: f32, _humidity: f32) -> FireplaceClimate {
} }
} }
type Increasing = bool; pub type Increasing = bool;
pub fn smoke_at_time( pub fn smoke_at_time(
position: Vec3<i32>, position: Vec3<i32>,
@ -112,7 +114,6 @@ pub fn smoke_at_time(
let timing = create_timing(&mut pseudorandom); let timing = create_timing(&mut pseudorandom);
let climate = create_climate(temperature, humidity); let climate = create_climate(temperature, humidity);
let after_breakfast = time_of_day - timing.breakfast; let after_breakfast = time_of_day - timing.breakfast;
//let after_dinner = time_of_day-timing.dinner;
if after_breakfast < -SMOKE_BREAKFAST_HALF_DURATION { if after_breakfast < -SMOKE_BREAKFAST_HALF_DURATION {
/* night */ /* night */
(0.0, false) (0.0, false)
@ -123,16 +124,16 @@ pub fn smoke_at_time(
* (SMOKE_BREAKFAST_STRENGTH / SMOKE_BREAKFAST_HALF_DURATION), * (SMOKE_BREAKFAST_STRENGTH / SMOKE_BREAKFAST_HALF_DURATION),
true, true,
) )
} else if time_of_day < climate.day_start { } else if after_breakfast < climate.day_start {
/* cooling */ /* cooling */
( (
(SMOKE_BREAKFAST_HALF_DURATION - after_breakfast) (SMOKE_BREAKFAST_HALF_DURATION - after_breakfast)
* (SMOKE_BREAKFAST_STRENGTH / SMOKE_BREAKFAST_HALF_DURATION), * (SMOKE_BREAKFAST_STRENGTH / SMOKE_BREAKFAST_HALF_DURATION),
false, false,
) )
} else if time_of_day < climate.day_end { } else if time_of_day < timing.dinner - climate.day_end {
/* day cycle */ /* day cycle */
let day_phase = ((time_of_day - climate.day_start) / timing.daily_cycle).fract(); let day_phase = ((after_breakfast - climate.day_start) / timing.daily_cycle).fract();
if day_phase < 0.5 { if day_phase < 0.5 {
( (
(climate.daily_strength + day_phase * (2.0 * SMOKE_DAILY_VARIATION)).max(0.0), (climate.daily_strength + day_phase * (2.0 * SMOKE_DAILY_VARIATION)).max(0.0),
@ -148,15 +149,15 @@ pub fn smoke_at_time(
} else if time_of_day < timing.dinner { } else if time_of_day < timing.dinner {
/* cooking dinner */ /* cooking dinner */
( (
(SMOKE_BREAKFAST_HALF_DURATION + time_of_day - timing.dinner) (SMOKE_DINNER_HALF_DURATION + time_of_day - timing.dinner)
* (SMOKE_BREAKFAST_STRENGTH / SMOKE_BREAKFAST_HALF_DURATION), * (SMOKE_DINNER_STRENGTH / SMOKE_DINNER_HALF_DURATION),
true, true,
) )
} else { } else {
/* cooling + night */ /* cooling + night */
( (
(SMOKE_BREAKFAST_HALF_DURATION - time_of_day + timing.dinner).max(0.0) (SMOKE_DINNER_HALF_DURATION - time_of_day + timing.dinner).max(0.0)
* (SMOKE_BREAKFAST_STRENGTH / SMOKE_BREAKFAST_HALF_DURATION), * (SMOKE_DINNER_STRENGTH / SMOKE_DINNER_HALF_DURATION),
false, false,
) )
} }
@ -166,18 +167,41 @@ pub fn smoke_at_time(
mod tests { mod tests {
use super::*; use super::*;
#[test] fn test_conditions(position: Vec3<i32>, temperature: f32, humidity: f32) {
fn test_smoke() { print!("{} T{:.1} ", position, temperature);
let position = Vec3::new(22_i32, 11, 33); let mut pseudorandom = ChaCha8Rng::from_seed(seed_from_pos(position));
let temperature = -0.5; if true {
let humidity = 0.5; let timing = create_timing(&mut pseudorandom);
for i in (0..24 * 4) { let climate = create_climate(temperature, humidity);
let time_of_day = 15.0 * 60.0 * (i as f32); print!(
println!( "B{:.1}+{:.1} D{:.1}-{:.1} C{:.0} S{:.0} ",
"{:.2} {}", timing.breakfast / 3600.0,
time_of_day / (60.0 * 60.0), climate.day_start / 3600.0,
smoke_at_time(position, temperature, humidity, time_of_day) timing.dinner / 3600.0,
climate.day_end / 3600.0,
timing.daily_cycle / 60.0,
climate.daily_strength
); );
} }
for i in 0..24 {
print!(" {}:", i);
for j in 0..6 {
let time_of_day = 60.0 * 60.0 * (i as f32) + 60.0 * 10.0 * (j as f32);
let res = smoke_at_time(position, temperature, humidity, time_of_day);
print!("{:.0}{} ", res.0, if res.1 { "^" } else { "" },);
assert!(res.0>=0.0);
assert!(res.0<=SMOKE_DINNER_STRENGTH);
}
}
println!();
}
#[test]
fn test_smoke() {
test_conditions(Vec3::new(25_i32, 11, 33), -1.0, 0.5);
test_conditions(Vec3::new(22_i32, 11, 33), -0.5, 0.5);
test_conditions(Vec3::new(27_i32, 11, 33), 0.0, 0.5);
test_conditions(Vec3::new(24_i32, 11, 33), 0.5, 0.5);
test_conditions(Vec3::new(26_i32, 11, 33), 1.0, 0.5);
} }
} }