src/main.rs

changeset 20
bd6fd49146ce
parent 19
bc7f268b324a
child 24
8b4b014277fa
equal deleted inserted replaced
19:bc7f268b324a 20:bd6fd49146ce
13 mod cube; 13 mod cube;
14 mod dist; 14 mod dist;
15 mod zero; 15 mod zero;
16 mod scaled; 16 mod scaled;
17 17
18 use serde::Serialize; 18 use serde::{Serialize, Deserialize};
19 use alg_tools::logger::Logger; 19 use alg_tools::logger::Logger;
20 use alg_tools::tabledump::{TableDump, write_csv}; 20 use alg_tools::tabledump::{TableDump, write_csv};
21 use alg_tools::error::DynError; 21 use alg_tools::error::DynError;
22 use alg_tools::lingrid::LinSpace; 22 use alg_tools::lingrid::LinSpace;
23 use alg_tools::loc::Loc; 23 use alg_tools::loc::Loc;
38 /// Program entry point 38 /// Program entry point
39 fn main() { 39 fn main() {
40 simple_cube_test().unwrap() 40 simple_cube_test().unwrap()
41 } 41 }
42 42
43 /// Helper structure for saving the log into a CSV file 43 /// Helper structure for saving a point on a cube into a CSV file
44 #[derive(Serialize)] 44 #[derive(Serialize,Deserialize,Debug)]
45 struct CSVLog { 45 struct CSVPoint {
46 iter : usize,
47 value : f64,
48 face : Face, 46 face : Face,
49 x : f64, 47 x : f64,
50 y : f64, 48 y : f64,
51 z : f64 49 z : f64
52 } 50 }
51
52 impl From<&OnCube> for CSVPoint {
53 fn from(point : &OnCube) -> Self {
54 let Loc([x,y,z]) = point.embedded_coords();
55 let face = point.face();
56 CSVPoint { face, x, y, z }
57 }
58 }
59
60 /// Helper structure for saving the log into a CSV file
61 #[derive(Serialize,Deserialize,Debug)]
62 struct CSVLog {
63 iter : usize,
64 value : f64,
65 // serde is junk
66 //#[serde(flatten)]
67 //point : CSVPoint
68 face : Face,
69 x : f64,
70 y : f64,
71 z : f64
72 }
73
53 74
54 /// Location for saving results 75 /// Location for saving results
55 static PREFIX : &str = "res"; 76 static PREFIX : &str = "res";
56 77
57 /// A simple test on the cube 78 /// A simple test on the cube
59 80
60 let points = [ 81 let points = [
61 //OnCube::new(F1, Loc([0.5, 0.5])), 82 //OnCube::new(F1, Loc([0.5, 0.5])),
62 //OnCube::new(F2, Loc([0.5, 0.5])), 83 //OnCube::new(F2, Loc([0.5, 0.5])),
63 //OnCube::new(F4, Loc([0.1, 0.1])), 84 //OnCube::new(F4, Loc([0.1, 0.1])),
64 OnCube::new(F1, Loc([0.5, 0.5])), 85 OnCube::new(F1, Loc([0.5, 0.7])),
65 OnCube::new(F3, Loc([0.5, 0.5])), 86 OnCube::new(F2, Loc([0.3, 0.5])),
66 OnCube::new(F2, Loc([0.5, 0.5])), 87 OnCube::new(F4, Loc([0.9, 0.9])),
88 OnCube::new(F6, Loc([0.4, 0.3])),
89 OnCube::new(F4, Loc([0.3, 0.7])),
90 OnCube::new(F3, Loc([0.3, 0.7])),
67 ]; 91 ];
92
93 let origin = OnCube::new(F4, Loc([0.5, 0.5]));
94
95 write_points(format!("{PREFIX}/data"), points.iter())?;
96 write_points(format!("{PREFIX}/origin"), std::iter::once(&origin))?;
68 97
69 //let x = points[0].clone(); 98 //let x = points[0].clone();
70 // OnCube::new(F3, Loc([0.5, 0.5])); goes to opposite side 99 // OnCube::new(F3, Loc([0.5, 0.5])); goes to opposite side
71 let x = OnCube::new(F3, Loc([0.5, 0.4])); 100 let x = OnCube::new(F3, Loc([0.1, 0.7]));
72 let f = Sum::new(points.into_iter().map(DistToSquaredDiv2)); 101 let f = Sum::new(points.into_iter().map(DistToSquaredDiv2));
73 //let g = ZeroFn::new(); 102 //let g = ZeroFn::new();
74 let g = Scaled::new(1.0, DistTo(OnCube::new(F4, Loc([0.5, 0.5])))); 103 let g = Scaled::new(0.5, DistTo(origin));
75 let τ = 0.1; 104 let τ = 0.05;
76 105
77 let mut logger = Logger::new(); 106 let mut logger = Logger::new();
78 let logmap = |iter, IterInfo { value, point } : IterInfo<OnCube>| { 107 let logmap = |iter, IterInfo { value, point } : IterInfo<OnCube>| {
79 let Loc([x,y,z]) = point.embedded_coords(); 108 let CSVPoint {x , y, z, face} = CSVPoint::from(&point);
80 let face = point.face(); 109 CSVLog {
81 CSVLog { iter, value, face, x, y, z } 110 iter, value, //point : CSVPoint::from(&point)
111 x, y, z, face
112 }
82 }; 113 };
83 let iter = AlgIteratorOptions{ 114 let iter = AlgIteratorOptions{
84 max_iter : 100, 115 max_iter : 100,
85 verbose_iter : Verbose::Every(1), 116 verbose_iter : Verbose::Every(1),
86 .. Default::default() 117 .. Default::default()
93 std::fs::create_dir_all(PREFIX)?; 124 std::fs::create_dir_all(PREFIX)?;
94 125
95 logger.write_csv(format!("{PREFIX}/log.txt"))?; 126 logger.write_csv(format!("{PREFIX}/log.txt"))?;
96 127
97 for face in Face::all() { 128 for face in Face::all() {
98 write_face_csv(format!("{PREFIX}/{face}"), face, 64, |x| f.apply(x) + g.apply(x))?; 129 write_face_csv(format!("{PREFIX}/{face}"), face, 32, |x| f.apply(x) + g.apply(x))?;
99 write_face_img(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?; 130 write_face_img(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?;
100 } 131 }
101 132
102 Ok(()) 133 Ok(())
103 } 134 }
146 177
147 write_csv(data, format!("{filename}.csv"))?; 178 write_csv(data, format!("{filename}.csv"))?;
148 179
149 Ok(()) 180 Ok(())
150 } 181 }
182
183 /// Writes a list of points on a [`OnCube`] into a CSV file
184 fn write_points<'a, I : Iterator<Item=&'a OnCube>>(filename : String, list : I) -> DynError {
185
186 write_csv(list.map(CSVPoint::from), format!("{filename}.csv"))?;
187 Ok(())
188 }
189

mercurial