--- a/src/main.rs Mon Oct 21 23:07:01 2024 -0500 +++ b/src/main.rs Tue Oct 22 08:27:45 2024 -0500 @@ -5,9 +5,13 @@ #![allow(mixed_script_confusables)] #![allow(confusable_idents)] +use serde::Serialize; use dist::DistToSquaredDiv2; -use fb::forward_backward; +use fb::{forward_backward, IterInfo}; use manifold::EmbeddedManifoldPoint; +use alg_tools::logger::Logger; +use alg_tools::tabledump::TableDump; +use alg_tools::error::DynError; use cube::*; use image::{ ImageFormat, @@ -22,15 +26,29 @@ mod zero; fn main() { - simple_test() + simple_cube_test().unwrap() } -fn simple_test() { +/// Helper structure for saving the log into a CSV file +#[derive(Serialize)] +struct CSVLog { + iter : usize, + value : f64, + face : Face, + x : f64, + y : f64, + z : f64 +} + +static PREFIX : &str = "res"; + +/// A simple test on the cube +fn simple_cube_test() -> DynError { use alg_tools::loc::Loc; use Face::*; use zero::ZeroFn; use alg_tools::mapping::{Sum, Apply}; - use alg_tools::iterate::{AlgIteratorOptions, Verbose}; + use alg_tools::iterate::{AlgIteratorOptions, AlgIteratorFactory, Verbose}; let points = [ //OnCube::new(F1, Loc([0.5, 0.5])), @@ -47,21 +65,37 @@ let f = Sum::new(points.into_iter().map(DistToSquaredDiv2)); let g = ZeroFn::new(); let τ = 0.1; + + let mut logger = Logger::new(); + let logmap = |iter, IterInfo { value, point } : IterInfo<OnCube>| { + let Loc([x,y,z]) = point.embedded_coords(); + let face = point.face(); + CSVLog { iter, value, face, x, y, z } + }; let iter = AlgIteratorOptions{ max_iter : 100, verbose_iter : Verbose::Every(1), .. Default::default() - }; + }.mapped(logmap) + .into_log(&mut logger); let x̂ = forward_backward(&f, &g, x, τ, iter); println!("result = {}\n{:?}", x̂.embedded_coords(), &x̂); + std::fs::create_dir_all(PREFIX)?; + + logger.write_csv(format!("{PREFIX}/log.txt"))?; + for face in Face::all() { - write_face(format!("{face}"), face, 128, |x| f.apply(x) + g.apply(x)) + write_face(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?; } + + Ok(()) } -fn write_face(filename : String, face : Face, n : usize, mut f : impl FnMut(&OnCube) -> f64) { +/// Writes the values of `f` on `face` of a [`OnCube`] into a PNG file +/// with resolution `n × n`. +fn write_face(filename : String, face : Face, n : usize, mut f : impl FnMut(&OnCube) -> f64) -> DynError { use alg_tools::lingrid::LinSpace; use alg_tools::loc::Loc; use alg_tools::types::*; @@ -84,6 +118,7 @@ *p = Rgb(rgb.map(|v| (v*(u8::RANGE_MAX as f64)) as u8)) }); - img.save_with_format(format!("{filename}.png"), ImageFormat::Png) - .expect("Image save error"); + img.save_with_format(format!("{filename}.png"), ImageFormat::Png)?; + + Ok(()) }