From 50ddf0accecf2ecee22178ac949ee1b04276b606 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Mon, 6 Apr 2020 22:32:36 +0200 Subject: [PATCH] extract extract_boundaries --- sbp/examples/multigrid/bin.rs | 146 +++++++++++++--------------------- 1 file changed, 55 insertions(+), 91 deletions(-) diff --git a/sbp/examples/multigrid/bin.rs b/sbp/examples/multigrid/bin.rs index dd194ba..a065f27 100644 --- a/sbp/examples/multigrid/bin.rs +++ b/sbp/examples/multigrid/bin.rs @@ -155,52 +155,7 @@ impl System { } let fields = &self.fnext; - - let bt = self - .bt - .iter() - .zip(&mut self.eb) - .zip(&self.grids) - .enumerate() - .map(|(i, ((bt, eb), grid))| euler::BoundaryTerms { - north: match bt.north { - euler::BoundaryCharacteristic::This => fields[i].south(), - euler::BoundaryCharacteristic::Grid(g) => fields[g].south(), - euler::BoundaryCharacteristic::Vortex(v) => { - let field = eb.n.as_mut().unwrap(); - vortexify(field.view_mut(), grid.north(), v, time); - field.view() - } - }, - south: match bt.south { - euler::BoundaryCharacteristic::This => fields[i].north(), - euler::BoundaryCharacteristic::Grid(g) => fields[g].north(), - euler::BoundaryCharacteristic::Vortex(v) => { - let field = eb.s.as_mut().unwrap(); - vortexify(field.view_mut(), grid.south(), v, time); - field.view() - } - }, - west: match bt.west { - euler::BoundaryCharacteristic::This => fields[i].east(), - euler::BoundaryCharacteristic::Grid(g) => fields[g].east(), - euler::BoundaryCharacteristic::Vortex(v) => { - let field = eb.w.as_mut().unwrap(); - vortexify(field.view_mut(), grid.west(), v, time); - field.view() - } - }, - east: match bt.east { - euler::BoundaryCharacteristic::This => fields[i].west(), - euler::BoundaryCharacteristic::Grid(g) => fields[g].west(), - euler::BoundaryCharacteristic::Vortex(v) => { - let field = eb.e.as_mut().unwrap(); - vortexify(field.view_mut(), grid.east(), v, time); - field.view() - } - }, - }) - .collect::>(); + let bt = extract_boundaries(&fields, &mut self.bt, &mut self.eb, &self.grids, time); for ((((prev, fut), metrics), wb), bt) in fields .iter() @@ -295,51 +250,7 @@ impl System { s.scope(|s| { let fields = &self.fnext; - let bt = self - .bt - .iter() - .zip(&mut self.eb) - .zip(&self.grids) - .enumerate() - .map(|(i, ((bt, eb), grid))| euler::BoundaryTerms { - north: match bt.north { - euler::BoundaryCharacteristic::This => fields[i].south(), - euler::BoundaryCharacteristic::Grid(g) => fields[g].south(), - euler::BoundaryCharacteristic::Vortex(v) => { - let field = eb.n.as_mut().unwrap(); - vortexify(field.view_mut(), grid.north(), v, time); - field.view() - } - }, - south: match bt.south { - euler::BoundaryCharacteristic::This => fields[i].north(), - euler::BoundaryCharacteristic::Grid(g) => fields[g].north(), - euler::BoundaryCharacteristic::Vortex(v) => { - let field = eb.s.as_mut().unwrap(); - vortexify(field.view_mut(), grid.south(), v, time); - field.view() - } - }, - west: match bt.west { - euler::BoundaryCharacteristic::This => fields[i].east(), - euler::BoundaryCharacteristic::Grid(g) => fields[g].east(), - euler::BoundaryCharacteristic::Vortex(v) => { - let field = eb.w.as_mut().unwrap(); - vortexify(field.view_mut(), grid.west(), v, time); - field.view() - } - }, - east: match bt.east { - euler::BoundaryCharacteristic::This => fields[i].west(), - euler::BoundaryCharacteristic::Grid(g) => fields[g].west(), - euler::BoundaryCharacteristic::Vortex(v) => { - let field = eb.e.as_mut().unwrap(); - vortexify(field.view_mut(), grid.east(), v, time); - field.view() - } - }, - }) - .collect::>(); + let bt = extract_boundaries(&fields, &mut self.bt, &mut self.eb, &self.grids, time); for ((((prev, fut), metrics), wb), bt) in fields .iter() .zip(&mut self.k[i]) @@ -353,6 +264,59 @@ impl System { } } } + +fn extract_boundaries<'a>( + fields: &'a [euler::Field], + bt: &[euler::BoundaryCharacteristics], + eb: &'a mut [MaybeBoundary], + grids: &[grid::Grid], + time: Float, +) -> Vec> { + bt.iter() + .zip(eb) + .zip(grids) + .zip(fields) + .map(|(((bt, eb), grid), field)| euler::BoundaryTerms { + north: match bt.north { + euler::BoundaryCharacteristic::This => field.south(), + euler::BoundaryCharacteristic::Grid(g) => fields[g].south(), + euler::BoundaryCharacteristic::Vortex(v) => { + let field = eb.n.as_mut().unwrap(); + vortexify(field.view_mut(), grid.north(), v, time); + field.view() + } + }, + south: match bt.south { + euler::BoundaryCharacteristic::This => field.north(), + euler::BoundaryCharacteristic::Grid(g) => fields[g].north(), + euler::BoundaryCharacteristic::Vortex(v) => { + let field = eb.s.as_mut().unwrap(); + vortexify(field.view_mut(), grid.south(), v, time); + field.view() + } + }, + west: match bt.west { + euler::BoundaryCharacteristic::This => field.east(), + euler::BoundaryCharacteristic::Grid(g) => fields[g].east(), + euler::BoundaryCharacteristic::Vortex(v) => { + let field = eb.w.as_mut().unwrap(); + vortexify(field.view_mut(), grid.west(), v, time); + field.view() + } + }, + east: match bt.east { + euler::BoundaryCharacteristic::This => field.west(), + euler::BoundaryCharacteristic::Grid(g) => fields[g].west(), + euler::BoundaryCharacteristic::Vortex(v) => { + let field = eb.e.as_mut().unwrap(); + vortexify(field.view_mut(), grid.east(), v, time); + field.view() + } + }, + }) + .collect() +} + #[derive(Debug, Clone)] struct MaybeBoundary { n: Option>,