use crate::atoms::NodeId;
use crate::containers::field::Field;
use crate::containers::node_field::NodeField;
use crate::coords::Coords;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
fn resolve_axis_components(
field: &NodeField,
components: Option<Vec<String>>,
dim: usize,
default: &[&str],
) -> Result<Vec<String>> {
let comps = match components {
Some(c) => c,
None => default[..dim.min(default.len())]
.iter()
.map(|s| s.to_string())
.collect(),
};
if comps.len() != dim {
return Err(PyrucastError::Message(format!(
"expected {dim} component name(s) (one per axis), got {}",
comps.len()
)));
}
let have = Field::components(field);
for name in &comps {
if !have.iter().any(|c| c == name) {
return Err(PyrucastError::Message(format!(
"component '{name}' not in field (has: [{}])",
have.join(", ")
)));
}
}
Ok(comps)
}
fn per_node_values(field: &NodeField, comps: &[String]) -> Result<Vec<(NodeId, Vec<f64>)>> {
let nodes = field.node_ids();
let mut out = Vec::with_capacity(nodes.len());
for nid in nodes {
let mut values = Vec::with_capacity(comps.len());
for name in comps {
values.push(field.value(nid, name)?);
}
out.push((nid, values));
}
Ok(out)
}
pub fn set(field: &NodeField, components: Option<Vec<String>>) -> Result<()> {
let coords = field.coords()?;
let dim = coords.read().dim() as usize;
let comps = resolve_axis_components(field, components, dim, &["X", "Y", "Z"])?;
let targets = per_node_values(field, &comps)?;
let mut c = coords.write();
for (nid, coord) in &targets {
c.set_position(*nid, coord)?;
}
Ok(())
}
pub fn displace(field: &NodeField, components: Option<Vec<String>>) -> Result<()> {
let coords = field.coords()?;
let dim = coords.read().dim() as usize;
let comps = resolve_axis_components(field, components, dim, &["ux", "uy", "uz"])?;
let increments = per_node_values(field, &comps)?;
let mut c = coords.write();
for (nid, inc) in &increments {
let mut coord = c.position(*nid)?.to_vec();
for (a, dv) in inc.iter().enumerate() {
coord[a] += dv;
}
c.set_position(*nid, &coord)?;
}
Ok(())
}
pub fn displace_from(
field: &NodeField,
reference: usize,
target: usize,
components: Option<Vec<String>>,
) -> Result<()> {
let coords = field.coords()?;
let dim = coords.read().dim() as usize;
let comps = resolve_axis_components(field, components, dim, &["ux", "uy", "uz"])?;
let increments = per_node_values(field, &comps)?;
coords.write().displace_from(
reference,
target,
increments.iter().map(|(nid, inc)| (*nid, inc.as_slice())),
)
}
pub fn in_config<R>(
coords: &Handle<Coords>,
config: usize,
f: impl FnOnce() -> Result<R>,
) -> Result<R> {
let previous = {
let mut c = coords.write();
let previous = c.active();
c.select(config)?;
previous
};
let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
coords.write().select(previous)?;
match out {
Ok(result) => result,
Err(panic) => std::panic::resume_unwind(panic),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::aggregate::Aggregate;
use crate::atoms::{ElementType, Node};
use crate::containers::mesh::{Mesh, SubMesh};
use crate::containers::node_field::SubNodeField;
use crate::coords::Coords;
use crate::handle::Handle;
use crate::ops::node_field::positions;
#[test]
fn set_writes_active_config() {
let coords = Handle::new(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, 1.0]).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::POI1));
mesh.add_cell(&[a.id()]).unwrap();
mesh.add_cell(&[b.id()]).unwrap();
let f = positions(&mesh, None).unwrap();
{
let mut s = f.get(0).unwrap().write();
s.set_value(a.id(), "X", 10.0).unwrap();
s.set_value(a.id(), "Y", 20.0).unwrap();
}
set(&f, None).unwrap();
{
let c = coords.read();
assert_eq!(c.position(a.id()).unwrap(), &[10.0, 20.0]);
assert_eq!(c.position(b.id()).unwrap(), &[1.0, 1.0]);
}
}
#[test]
fn displace_adds_to_active_config() {
let coords = Handle::new(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, 1.0]).unwrap();
let support = Handle::new(SubMesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap());
let mut d = SubNodeField::from_poi1(&support, vec!["ux".into(), "uy".into()]).unwrap();
d.set_value(a.id(), "ux", 5.0).unwrap();
d.set_value(a.id(), "uy", -1.0).unwrap();
d.set_value(b.id(), "ux", 2.0).unwrap();
displace(&NodeField::from_sub(d), None).unwrap();
{
let c = coords.read();
assert_eq!(c.position(a.id()).unwrap(), &[5.0, -1.0]);
assert_eq!(c.position(b.id()).unwrap(), &[3.0, 1.0]);
}
}
#[test]
fn displace_moves_interface_nodes_once() {
let coords = Handle::new(Coords::new(1).unwrap());
let s = Node::create_in(coords.clone(), &[1.0]).unwrap();
let mut mesh = Mesh::empty();
for _ in 0..2 {
let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
sm.add_cell(&[s.id()]).unwrap();
mesh.add_sub(Handle::new(sm)).unwrap();
}
let f = NodeField::new(&mesh, vec!["ux".into()]).unwrap();
for i in 0..2 {
f.get(i)
.unwrap()
.write()
.set_value(s.id(), "ux", 0.5)
.unwrap();
}
displace(&f, None).unwrap();
assert_eq!(coords.read().position(s.id()).unwrap(), &[1.5]);
}
#[test]
fn in_config_restores_the_previous_configuration() {
let coords = Handle::new(Coords::new(1).unwrap());
let a = coords.write().add_config("a");
let b = coords.write().add_config("b");
coords.write().select(a).unwrap();
let seen = in_config(&coords, b, || {
let inner = in_config(&coords, 0, || Ok(coords.read().active()))?;
Ok((coords.read().active(), inner))
})
.unwrap();
assert_eq!(seen, (b, 0));
assert_eq!(coords.read().active(), a);
let err = in_config(&coords, b, || -> Result<()> {
Err(PyrucastError::Message("inside".into()))
});
assert!(err.is_err());
assert_eq!(coords.read().active(), a);
let panicked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _ = in_config(&coords, b, || -> Result<()> { panic!("inside") });
}));
assert!(panicked.is_err());
assert_eq!(coords.read().active(), a);
let mut ran = false;
assert!(in_config(&coords, 3, || {
ran = true;
Ok(())
})
.is_err());
assert!(!ran);
assert_eq!(coords.read().active(), a);
}
#[test]
fn writers_reject_wrong_component_count() {
let coords = Handle::new(Coords::new(2).unwrap());
let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::POI1));
mesh.add_cell(&[a.id()]).unwrap();
let f = positions(&mesh, None).unwrap();
assert!(set(&f, Some(vec!["X".into()])).is_err());
assert!(set(&f, Some(vec!["X".into(), "BOGUS".into()])).is_err());
}
}