simloadtools.py¶
- str_to_path(default_path, file)[source]¶
Appends a string filename to a path or just returns a path to the file. Also, checks the file exists.
- Parameters:
default_path (
Path) – The default path to be used if the file is a string.file (
str | Path) – The file as either a string name or a full path to the file.
- Returns:
Path– Full path to the file.- Raises:
FileNotFoundError – The assemebled full path is not a file.
- load_field_files(fields_dir, files_pattern, field_slices, header, delimiter=',', frames=None, workers=None)[source]¶
Loads a series of physics field files into a dictionary keyed by the field names given in the field slices with values that are numpy arrays.
- Parameters:
fields_dir (
Path) – Path to the directory in which the field files are contained.files_pattern (
str) – Wildcard string pattern used to identify the field files to load. For example: ‘node_field_*’’ or ‘node_field_frame*’’.field_slices (
dict[str,slice | None]) – Dictionary keyed with the field names with slices that identify which column contains the field data. If the slice is None then the whole file is assumed to be one field where the second dimension is time.header (
int | None) – Header rows to skip for loading plain text files, starts at 0 to skip the first row. None does not skip any rows.frames (
slice | None, optional) – _description_, by default Noneload_opts (
SimLoadOpts | None, optional) – Options for loading the data including parallelisation and delimiter for plain text files, by default None
- Returns:
dict[str,np.ndarray]– The field data as numpy arrays keyed with the field name e.g. ‘disp_x’.- Raises:
FileNotFoundError – The specified directory to load from is not a directory or there are no files found in the directory using the given wildcard matching pattern.
- load_field_dict(path, field_slices, header, delimiter)[source]¶
Loads a single field file into a dictionary keyed by the field name.
- Parameters:
path (
Path) – Full file path to load the field array from can be plain txt or numpy npy binary format.field_slices (
dict[str,slice]) – Slices to extract the field data from the arrayheader (
int | None) – Number of header rows for plain text files to skip starting at 0. If None then now headers rows are skipped.delimiter (
str) – Delimiter used for plain text files.
- Returns:
dict[str,np.ndarray]– The field data from the single file as a dictionary keyed by the field name.
- load_array(file_path, header, delimiter)[source]¶
Loads a single array from disk as either plain text or in numpy npy binary format.
- Parameters:
file_path (
Path) – Full path to the file including the extension. Note that for numpy binary format the .npy extension must be used all other extensions will be treated as plain text.header (
int | None) – Number of header rows for plain text files to skip starting at 0. If None then now headers rows are skipped.delimiter (
str) – Delimiter used for plain text files.
- Returns:
np.ndarray– The data array loaded from disk.- Raises:
FileNotFoundError – The specified path is not a file.
- load_txt_file(file_path, header, delimiter)[source]¶
Wrapper function that loads a delimited plain text file as a numpy array. Allows for simple substitution of different text file loading backends.
- Parameters:
file_path (
Path) – Full path to the plain text file.header (
int | None) – Number of header rows to skip, starts at 0 skipping the first row. If None then no header rows are skipped.delimiter (
str) – _description_
- Returns:
np.ndarray– Numpy array loaded from the specified plain text file.
- load_connectivity(connect_dir, connect_pattern, load_opts)[source]¶
Loads the connectivity tables for all meshes in a given simulation into a dictionary keyed by the mesh name. The keys default to “connectX” where X is an integer. Note that files with a .npy extension will be loaded as numpy binary arrays and any other extension is treated as delimited plain text.
- Parameters:
connect_dir (
Path) – Directory containing the connectivity table files with one file per mesh in the simulation.connect_pattern (
str | list[str]) – Wildcard pattern used to identify connectivity files in the directory or list of file names in the given directory to loadload_opts (
SimLoadOpts) – Options for loading the simulation data including header information for the connectivity file.
- Returns:
dict[str,np.ndarray]– Dicitionary of connectivity tables for meshes in the simulation.- Raises:
SimLoadErr – The connectivity wildcard pattern is not a string or list of strings.
- load_glob_vars(glob_file, glob_slices, load_opts)[source]¶
Loads the global variables from disk into a dictionary. Examples of global simulation variables include: the maximum temperature or a reaction force.
- Parameters:
glob_file (
Path) – Full path to the file containing the global variable data. Can be either numpy binary with a .npy extension of plain text with any other extension.glob_slices (
dict[str,slice]) – Dictionary specifying which columns should be sliced to extract the named global variable.load_opts (
SimLoadOpts) – Options for loading the simulation data.
- Returns:
dict[str,np.ndarray]– Dictionary containing the global variables keyed using the same keys as provided in the ‘glob_slices’ dictionary above.- Raises:
SimLoadErr – The specified path is not a file.
- check_sim_data_consistency(sim_data)[source]¶
Checks consistency of nodal field variables with the rest of the SimData structure.
- Parameters:
sim_data (
SimData) – The SimData object to check the nodal field variables for consistency.- Raises:
SimLoadErr – The nodal fields in the node_vars dictionary are dimensionally inconsistent with themselves, the coordinates or the time steps.
- inv_group_dict(dict_com)[source]¶
Helper function to switch keys and values in a dictionary, i.e. invert the dictionary such that keys become values and values become keys.
- Parameters:
dict_com (
dict[str,str]) – Input dictionary to be inverted with keys and values of strings.- Returns:
dict[str,str]– Inverted dictionary where the keys and values are switched compared to the input dictionary.