diff --git a/src/expr/linkplan.rs b/src/expr/linkplan.rs index 9a4739b..4d379a2 100644 --- a/src/expr/linkplan.rs +++ b/src/expr/linkplan.rs @@ -15,9 +15,9 @@ struct PreHasseNode { /* fullfillment of links is sort-of complicated, because the tree of possiblities is really large - * start by splitting links according to their roots (the variable of which derivative of such is taken) - * split the derivation-vectors into levels (sum of their components) - */ + * start by splitting links according to their roots (the variable of which derivative of such is taken) + * split the derivation-vectors into levels (sum of their components) +*/ fn sum(drvs: &[u8]) -> usize { drvs.iter().copied().map(usize::from).sum::() @@ -47,15 +47,24 @@ fn calculate_votes(mut que: Vec>) -> Vec, VoteEntry // split que into levels let mut levels = core::iter::repeat_with(BTreeMap::new) - .take(que.iter().map(|i| sum(&i[..])).max().map(|i| i.checked_add(1).unwrap()).unwrap_or(0)) + .take( + que.iter() + .map(|i| sum(&i[..])) + .max() + .map(|i| i.checked_add(1).unwrap()) + .unwrap_or(0), + ) .collect::, VoteEntry>>>(); for i in core::mem::take(&mut que) { - levels[sum(&i[..])].insert(i, VoteEntry { - votes: 1, - best_parent: 0, - is_used: true, - }); + levels[sum(&i[..])].insert( + i, + VoteEntry { + votes: 1, + best_parent: 0, + is_used: true, + }, + ); } // process levels (start from largest) @@ -90,12 +99,17 @@ fn calculate_votes(mut que: Vec>) -> Vec, VoteEntry } else { x } - }, + } }); } let pbs = pbs.unwrap(); let pbs_index = { - let mut pbsi = j.iter().zip(pbs.iter()).map(|(i, p)| i - p).enumerate().filter(|(_, i)| *i > 0); + let mut pbsi = j + .iter() + .zip(pbs.iter()) + .map(|(i, p)| i - p) + .enumerate() + .filter(|(_, i)| *i > 0); let pbs_index = pbsi.next().unwrap(); assert_eq!(pbsi.next(), None); pbs_index diff --git a/src/expr/mod.rs b/src/expr/mod.rs index 7397b19..cf7619b 100644 --- a/src/expr/mod.rs +++ b/src/expr/mod.rs @@ -33,12 +33,14 @@ pub enum Expr { impl Expr { fn extract_links_intern(&self, out: &mut Vec) { match self { - Expr::Const(_) => {}, + Expr::Const(_) => {} Expr::Link(l) => out.push(l.clone()), Expr::Sum(xs) => xs.iter().for_each(|i| i.extract_links_intern(out)), Expr::Prod(xs) => xs.iter().for_each(|i| i.extract_links_intern(out)), - Expr::Sine(x) | Expr::Cosine(x) | Expr::Tangens(x) | Expr::ArcTan(x) => x.extract_links_intern(out), + Expr::Sine(x) | Expr::Cosine(x) | Expr::Tangens(x) | Expr::ArcTan(x) => { + x.extract_links_intern(out) + } } } @@ -70,8 +72,12 @@ pub struct System { impl System { pub fn extract_links(&self) -> Vec { let mut ret = Vec::new(); - self.vs.iter().for_each(|i| i.extract_links_intern(&mut ret)); - self.mons.iter().for_each(|i| i.1.extract_links_intern(&mut ret)); + self.vs + .iter() + .for_each(|i| i.extract_links_intern(&mut ret)); + self.mons + .iter() + .for_each(|i| i.1.extract_links_intern(&mut ret)); ret.sort_unstable(); ret.dedup(); ret @@ -79,7 +85,9 @@ impl System { // this is invoked for each time step times points in space for which the links can be computed pub fn interp_vs(&self, link_data: &BTreeMap, out: &mut [f64]) { - self.vs.iter().zip(out.iter_mut()).for_each(|(x, y)| { *y = x.interp(link_data); }); + self.vs.iter().zip(out.iter_mut()).for_each(|(x, y)| { + *y = x.interp(link_data); + }); } // this is invoked once per time step @@ -97,7 +105,9 @@ impl System { } pub fn interp_mons_data(&self, link_data: &BTreeMap, out: &mut [f64]) { - self.mons.iter().zip(out.iter_mut()).for_each(|(x, y)| { *y = x.1.interp(link_data); }); + self.mons.iter().zip(out.iter_mut()).for_each(|(x, y)| { + *y = x.1.interp(link_data); + }); } } @@ -186,7 +196,10 @@ fn parse_expr(slb: &mut StrLexerBase<'_>) -> Result(slb: &mut StrLexerBase<'_>) -> Result Ok(Expr::Cosine(Box::new(arg.clone()))), ("tan", [arg]) => Ok(Expr::Tangens(Box::new(arg.clone()))), ("atan", [arg]) => Ok(Expr::ArcTan(Box::new(arg.clone()))), - ("sin" | "cos" | "tan" | "atan", _) => Err(ParseError::InvalidArgCount(ident.to_string(), args.len())), + ("sin" | "cos" | "tan" | "atan", _) => { + Err(ParseError::InvalidArgCount(ident.to_string(), args.len())) + } _ => Err(ParseError::UnknownFun(ident.to_string())), } } -pub fn parse_expr_final(slb: &mut StrLexerBase) -> Result, ParseError> { +pub fn parse_expr_final( + slb: &mut StrLexerBase, +) -> Result, ParseError> { let x = parse_expr(slb)?; if !slb.inp.is_empty() { return Err(ParseError::InvalidTrail); @@ -318,7 +335,7 @@ pub fn parse_system(s: &str) -> Result { [x] => (0, x), [x, y] => { match x.parse::() { - Ok(xid) if xid == ret.vs.len() => {}, + Ok(xid) if xid == ret.vs.len() => {} _ => return Err(((lnr, 0), ParseError::InvalidStmtId)), } (x.len() + 1, y) @@ -337,7 +354,10 @@ pub fn parse_system(s: &str) -> Result { if parts.len() < 2 { return Err(((lnr, 0), ParseError::InvalidLineFormat)); } - let mut slb = StrLexerBase { inp: parts[0], offset: 0 }; + let mut slb = StrLexerBase { + inp: parts[0], + offset: 0, + }; let valxp = match parse_expr_final::(&mut slb) { Ok(x) => x, Err(e) => return Err(((lnr, slb.offset), e)), @@ -367,17 +387,25 @@ mod tests { #[test] fn example0_fail() { - assert_eq!(parse_system(" + assert_eq!( + parse_system( + " %vs # u_t + 0.11 u_x = 0 0; -0.11 0_0_ - "), Err(((3, 8), ParseError::InvalidTrail))); + " + ), + Err(((3, 8), ParseError::InvalidTrail)) + ); } #[test] fn example0() { - println!("{:?}", parse_system(" + println!( + "{:?}", + parse_system( + " %vs # u_t + 0.11 u_x = 0 0; prod(-0.11, 0_1_) @@ -385,6 +413,9 @@ mod tests { %mons # monitor u @ 1 + sin(t) 0_0; sum(1.0, sin(.)) - ").unwrap()); + " + ) + .unwrap() + ); } } diff --git a/src/main.rs b/src/main.rs index 526dc0e..a2e8bee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,12 +42,17 @@ fn main() { // parse system let system = { let system = std::fs::read(&cli.system).expect("unable to read system to render"); - let system = std::str::from_utf8(&system).expect("unable to interpret system description as UTF-8"); + let system = + std::str::from_utf8(&system).expect("unable to interpret system description as UTF-8"); expr::parse_system(system).expect("unable to parse system") }; // parse init function - let init = expr::parse_expr_final::(&mut yz_string_utils::StrLexerBase { inp: &cli.init, offset: 0 }).expect("unable to parse init function"); + let init = expr::parse_expr_final::(&mut yz_string_utils::StrLexerBase { + inp: &cli.init, + offset: 0, + }) + .expect("unable to parse init function"); // construct shape let shape = mesh::grid::Shape(cli.shape); @@ -61,7 +66,12 @@ fn main() { // check monitor consistency with shape for (n, (i, _)) in system.mons[..].iter().enumerate() { if i.len() != shape.0.len() { - panic!("monitor {} has incompatible dimension {} vs {}", n, i.len(), shape.0.len()); + panic!( + "monitor {} has incompatible dimension {} vs {}", + n, + i.len(), + shape.0.len() + ); } } @@ -82,7 +92,7 @@ fn main() { let mut grid = nalgebra::DMatrix::::zeros(shape_amount, system.vs.len()); let mut grid_stage = nalgebra::DMatrix::::zeros(shape_amount, system.vs.len()); - // data = + // data = let mut grid_next = grid.clone(); diff --git a/src/mesh/grid.rs b/src/mesh/grid.rs index 2eab7c4..3c64735 100644 --- a/src/mesh/grid.rs +++ b/src/mesh/grid.rs @@ -18,12 +18,16 @@ struct ForEachStencilData<'a, F> { impl ForEachStencilData<'_, F> { fn fe_st_intern(&mut self, shape: &[u16]) { - let prefix: usize = self.buf.len().checked_sub(shape.len()).expect("buffer too small"); + let prefix: usize = self + .buf + .len() + .checked_sub(shape.len()) + .expect("buffer too small"); // recurse over `this` if let Some(&fi) = shape.first() { let shnext = &shape[1..]; - for i in self.stencil_start .. (fi + 1 - self.stencil_end) { + for i in self.stencil_start..(fi + 1 - self.stencil_end) { self.buf[prefix] = i; self.fe_st_intern(shnext); } @@ -36,12 +40,14 @@ impl ForEachStencilData<'_, F> { impl Shape { pub fn amount(&self) -> Option { assert!(self.0.len() <= usize::from(u16::MAX)); - self.0.iter() - .try_fold(1, |acc: usize, &i| acc.checked_mul(usize::from(i).checked_add(1)?)) + self.0.iter().try_fold(1, |acc: usize, &i| { + acc.checked_mul(usize::from(i).checked_add(1)?) + }) } pub fn decode_pos_with_ret(&self, x: usize, out: &mut [u16]) { - self.0.iter() + self.0 + .iter() .map(|&i| usize::from(i) + 1) .zip(out.iter_mut()) .fold(x, |x: usize, (i, o)| { @@ -58,7 +64,8 @@ impl Shape { } pub fn encode_pos(&self, x: &[u16]) -> Option { - self.0.iter() + self.0 + .iter() .map(|&i| usize::from(i) + 1) .zip(x.iter().map(|&i| usize::from(i))) .rev() @@ -71,7 +78,11 @@ impl Shape { }) } - pub fn for_each_stencil(&self, stencil: core::ops::RangeInclusive, mut f: impl FnMut(&[u16])) { + pub fn for_each_stencil( + &self, + stencil: core::ops::RangeInclusive, + mut f: impl FnMut(&[u16]), + ) { assert!(*stencil.start() <= 0); assert!(*stencil.end() >= 0); assert!(self.0.len() <= usize::from(u16::MAX)); @@ -81,7 +92,8 @@ impl Shape { stencil_end: *stencil.end() as u16, buf: &mut buf[..], f: &mut f, - }.fe_st_intern(&self.0[..]) + } + .fe_st_intern(&self.0[..]) } } diff --git a/src/mesh/mod.rs b/src/mesh/mod.rs index 305ba55..d66e26b 100644 --- a/src/mesh/mod.rs +++ b/src/mesh/mod.rs @@ -4,7 +4,6 @@ * SPDX-License-Identifier: EUPL-1.2 */ /// pdxdrive meshes - pub mod grid; /*