use crate::aggregate::Aggregate;
use crate::containers::mesh::Mesh;
use crate::error::Result;
use crate::store::read;
pub fn to_poi1(mesh: &Mesh) -> Result<Mesh> {
let mut result = Mesh::empty();
for sm_handle in mesh {
result.add_sub(read(sm_handle)?.to_poi1()?)?;
}
Ok(result)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::aggregate::Aggregate;
use crate::atoms::ElementType;
use crate::atoms::Node;
use crate::containers::mesh::{Mesh, SubMesh};
use crate::coords::Coords;
use crate::store::insert;
#[test]
fn tri3_submesh_becomes_unique_node_list() {
let coords = insert(Coords::new(2).unwrap());
let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[1.0, 0.0]).unwrap();
let c = Node::create_in(coords.clone(), &[0.5, 1.0]).unwrap();
let d = Node::create_in(coords.clone(), &[1.5, 1.0]).unwrap();
let mut tri = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::TRI3));
tri.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
tri.add_cell(&[b.id(), d.id(), c.id()]).unwrap();
let poi = to_poi1(&tri).unwrap();
assert_eq!(poi.len(), 1);
assert_eq!(poi.element_types().unwrap(), vec![ElementType::POI1]);
assert_eq!(poi.cell_count().unwrap(), 4);
let ids: Vec<_> = (0..4).map(|i| poi.node(0, i, 0).unwrap().id()).collect();
assert_eq!(ids, vec![a.id(), b.id(), c.id(), d.id()]);
}
#[test]
fn preserves_submesh_count_and_increfs_nodes() {
let coords = insert(Coords::new(2).unwrap());
let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[1.0, 0.0]).unwrap();
let c = Node::create_in(coords.clone(), &[0.5, 1.0]).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::POI1));
mesh.add_cell(&[a.id()]).unwrap();
let sm_tri = {
let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
sm.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
insert(sm)
};
mesh.add_sub(sm_tri).unwrap();
{
let cf = read(&coords).unwrap();
assert_eq!(cf.refcount(a.id()), 3);
assert_eq!(cf.refcount(b.id()), 2);
}
let poi = to_poi1(&mesh).unwrap();
assert_eq!(poi.len(), 2, "same number of submeshes");
assert_eq!(
poi.element_types().unwrap(),
vec![ElementType::POI1, ElementType::POI1]
);
assert_eq!(poi.cell_counts().unwrap(), vec![1, 3]);
{
let cf = read(&coords).unwrap();
assert_eq!(cf.refcount(a.id()), 5); assert_eq!(cf.refcount(b.id()), 3); }
drop(poi);
{
let cf = read(&coords).unwrap();
assert_eq!(cf.refcount(a.id()), 3);
assert_eq!(cf.refcount(b.id()), 2);
}
}
#[test]
fn empty_mesh_gives_empty_mesh() {
let poi = to_poi1(&Mesh::empty()).unwrap();
assert_eq!(poi.len(), 0);
}
}