pub struct Frame {
pub meta: HashMap<String, String>,
pub simbox: Option<SimBox>,
/* private fields */
}Expand description
A dictionary from string keys to Blocks.
Frame provides a simple container for organizing multiple blocks of data, typically representing different aspects of a molecular system (e.g., atoms, bonds, velocities). Each block can have different numbers of rows and different column types.
Fields§
§meta: HashMap<String, String>Arbitrary key-value metadata associated with the frame.
simbox: Option<SimBox>Simulation box defining periodic boundary conditions.
Implementations§
Source§impl Frame
impl Frame
Sourcepub fn new() -> Frame
pub fn new() -> Frame
Creates an empty Frame.
§Examples
use molrs_core::frame::Frame;
let frame = Frame::new();
assert!(frame.is_empty());Sourcepub fn with_capacity(cap: usize) -> Frame
pub fn with_capacity(cap: usize) -> Frame
Creates an empty Frame with the specified capacity for blocks.
§Examples
use molrs_core::frame::Frame;
let frame = Frame::with_capacity(10);
assert!(frame.is_empty());Sourcepub fn from_map(map: HashMap<String, Block>) -> Frame
pub fn from_map(map: HashMap<String, Block>) -> Frame
Creates a Frame from an existing HashMap of blocks.
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("atoms".to_string(), Block::new());
let frame = Frame::from_map(map);
assert_eq!(frame.len(), 1);Sourcepub fn into_inner(
self,
) -> (HashMap<String, Block>, HashMap<String, Grid>, HashMap<String, String>, Option<SimBox>)
pub fn into_inner( self, ) -> (HashMap<String, Block>, HashMap<String, Grid>, HashMap<String, String>, Option<SimBox>)
Consumes the Frame and returns the inner HashMap of blocks, grids, metadata, and simbox.
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
let mut frame = Frame::new();
frame.insert("atoms", Block::new());
frame.meta.insert("title".into(), "Test".into());
let (blocks, grids, meta, simbox) = frame.into_inner();
assert_eq!(blocks.len(), 1);
assert!(grids.is_empty());
assert_eq!(meta.get("title").unwrap(), "Test");
assert!(simbox.is_none());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of blocks (keys) in the frame.
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
let mut frame = Frame::new();
assert_eq!(frame.len(), 0);
frame.insert("atoms", Block::new());
assert_eq!(frame.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the frame contains no blocks.
Note: This only checks blocks, not metadata.
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Returns true if the frame contains the specified key.
Sourcepub fn get(&self, key: &str) -> Option<&Block>
pub fn get(&self, key: &str) -> Option<&Block>
Gets an immutable reference to the block for key if present.
For a panicking version, use the Index trait: &frame["key"].
Sourcepub fn get_mut(&mut self, key: &str) -> Option<&mut Block>
pub fn get_mut(&mut self, key: &str) -> Option<&mut Block>
Gets a mutable reference to the block for key if present.
For a panicking version, use the IndexMut trait: &mut frame["key"].
Sourcepub fn insert(&mut self, key: impl Into<String>, block: Block) -> Option<Block>
pub fn insert(&mut self, key: impl Into<String>, block: Block) -> Option<Block>
Inserts a block under key. Returns the previous block if any.
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
let mut frame = Frame::new();
let old = frame.insert("atoms", Block::new());
assert!(old.is_none());
let old = frame.insert("atoms", Block::new());
assert!(old.is_some());Sourcepub fn remove(&mut self, key: &str) -> Option<Block>
pub fn remove(&mut self, key: &str) -> Option<Block>
Removes and returns the block for key, if present.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the frame, removing all blocks.
Note: This does NOT clear metadata. Use clear_all() to clear both.
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
let mut frame = Frame::new();
frame.insert("atoms", Block::new());
frame.meta.insert("title".into(), "Test".into());
frame.clear();
assert!(frame.is_empty());
assert!(!frame.meta.is_empty()); // metadata preservedSourcepub fn clear_all(&mut self)
pub fn clear_all(&mut self)
Clears both blocks and metadata.
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
let mut frame = Frame::new();
frame.insert("atoms", Block::new());
frame.meta.insert("title".into(), "Test".into());
frame.clear_all();
assert!(frame.is_empty());
assert!(frame.meta.is_empty());Sourcepub fn insert_grid(
&mut self,
name: impl Into<String>,
grid: Grid,
) -> Option<Grid>
pub fn insert_grid( &mut self, name: impl Into<String>, grid: Grid, ) -> Option<Grid>
Insert or replace a named grid.
Sourcepub fn remove_grid(&mut self, name: &str) -> Option<Grid>
pub fn remove_grid(&mut self, name: &str) -> Option<Grid>
Remove a named grid.
Sourcepub fn get_grid_mut(&mut self, name: &str) -> Option<&mut Grid>
pub fn get_grid_mut(&mut self, name: &str) -> Option<&mut Grid>
Borrow a grid mutably by name.
Sourcepub fn grids(&self) -> impl Iterator<Item = (&str, &Grid)>
pub fn grids(&self) -> impl Iterator<Item = (&str, &Grid)>
Returns an iterator over (name, grid) pairs.
Sourcepub fn rename_column(
&mut self,
block_key: &str,
old_col_key: &str,
new_col_key: &str,
) -> bool
pub fn rename_column( &mut self, block_key: &str, old_col_key: &str, new_col_key: &str, ) -> bool
Renames a column in the specified block.
Returns true if the column was successfully renamed, false if the block doesn’t exist,
the old column key doesn’t exist, or the new column key already exists.
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
use molrs_core::types::F;
use ndarray::Array1;
let mut frame = Frame::new();
let mut atoms = Block::new();
atoms.insert("x", Array1::from_vec(vec![1.0 as F, 2.0 as F]).into_dyn()).unwrap();
frame.insert("atoms", atoms);
assert!(frame.rename_column("atoms", "x", "position_x"));
assert!(!frame["atoms"].contains_key("x"));
assert!(frame["atoms"].contains_key("position_x"));Sourcepub fn rename_block(&mut self, old_key: &str, new_key: &str) -> bool
pub fn rename_block(&mut self, old_key: &str, new_key: &str) -> bool
Renames a block in the frame.
Returns true if the block was successfully renamed, false if the old block
doesn’t exist or the new block name already exists.
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
use molrs_core::types::F;
use ndarray::Array1;
let mut frame = Frame::new();
let mut atoms = Block::new();
atoms.insert("x", Array1::from_vec(vec![1.0 as F]).into_dyn()).unwrap();
frame.insert("atoms", atoms);
assert!(frame.rename_block("atoms", "molecules"));
assert!(!frame.contains_key("atoms"));
assert!(frame.contains_key("molecules"));Sourcepub fn iter(&self) -> impl Iterator<Item = (&str, &Block)>
pub fn iter(&self) -> impl Iterator<Item = (&str, &Block)>
Returns an iterator over (&str, &Block).
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut Block)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut Block)>
Returns a mutable iterator over (&str, &mut Block).
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
use molrs_core::types::F;
use ndarray::Array1;
let mut frame = Frame::new();
let mut atoms = Block::new();
atoms.insert("x", Array1::from_vec(vec![1.0 as F]).into_dyn()).unwrap();
frame.insert("atoms", atoms);
for (_name, block) in frame.iter_mut() {
// Can mutate blocks
if let Some(x) = block.get_float_mut("x") {
x[[0]] = 99.0 as F;
}
}Sourcepub fn values(&self) -> impl Iterator<Item = &Block>
pub fn values(&self) -> impl Iterator<Item = &Block>
Returns an iterator over block references.
Sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut Block>
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut Block>
Returns a mutable iterator over block references.
Sourcepub fn validate(&self) -> Result<(), MolRsError>
pub fn validate(&self) -> Result<(), MolRsError>
Validates cross-block consistency.
This method checks for common consistency issues:
- All blocks with “atoms” prefix should have the same nrows
- Bond indices (if present) should reference valid atoms
§Returns
Ok(())if validation passesErr(MolRsError::Validation)with details if validation fails
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
use molrs_core::types::F;
use ndarray::Array1;
let mut frame = Frame::new();
let mut atoms = Block::new();
atoms.insert("x", Array1::from_vec(vec![1.0 as F, 2.0 as F, 3.0 as F]).into_dyn()).unwrap();
atoms.insert("y", Array1::from_vec(vec![0.0 as F, 1.0 as F, 2.0 as F]).into_dyn()).unwrap();
frame.insert("atoms", atoms);
assert!(frame.validate().is_ok());Sourcepub fn is_consistent(&self) -> bool
pub fn is_consistent(&self) -> bool
Checks if the frame is consistent without returning an error.
This is a non-panicking version of validate() that returns a boolean.
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
let frame = Frame::new();
assert!(frame.is_consistent());Trait Implementations§
Source§impl FrameAccess for Frame
impl FrameAccess for Frame
Source§fn get_float(
&self,
block_key: &str,
col_key: &str,
) -> Option<ArrayBase<ViewRepr<&f64>, Dim<IxDynImpl>>>
fn get_float( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&f64>, Dim<IxDynImpl>>>
Source§fn get_int(
&self,
block_key: &str,
col_key: &str,
) -> Option<ArrayBase<ViewRepr<&i32>, Dim<IxDynImpl>>>
fn get_int( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&i32>, Dim<IxDynImpl>>>
Source§fn get_bool(
&self,
block_key: &str,
col_key: &str,
) -> Option<ArrayBase<ViewRepr<&bool>, Dim<IxDynImpl>>>
fn get_bool( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&bool>, Dim<IxDynImpl>>>
Source§fn get_uint(
&self,
block_key: &str,
col_key: &str,
) -> Option<ArrayBase<ViewRepr<&u32>, Dim<IxDynImpl>>>
fn get_uint( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&u32>, Dim<IxDynImpl>>>
Source§fn get_u8(
&self,
block_key: &str,
col_key: &str,
) -> Option<ArrayBase<ViewRepr<&u8>, Dim<IxDynImpl>>>
fn get_u8( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&u8>, Dim<IxDynImpl>>>
Source§fn get_string(
&self,
block_key: &str,
col_key: &str,
) -> Option<ArrayBase<ViewRepr<&String>, Dim<IxDynImpl>>>
fn get_string( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&String>, Dim<IxDynImpl>>>
Source§fn simbox_ref(&self) -> Option<&SimBox>
fn simbox_ref(&self) -> Option<&SimBox>
Source§fn block_keys(&self) -> Vec<&str>
fn block_keys(&self) -> Vec<&str>
Vec.Source§fn contains_block(&self, key: &str) -> bool
fn contains_block(&self, key: &str) -> bool
true if the frame contains the specified block key.Source§fn block_count(&self) -> usize
fn block_count(&self) -> usize
Source§fn visit_block<R>(
&self,
key: &str,
f: impl FnOnce(&dyn BlockAccess) -> R,
) -> Option<R>
fn visit_block<R>( &self, key: &str, f: impl FnOnce(&dyn BlockAccess) -> R, ) -> Option<R>
BlockAccess trait, using the visitor pattern
to avoid lifetime/return-type issues. Returns None if the block does not exist.