Source code for pyNastran.bdf.field_writer_16

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

import numpy as np
from numpy import float32, isnan  # type: ignore

from pyNastran.utils.numpy_utils import integer_types
from pyNastran.bdf.cards.utils import wipe_empty_fields
from pyNastran.bdf.field_writer_8 import set_blank_if_default


[docs] def set_string16_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 '%16s' % val
_POS_BINS_16 = [ (0.001, 1.0, '%16.15f'), (1.0, 10.0, '%16.14f'), (10.0, 100.0, '%16.13f'), (100.0, 1000.0, '%16.12f'), (1000.0, 10000.0, '%16.11f'), (10000.0, 100000.0, '%16.10f'), (100000.0, 1000000.0, '%16.9f'), (1000000.0, 10000000.0, '%16.8f'), (10000000.0, 100000000.0, '%16.7f'), (100000000.0, 1000000000.0, '%16.6f'), (1000000000.0, 10000000000.0, '%16.5f'), (10000000000.0, 100000000000.0, '%16.4f'), (100000000000.0, 1000000000000.0, '%16.3f'), (1000000000000.0, 10000000000000.0, '%16.2f'), (10000000000000.0, 100000000000000.0, '%16.1f'), ] _NEG_BINS_16 = [ (0.01, 1.0, '%16.14f'), (1.0, 10.0, '%16.13f'), (10.0, 100.0, '%16.12f'), (100.0, 1000.0, '%16.11f'), (1000.0, 10000.0, '%16.10f'), (10000.0, 100000.0, '%16.9f'), (100000.0, 1000000.0, '%16.8f'), (1000000.0, 10000000.0, '%16.7f'), (10000000.0, 100000000.0, '%16.6f'), (100000000.0, 1000000000.0, '%16.5f'), (1000000000.0, 10000000000.0, '%16.4f'), (10000000000.0, 100000000000.0, '%16.3f'), (100000000000.0, 1000000000000.0, '%16.2f'), (1000000000000.0, 10000000000000.0, '%16.1f'), ]
[docs] def array_float_16(ndarray: np.ndarray) -> np.ndarray: """Vectorized print_float_16 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='|U16') 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_16: 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(16) for v in flat[indices]] processed[indices] = True for lo, hi, fmt in _NEG_BINS_16: 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(16) for v in flat[indices]] processed[indices] = True remaining = np.where(~processed)[0] for idx in remaining: result[idx] = print_float_16(flat[idx]) return result.reshape(shape)