Source code for pyNastran.bdf.field_writer_8

"""Defines functions for single precision 8 character field writing."""
import sys
import math
import warnings
from typing import Optional, Any

import numpy as np
from numpy import int32, int64, float32, float64, isnan


[docs] def set_string8_blank_if_default(value: Any, default: Any) -> str: """helper method for writing BDFs""" val = set_blank_if_default(value, default) if val is None: return ' ' return '%8s' % val
[docs] def is_same(value1: Any, value2: Any) -> bool: """ Checks to see if 2 values are the same .. note:: this method is used by almost every card when printing """ if isinstance(value1, str) or value1 is None: return value1 == value2 if value1 == value2: return True return False
[docs] def set_blank_if_default(value: Any, default: Any) -> Optional[int | float | str]: """ Used when setting the output data of a card to clear default values Parameters ---------- value : int/float/str the field value the may be set to None (blank) if value=default, the default value for the field default : int/float/str the default value .. note:: this method is used by almost every card when printing """ if isinstance(value, (float, float32, float64)) and isnan(value): return None return None if is_same(value, default) else value
[docs] def set_default_if_blank(value: Any, default: Any) -> int | float | str: """ Used when initializing a card and the default value isn't set Used on PBARL""" return default if value is None or value == '' else value
# def print_float_or_int_8(value: int | float) - str: # """ # Prints an 8-character width field # # Parameters # ---------- # value : int/float # the value to print # # Returns # ------- # field : str # an 8-character string # """ # if isinstance(value, (float, float32, float64)): # field = print_float_8(value) # elif isinstance(value, int): # field = "%8i" % value # else: # msg = 'Invalid Type: value=%r type=%s' % (value, type(value)) # raise TypeError(msg) # return field _POS_BINS_8 = [ (0.1, 1.0, '%8.7f'), (1.0, 10.0, '%8.6f'), (10.0, 100.0, '%8.5f'), (100.0, 1000.0, '%8.4f'), (1000.0, 10000.0, '%8.3f'), (10000.0, 100000.0, '%8.2f'), (100000.0, 1000000.0, '%8.1f'), ] _NEG_BINS_8 = [ (0.1, 1.0, '%8.6f'), (1.0, 10.0, '%8.5f'), (10.0, 100.0, '%8.4f'), (100.0, 1000.0, '%8.3f'), (1000.0, 10000.0, '%8.2f'), (10000.0, 100000.0, '%8.1f'), ]
[docs] def array_float_8(ndarray: np.ndarray) -> np.ndarray: """Vectorized print_float_8 for arrays — bins values by magnitude and formats in batches to avoid per-element Python call overhead.""" shape = ndarray.shape flat = ndarray.ravel() n = len(flat) result = np.empty(n, dtype='|U8') abs_val = np.abs(flat) processed = np.zeros(n, dtype=bool) izero = flat == 0.0 result[izero] = ' 0.' processed |= izero pos = flat > 0 neg = flat < 0 for lo, hi, fmt in _POS_BINS_8: mask = pos & (flat >= lo) & (flat < hi) if not np.any(mask): continue indices = np.where(mask)[0] result[indices] = [((fmt % v).strip(' 0') or '0.').rjust(8) for v in flat[indices]] processed[indices] = True for lo, hi, fmt in _NEG_BINS_8: mask = neg & (abs_val >= lo) & (abs_val < hi) if not np.any(mask): continue indices = np.where(mask)[0] result[indices] = [((fmt % v).replace('-0.', '-.').strip(' 0') or '-.').rjust(8) for v in flat[indices]] processed[indices] = True remaining = np.where(~processed)[0] for idx in remaining: result[idx] = print_float_8(flat[idx]) return result.reshape(shape)