diff --git a/src/main.rs b/src/main.rs index 98645b4..2579b81 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,19 +11,41 @@ struct Args { /// Name of the output file. #[arg(short, long)] output: String, + + /// Number of voxels in the x-direction. + #[arg(long, default_value_t = 0)] + nelx: usize, + + /// Number of voxels in the y-direction. + #[arg(long, default_value_t = 0)] + nely: usize, + + /// Number of voxels in the z-direction. + #[arg(long, default_value_t = 0)] + nelz: usize, } fn main() { let args = Args::parse(); let input = if args.input.ends_with(".npy") { Spn::from_npy(&args.input) + } else if args.input.ends_with(".spn") { + if args.nelx < 1 { + panic!("Need to specify nelx > 0.") + } else if args.nely < 1 { + panic!("Need to specify nely > 0.") + } else if args.nelz < 1 { + panic!("Need to specify nelz > 0.") + } else { + Spn::new(&args.input, args.nelx, args.nely, args.nelz) + } } else { - panic!("Invalid input {} specified.", args.input) + panic!("Invalid input ({}) specified.", args.input) }; let output = if args.output.ends_with(".exo") { input.into_exodus() } else { - panic!("Invalid output {} specified.", args.output) + panic!("Invalid output ({}) specified.", args.output) }; output.write(&args.output); } diff --git a/src/spn/mod.rs b/src/spn/mod.rs index 672e7ab..d5fe08c 100644 --- a/src/spn/mod.rs +++ b/src/spn/mod.rs @@ -13,6 +13,8 @@ use std::{ io::{BufRead, BufReader}, }; +const NODE_NUMBERING_OFFSET: usize = 1; + type SpnData = Array3; type VoxelData = Vec<[usize; N]>; @@ -88,43 +90,87 @@ fn exodus_data_from_npy_data( .iter() .map(|entry| { vec![ - entry[2] * nelzplus1 * nelyplus1 + entry[1] * nelzplus1 + entry[0] + 2, - entry[2] * nelzplus1 * nelyplus1 + entry[1] * nelzplus1 + entry[0] + 1, - entry[2] * nelzplus1 * nelyplus1 + (entry[1] + 1) * nelzplus1 + entry[0] + 1, - entry[2] * nelzplus1 * nelyplus1 + (entry[1] + 1) * nelzplus1 + entry[0] + 2, - (entry[2] + 1) * nelzplus1 * nelyplus1 + entry[1] * nelzplus1 + entry[0] + 2, - (entry[2] + 1) * nelzplus1 * nelyplus1 + entry[1] * nelzplus1 + entry[0] + 1, - (entry[2] + 1) * nelzplus1 * nelyplus1 + (entry[1] + 1) * nelzplus1 + entry[0] + 1, - (entry[2] + 1) * nelzplus1 * nelyplus1 + (entry[1] + 1) * nelzplus1 + entry[0] + 2, + entry[2] * nelzplus1 * nelyplus1 + + entry[1] * nelzplus1 + + entry[0] + + 1 + + NODE_NUMBERING_OFFSET, + entry[2] * nelzplus1 * nelyplus1 + + entry[1] * nelzplus1 + + entry[0] + + NODE_NUMBERING_OFFSET, + entry[2] * nelzplus1 * nelyplus1 + + (entry[1] + 1) * nelzplus1 + + entry[0] + + NODE_NUMBERING_OFFSET, + entry[2] * nelzplus1 * nelyplus1 + + (entry[1] + 1) * nelzplus1 + + entry[0] + + 1 + + NODE_NUMBERING_OFFSET, + (entry[2] + 1) * nelzplus1 * nelyplus1 + + entry[1] * nelzplus1 + + entry[0] + + 1 + + NODE_NUMBERING_OFFSET, + (entry[2] + 1) * nelzplus1 * nelyplus1 + + entry[1] * nelzplus1 + + entry[0] + + NODE_NUMBERING_OFFSET, + (entry[2] + 1) * nelzplus1 * nelyplus1 + + (entry[1] + 1) * nelzplus1 + + entry[0] + + NODE_NUMBERING_OFFSET, + (entry[2] + 1) * nelzplus1 * nelyplus1 + + (entry[1] + 1) * nelzplus1 + + entry[0] + + 1 + + NODE_NUMBERING_OFFSET, ] }) .collect(); element_connectivity_node_renumbering(&mut element_connectivity); - let nodal_coordinates = filtered_voxel_data - .iter() - .map(|entry| { - vec![ - [entry[2], entry[1], entry[0] + 1], - [entry[2], entry[1], entry[0]], - [entry[2], entry[1] + 1, entry[0]], - [entry[2], entry[1] + 1, entry[0] + 1], - [entry[2] + 1, entry[1], entry[0] + 1], - [entry[2] + 1, entry[1], entry[0]], - [entry[2] + 1, entry[1] + 1, entry[0]], - [entry[2] + 1, entry[1] + 1, entry[0] + 1], - ] - }) - .collect::>>() - .iter() + let number_of_nodes = element_connectivity + .clone() + .into_iter() .flatten() .unique() - .map(|coordinates| { - coordinates - .iter() - .map(|coordinate| *coordinate as f64) - .collect() - }) - .collect(); + .collect::>() + .len(); + let mut nodal_coordinates = vec![vec![0.0; 3]; number_of_nodes]; + filtered_voxel_data + .iter() + .zip(element_connectivity.iter()) + .for_each(|(entry, connectivity)| { + nodal_coordinates[connectivity[0] - NODE_NUMBERING_OFFSET] = + vec![entry[2] as f64, entry[1] as f64, entry[0] as f64 + 1.0]; + nodal_coordinates[connectivity[1] - NODE_NUMBERING_OFFSET] = + vec![entry[2] as f64, entry[1] as f64, entry[0] as f64]; + nodal_coordinates[connectivity[2] - NODE_NUMBERING_OFFSET] = + vec![entry[2] as f64, entry[1] as f64 + 1.0, entry[0] as f64]; + nodal_coordinates[connectivity[3] - NODE_NUMBERING_OFFSET] = vec![ + entry[2] as f64, + entry[1] as f64 + 1.0, + entry[0] as f64 + 1.0, + ]; + nodal_coordinates[connectivity[4] - NODE_NUMBERING_OFFSET] = vec![ + entry[2] as f64 + 1.0, + entry[1] as f64, + entry[0] as f64 + 1.0, + ]; + nodal_coordinates[connectivity[5] - NODE_NUMBERING_OFFSET] = + vec![entry[2] as f64 + 1.0, entry[1] as f64, entry[0] as f64]; + nodal_coordinates[connectivity[6] - NODE_NUMBERING_OFFSET] = vec![ + entry[2] as f64 + 1.0, + entry[1] as f64 + 1.0, + entry[0] as f64, + ]; + nodal_coordinates[connectivity[7] - NODE_NUMBERING_OFFSET] = vec![ + entry[2] as f64 + 1.0, + entry[1] as f64 + 1.0, + entry[0] as f64 + 1.0, + ]; + }); (element_blocks, element_connectivity, nodal_coordinates) } diff --git a/tests/spn.py b/tests/spn.py index 9d057f6..234a1f1 100644 --- a/tests/spn.py +++ b/tests/spn.py @@ -50,108 +50,108 @@ [25, 24, 29, 30, 55, 54, 59, 60], ]) gold_coordinates = np.array([ - [0.0, 0.0, 1.0], [0.0, 0.0, 0.0], + [0.0, 0.0, 1.0], + [0.0, 0.0, 2.0], + [0.0, 0.0, 3.0], + [0.0, 0.0, 4.0], [0.0, 1.0, 0.0], [0.0, 1.0, 1.0], - [1.0, 0.0, 1.0], - [1.0, 0.0, 0.0], - [1.0, 1.0, 0.0], - [1.0, 1.0, 1.0], - [2.0, 0.0, 1.0], - [2.0, 0.0, 0.0], - [2.0, 1.0, 0.0], - [2.0, 1.0, 1.0], - [3.0, 0.0, 1.0], - [3.0, 0.0, 0.0], - [3.0, 1.0, 0.0], - [3.0, 1.0, 1.0], + [0.0, 1.0, 2.0], + [0.0, 1.0, 3.0], + [0.0, 1.0, 4.0], [0.0, 2.0, 0.0], [0.0, 2.0, 1.0], - [1.0, 2.0, 0.0], - [1.0, 2.0, 1.0], - [2.0, 2.0, 0.0], - [2.0, 2.0, 1.0], - [3.0, 2.0, 0.0], - [3.0, 2.0, 1.0], + [0.0, 2.0, 2.0], + [0.0, 2.0, 3.0], + [0.0, 2.0, 4.0], [0.0, 3.0, 0.0], [0.0, 3.0, 1.0], - [1.0, 3.0, 0.0], - [1.0, 3.0, 1.0], - [2.0, 3.0, 0.0], - [2.0, 3.0, 1.0], - [3.0, 3.0, 0.0], - [3.0, 3.0, 1.0], + [0.0, 3.0, 2.0], + [0.0, 3.0, 3.0], + [0.0, 3.0, 4.0], [0.0, 4.0, 0.0], [0.0, 4.0, 1.0], - [1.0, 4.0, 0.0], - [1.0, 4.0, 1.0], - [2.0, 4.0, 0.0], - [2.0, 4.0, 1.0], - [3.0, 4.0, 0.0], - [3.0, 4.0, 1.0], + [0.0, 4.0, 2.0], + [0.0, 4.0, 3.0], + [0.0, 4.0, 4.0], [0.0, 5.0, 0.0], [0.0, 5.0, 1.0], - [1.0, 5.0, 0.0], - [1.0, 5.0, 1.0], - [2.0, 5.0, 0.0], - [2.0, 5.0, 1.0], - [3.0, 5.0, 0.0], - [3.0, 5.0, 1.0], - [0.0, 0.0, 2.0], - [0.0, 1.0, 2.0], + [0.0, 5.0, 2.0], + [0.0, 5.0, 3.0], + [0.0, 5.0, 4.0], + [1.0, 0.0, 0.0], + [1.0, 0.0, 1.0], [1.0, 0.0, 2.0], + [1.0, 0.0, 3.0], + [1.0, 0.0, 4.0], + [1.0, 1.0, 0.0], + [1.0, 1.0, 1.0], [1.0, 1.0, 2.0], - [2.0, 0.0, 2.0], - [2.0, 1.0, 2.0], - [3.0, 0.0, 2.0], - [3.0, 1.0, 2.0], - [0.0, 2.0, 2.0], + [1.0, 1.0, 3.0], + [1.0, 1.0, 4.0], + [1.0, 2.0, 0.0], + [1.0, 2.0, 1.0], [1.0, 2.0, 2.0], - [0.0, 3.0, 2.0], + [1.0, 2.0, 3.0], + [1.0, 2.0, 4.0], + [1.0, 3.0, 0.0], + [1.0, 3.0, 1.0], [1.0, 3.0, 2.0], - [2.0, 2.0, 2.0], - [2.0, 3.0, 2.0], - [0.0, 4.0, 2.0], + [1.0, 3.0, 3.0], + [1.0, 3.0, 4.0], + [1.0, 4.0, 0.0], + [1.0, 4.0, 1.0], [1.0, 4.0, 2.0], - [0.0, 5.0, 2.0], + [1.0, 4.0, 3.0], + [1.0, 4.0, 4.0], + [1.0, 5.0, 0.0], + [1.0, 5.0, 1.0], [1.0, 5.0, 2.0], - [0.0, 0.0, 3.0], - [0.0, 1.0, 3.0], - [1.0, 0.0, 3.0], - [1.0, 1.0, 3.0], + [1.0, 5.0, 3.0], + [1.0, 5.0, 4.0], + [2.0, 0.0, 0.0], + [2.0, 0.0, 1.0], + [2.0, 0.0, 2.0], [2.0, 0.0, 3.0], + [2.0, 0.0, 4.0], + [2.0, 1.0, 0.0], + [2.0, 1.0, 1.0], + [2.0, 1.0, 2.0], [2.0, 1.0, 3.0], - [3.0, 0.0, 3.0], - [3.0, 1.0, 3.0], - [0.0, 2.0, 3.0], - [1.0, 2.0, 3.0], - [0.0, 3.0, 3.0], - [1.0, 3.0, 3.0], + [2.0, 1.0, 4.0], + [2.0, 2.0, 0.0], + [2.0, 2.0, 1.0], + [2.0, 2.0, 2.0], [2.0, 2.0, 3.0], + [2.0, 2.0, 4.0], + [2.0, 3.0, 0.0], + [2.0, 3.0, 1.0], + [2.0, 3.0, 2.0], [2.0, 3.0, 3.0], - [0.0, 4.0, 3.0], - [1.0, 4.0, 3.0], - [0.0, 5.0, 3.0], - [1.0, 5.0, 3.0], - [0.0, 0.0, 4.0], - [0.0, 1.0, 4.0], - [1.0, 0.0, 4.0], - [1.0, 1.0, 4.0], - [2.0, 0.0, 4.0], - [2.0, 1.0, 4.0], + [2.0, 3.0, 4.0], + [2.0, 4.0, 0.0], + [2.0, 4.0, 1.0], + [2.0, 5.0, 0.0], + [2.0, 5.0, 1.0], + [3.0, 0.0, 0.0], + [3.0, 0.0, 1.0], + [3.0, 0.0, 2.0], + [3.0, 0.0, 3.0], [3.0, 0.0, 4.0], + [3.0, 1.0, 0.0], + [3.0, 1.0, 1.0], + [3.0, 1.0, 2.0], + [3.0, 1.0, 3.0], [3.0, 1.0, 4.0], - [0.0, 2.0, 4.0], - [1.0, 2.0, 4.0], - [0.0, 3.0, 4.0], - [1.0, 3.0, 4.0], - [2.0, 2.0, 4.0], - [2.0, 3.0, 4.0], - [0.0, 4.0, 4.0], - [1.0, 4.0, 4.0], - [0.0, 5.0, 4.0], - [1.0, 5.0, 4.0], + [3.0, 2.0, 0.0], + [3.0, 2.0, 1.0], + [3.0, 3.0, 0.0], + [3.0, 3.0, 1.0], + [3.0, 4.0, 0.0], + [3.0, 4.0, 1.0], + [3.0, 5.0, 0.0], + [3.0, 5.0, 1.0], ]) diff --git a/tests/spn.rs b/tests/spn.rs index 16661b6..d1adbc0 100644 --- a/tests/spn.rs +++ b/tests/spn.rs @@ -49,108 +49,108 @@ const GOLD_CONNECTIVITY: [[usize; 8]; NUM_ELEMENTS] = [ [25, 24, 29, 30, 55, 54, 59, 60], ]; const GOLD_COORDINATES: [[f64; 3]; NUM_NODES] = [ - [0.0, 0.0, 1.0], [0.0, 0.0, 0.0], + [0.0, 0.0, 1.0], + [0.0, 0.0, 2.0], + [0.0, 0.0, 3.0], + [0.0, 0.0, 4.0], [0.0, 1.0, 0.0], [0.0, 1.0, 1.0], - [1.0, 0.0, 1.0], - [1.0, 0.0, 0.0], - [1.0, 1.0, 0.0], - [1.0, 1.0, 1.0], - [2.0, 0.0, 1.0], - [2.0, 0.0, 0.0], - [2.0, 1.0, 0.0], - [2.0, 1.0, 1.0], - [3.0, 0.0, 1.0], - [3.0, 0.0, 0.0], - [3.0, 1.0, 0.0], - [3.0, 1.0, 1.0], + [0.0, 1.0, 2.0], + [0.0, 1.0, 3.0], + [0.0, 1.0, 4.0], [0.0, 2.0, 0.0], [0.0, 2.0, 1.0], - [1.0, 2.0, 0.0], - [1.0, 2.0, 1.0], - [2.0, 2.0, 0.0], - [2.0, 2.0, 1.0], - [3.0, 2.0, 0.0], - [3.0, 2.0, 1.0], + [0.0, 2.0, 2.0], + [0.0, 2.0, 3.0], + [0.0, 2.0, 4.0], [0.0, 3.0, 0.0], [0.0, 3.0, 1.0], - [1.0, 3.0, 0.0], - [1.0, 3.0, 1.0], - [2.0, 3.0, 0.0], - [2.0, 3.0, 1.0], - [3.0, 3.0, 0.0], - [3.0, 3.0, 1.0], + [0.0, 3.0, 2.0], + [0.0, 3.0, 3.0], + [0.0, 3.0, 4.0], [0.0, 4.0, 0.0], [0.0, 4.0, 1.0], - [1.0, 4.0, 0.0], - [1.0, 4.0, 1.0], - [2.0, 4.0, 0.0], - [2.0, 4.0, 1.0], - [3.0, 4.0, 0.0], - [3.0, 4.0, 1.0], + [0.0, 4.0, 2.0], + [0.0, 4.0, 3.0], + [0.0, 4.0, 4.0], [0.0, 5.0, 0.0], [0.0, 5.0, 1.0], - [1.0, 5.0, 0.0], - [1.0, 5.0, 1.0], - [2.0, 5.0, 0.0], - [2.0, 5.0, 1.0], - [3.0, 5.0, 0.0], - [3.0, 5.0, 1.0], - [0.0, 0.0, 2.0], - [0.0, 1.0, 2.0], + [0.0, 5.0, 2.0], + [0.0, 5.0, 3.0], + [0.0, 5.0, 4.0], + [1.0, 0.0, 0.0], + [1.0, 0.0, 1.0], [1.0, 0.0, 2.0], + [1.0, 0.0, 3.0], + [1.0, 0.0, 4.0], + [1.0, 1.0, 0.0], + [1.0, 1.0, 1.0], [1.0, 1.0, 2.0], - [2.0, 0.0, 2.0], - [2.0, 1.0, 2.0], - [3.0, 0.0, 2.0], - [3.0, 1.0, 2.0], - [0.0, 2.0, 2.0], + [1.0, 1.0, 3.0], + [1.0, 1.0, 4.0], + [1.0, 2.0, 0.0], + [1.0, 2.0, 1.0], [1.0, 2.0, 2.0], - [0.0, 3.0, 2.0], + [1.0, 2.0, 3.0], + [1.0, 2.0, 4.0], + [1.0, 3.0, 0.0], + [1.0, 3.0, 1.0], [1.0, 3.0, 2.0], - [2.0, 2.0, 2.0], - [2.0, 3.0, 2.0], - [0.0, 4.0, 2.0], + [1.0, 3.0, 3.0], + [1.0, 3.0, 4.0], + [1.0, 4.0, 0.0], + [1.0, 4.0, 1.0], [1.0, 4.0, 2.0], - [0.0, 5.0, 2.0], + [1.0, 4.0, 3.0], + [1.0, 4.0, 4.0], + [1.0, 5.0, 0.0], + [1.0, 5.0, 1.0], [1.0, 5.0, 2.0], - [0.0, 0.0, 3.0], - [0.0, 1.0, 3.0], - [1.0, 0.0, 3.0], - [1.0, 1.0, 3.0], + [1.0, 5.0, 3.0], + [1.0, 5.0, 4.0], + [2.0, 0.0, 0.0], + [2.0, 0.0, 1.0], + [2.0, 0.0, 2.0], [2.0, 0.0, 3.0], + [2.0, 0.0, 4.0], + [2.0, 1.0, 0.0], + [2.0, 1.0, 1.0], + [2.0, 1.0, 2.0], [2.0, 1.0, 3.0], - [3.0, 0.0, 3.0], - [3.0, 1.0, 3.0], - [0.0, 2.0, 3.0], - [1.0, 2.0, 3.0], - [0.0, 3.0, 3.0], - [1.0, 3.0, 3.0], + [2.0, 1.0, 4.0], + [2.0, 2.0, 0.0], + [2.0, 2.0, 1.0], + [2.0, 2.0, 2.0], [2.0, 2.0, 3.0], + [2.0, 2.0, 4.0], + [2.0, 3.0, 0.0], + [2.0, 3.0, 1.0], + [2.0, 3.0, 2.0], [2.0, 3.0, 3.0], - [0.0, 4.0, 3.0], - [1.0, 4.0, 3.0], - [0.0, 5.0, 3.0], - [1.0, 5.0, 3.0], - [0.0, 0.0, 4.0], - [0.0, 1.0, 4.0], - [1.0, 0.0, 4.0], - [1.0, 1.0, 4.0], - [2.0, 0.0, 4.0], - [2.0, 1.0, 4.0], + [2.0, 3.0, 4.0], + [2.0, 4.0, 0.0], + [2.0, 4.0, 1.0], + [2.0, 5.0, 0.0], + [2.0, 5.0, 1.0], + [3.0, 0.0, 0.0], + [3.0, 0.0, 1.0], + [3.0, 0.0, 2.0], + [3.0, 0.0, 3.0], [3.0, 0.0, 4.0], + [3.0, 1.0, 0.0], + [3.0, 1.0, 1.0], + [3.0, 1.0, 2.0], + [3.0, 1.0, 3.0], [3.0, 1.0, 4.0], - [0.0, 2.0, 4.0], - [1.0, 2.0, 4.0], - [0.0, 3.0, 4.0], - [1.0, 3.0, 4.0], - [2.0, 2.0, 4.0], - [2.0, 3.0, 4.0], - [0.0, 4.0, 4.0], - [1.0, 4.0, 4.0], - [0.0, 5.0, 4.0], - [1.0, 5.0, 4.0], + [3.0, 2.0, 0.0], + [3.0, 2.0, 1.0], + [3.0, 3.0, 0.0], + [3.0, 3.0, 1.0], + [3.0, 4.0, 0.0], + [3.0, 4.0, 1.0], + [3.0, 5.0, 0.0], + [3.0, 5.0, 1.0], ]; const GOLD_DATA: [[[u8; NELX]; NELY]; NELZ] = [ [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]],