Skip to content

Commit

Permalink
bdf_vectorized3:
Browse files Browse the repository at this point in the history
 - turned off automatic sorting, which screws up slicing coord_ids that are consistent with an element; sorting is not the standard case
 - adding SPCOFF/SPCOFf1
 - more __apply_slice__, geom_check, and _save methods
 -
  • Loading branch information
SteveDoyle2 committed Oct 8, 2023
1 parent 3548929 commit faee64a
Show file tree
Hide file tree
Showing 18 changed files with 755 additions and 273 deletions.
6 changes: 4 additions & 2 deletions pyNastran/bdf/cards/elements/bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""
# pylint: disable=R0904,R0902,E1101,E1103,C0111,C0302,C0103,W0101
from __future__ import annotations
from typing import Optional, Any, TYPE_CHECKING
from typing import cast, Optional, Any, TYPE_CHECKING

import numpy as np
from numpy.linalg import norm
Expand All @@ -23,6 +23,7 @@
double)
from pyNastran.bdf.field_writer_8 import print_card_8
from pyNastran.bdf.field_writer_16 import print_card_16
from pyNastran.bdf.cards.coordinate_systems import CORD2R
if TYPE_CHECKING: # pragma: no cover
from pyNastran.nptyping_interface import NDArray3float, NDArray33float
from cpylog import SimpleLogger
Expand All @@ -32,7 +33,7 @@
class LineElement(Element): # CBAR, CBEAM, CBEAM3, CBEND
def __init__(self):
Element.__init__(self)
self.pid_ref = None # type: Optional[Any]
self.pid_ref: Optional[Any] = None
#self.nodes_ref = None

def C(self):
Expand Down Expand Up @@ -1938,6 +1939,7 @@ def rotate_v_wa_wb(model: BDF, elem,
# end A
# global - cid != 0
if cd1 != 0:
cd1_ref = cast(CORD2R, cd1_ref)
v = cd1_ref.transform_node_to_global_assuming_rectangular(v)
elif offt_vector == 'B':
# basic - cid = 0
Expand Down
4 changes: 2 additions & 2 deletions pyNastran/dev/bdf_vectorized3/bdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2618,8 +2618,8 @@ def add_card(cls, card, comment=''):
'SPC': partial(self._prepare_card, self.spc),
'SPC1': partial(self._prepare_card, self.spc1),
'SPCADD' : partial(self._prepare_card, self.spcadd),
'SPCOFF' : partial(self._prepare_card, self.spcoff),
'SPCOFF1' : partial(self._prepare_card, self.spcoff1),
'SPCOFF' : partial(self._prepare_card_by_method, self.spcoff.add_set_card),
'SPCOFF1' : partial(self._prepare_card_by_method, self.spcoff.add_set1_card),

# pseudo-constraint
'SUPORT': partial(self._prepare_card_by_method, self.suport.add_set_card),
Expand Down
3 changes: 2 additions & 1 deletion pyNastran/dev/bdf_vectorized3/bdf_interface/add_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -6918,7 +6918,8 @@ def add_rbar(self, eid: int, nids: list[int],
a comment for the card
"""
elem = self.rbar.add(eid, nids, cna, cnb, cma, cmb, alpha=alpha, tref=tref, comment=comment)
elem = self.rbar.add(eid, nids, cna, cnb, cma, cmb,
alpha=alpha, tref=tref, comment=comment)
return elem

def add_rbar1(self, eid: int, nids: list[int], cb: str,
Expand Down
6 changes: 3 additions & 3 deletions pyNastran/dev/bdf_vectorized3/bdf_interface/bdf_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
from pyNastran.dev.bdf_vectorized3.cards.coord import COORD
from pyNastran.dev.bdf_vectorized3.cards.constraints import (
SPC, SPC1, SPCADD,
SPCOFF, SPCOFF1,
SPCOFF, # SPCOFF1,
MPC, MPCADD)
from pyNastran.dev.bdf_vectorized3.cards.elements.rigid import (
RBAR, RBAR1,
Expand Down Expand Up @@ -454,7 +454,7 @@ def __init__(self):
self.spc = SPC(self)
self.spc1 = SPC1(self)
self.spcoff = SPCOFF(self)
self.spcoff1 = SPCOFF1(self)
#self.spcoff1 = SPCOFF1(self)
self.spcadd = SPCADD(self)
self.mpc = MPC(self)
self.mpcadd = MPCADD(self)
Expand Down Expand Up @@ -539,7 +539,7 @@ def __init__(self):
self.dti: dict[str, DMI] = {}
self._dmig_temp = defaultdict(list) # type: dict[str, list[str]]
# ----------------------------------------
self.suport1 = {}
#self.suport1 = {}
#self.suport = []
#self.suport = SUPORT(self)

Expand Down
1 change: 1 addition & 0 deletions pyNastran/dev/bdf_vectorized3/bdf_interface/write_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,7 @@ def _write_constraints(self, bdf_file: TextIOLike,
model.spcadd.write_file(bdf_file, size=size, is_double=is_double)
model.spc.write_file(bdf_file, size=size, is_double=is_double)
model.spc1.write_file(bdf_file, size=size, is_double=is_double)
model.spcoff.write_file(bdf_file, size=size, is_double=is_double)

if any((card.n for card in model.mpc_cards)):
bdf_file.write('$MPCs\n')
Expand Down
43 changes: 30 additions & 13 deletions pyNastran/dev/bdf_vectorized3/cards/base_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,12 @@ def remove_duplicates(self, inplace: bool=True) -> Any:
card = self
self.__apply_slice__(card, i)
else:
card = self.slice_card_by_index(i)
card = self.slice_card_by_index(i, sort_ids=True)
return card

def slice_card_by_id(self, ids: np.ndarray,
assume_sorted: bool=True) -> Any:
assume_sorted: bool=True,
sort_ids: bool=False) -> Any:
"""uses a node_id to extract Elements, Properties, etc.
Parameters
Expand All @@ -242,10 +243,16 @@ def slice_card_by_id(self, ids: np.ndarray,
the ids to extract (should be a subset of elem.element_id)
assume_sorted: bool; default=True
assume the parent array (e.g., elem.element_id is sorted)
sort_ids: bool; default=False
True: sort the input ids
False: output is unsorted, which could cause issues
"""
i = self.index(ids, assume_sorted=assume_sorted)
cls_obj = self.slice_card_by_index(i) # , assume_sorted=assume_sorted)
cls_obj = self.slice_card_by_index(i, sort_index=sort_ids) # , assume_sorted=assume_sorted)
#if 1:
#assert np.array_equal(self._ids, np.unique(self._ids))
#ids = np.atleast_1d(np.asarray(ids, dtype=self._ids.dtype))
return cls_obj

def remove_card_by_id(self, ids: np.ndarray) -> Any:
Expand All @@ -259,6 +266,7 @@ def remove_card_by_id(self, ids: np.ndarray) -> Any:
#return card

def slice_card_by_index(self, i: np.ndarray,
sort_index: bool=False,
#assume_sorted: bool=True,
) -> Any:
"""
Expand All @@ -268,16 +276,22 @@ def slice_card_by_index(self, i: np.ndarray,
----------
i : (n,) int array
the indices to extract (should be a subset of elem.element_id)
assume_sorted: bool; default=True
assume the parent array (e.g., elem.element_id is sorted)
sort_index: bool; default=False
True: sort the input ids (i)
False: output is unsorted, which could cause issues
#assume_sorted: bool; default=True
#assume the parent array (e.g., elem.element_id is sorted)
"""
assert self.n > 0, self
self_ids = self._ids
assert len(self_ids), self_ids
i = np.atleast_1d(np.asarray(i, dtype=self_ids.dtype))
i.sort()
imax = i[-1]
if sort_index:
i.sort()
imax = i[-1]
else:
imax = i.max()
imax_allowable = len(self_ids) - 1
#if imax_allowable == -1:
#imax_allowable = 0
Expand Down Expand Up @@ -442,13 +456,14 @@ def __init__(self, model: BDF):
self.n: int = 0
self.element_id: np.ndarray = np.array([], dtype='int32')

def slice_card_by_element_id(self, element_id: np.ndarray) -> Element:
def slice_card_by_element_id(self, element_id: np.ndarray,
sort_ids: bool=False) -> Element:
assert self.n > 0, self.n
assert len(self.element_id) > 0, self.element_id
i = self.index(element_id)
#cls_obj = cls(self.model)
#cls_obj.__apply_slice__(self, i)
cls_obj = self.slice_card_by_index(i)
cls_obj = self.slice_card_by_index(i, sort_index=sort_ids)
assert cls_obj.n > 0, cls_obj
return cls_obj

Expand All @@ -461,11 +476,12 @@ def __init__(self, model: BDF):
self.n = 0
self.property_id: np.ndarray = np.array([], dtype='int32')

def slice_card_by_property_id(self, property_id: np.ndarray) -> Property:
def slice_card_by_property_id(self, property_id: np.ndarray,
sort_ids: bool=False) -> Property:
assert self.n > 0, self.n
assert len(self.property_id) > 0, self.property_id
i = self.index(property_id)
cls_obj = self.slice_card_by_index(i)
cls_obj = self.slice_card_by_index(i, sort_index=sort_ids)
assert cls_obj.n > 0, cls_obj
return cls_obj

Expand All @@ -478,10 +494,11 @@ def __init__(self, model: BDF):
self.n = 0
self.material_id: np.ndarray = np.array([], dtype='int32')

def slice_card_by_material_id(self, material_id: np.ndarray) -> Material:
def slice_card_by_material_id(self, material_id: np.ndarray,
sort_ids: bool=False) -> Material:
assert self.n > 0, self.n
i = self.index(material_id)
cls_obj = self.slice_card_by_index(i)
cls_obj = self.slice_card_by_index(i, sort_index=sort_ids)
assert cls_obj.n > 0, cls_obj
return cls_obj

Expand Down
69 changes: 62 additions & 7 deletions pyNastran/dev/bdf_vectorized3/cards/bdf_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,68 @@ def _save(self,
#self.__apply_slice__(grid, i)
#return grid

def __apply_slice__(self, grid: SUPORT, i: np.ndarray) -> None:
self._slice_comment(grid, i)
grid.n = len(i)
grid.suport_id = self.suport_id[i]
grid.node_id = self.node_id[i]
grid.component = self.component[i]

def index(self, ids: np.ndarray,
assume_sorted: bool=True,
check_index: bool=True,
inverse: bool=False) -> np.ndarray:
"""
Parameters
----------
ids: (n,) int array
the node/element/property/material/etc. ids
assume_sorted: bool; default=True
assume the parent array (e.g., elem.element_id is sorted)
check_index: bool; default=True
validate the lookup
inverse: bool; default=False
False: get the indices for the ids
True: get the inverse indices for the ids
Returns
-------
index: (n,) int array
the indicies in the node_ids array (or other array)
Example
-------
>>> all_ids = [1, 2, 3, 4, 5]
>>> all_index = [0, 1, 2, 3, 4]
>>> ids = [3, 4]
>>> index(all_ids, ids, inverse=False)
[2, 3]
>>> index(all_ids, ids, inverse=True)
[0, 1, 4]
"""
if not assume_sorted:
self.sort()
self_ids = self._ids
assert len(self_ids) > 0, f'{self.type}: {self._id_name}={self_ids}'
if ids is None:
return None # np.arange(len(ids), dtype='int32')
ids = np.atleast_1d(np.asarray(ids, dtype=self_ids.dtype))

ielem = np.array([
ii for ii, suport_idi in enumerate(ids)
if suport_idi in self.suport_id])
#ielem = np.searchsorted(self_ids, ids)

if check_index:
actual_ids = self_ids[ielem]
if not np.array_equal(actual_ids, ids):
raise KeyError(f'{self.type}: expected_{self._id_name}={ids}; actual_{self._id_name}={actual_ids}')
if inverse:
i = np.arange(len(self_ids), dtype=self_ids.dtype)
index = np.setdiff1d(i, ielem)
return index
return ielem

def __apply_slice__(self, suport: SUPORT, i: np.ndarray) -> None:
self._slice_comment(suport, i)
suport.n = len(i)
suport.suport_id = self.suport_id[i]
suport.node_id = self.node_id[i]
suport.component = self.component[i]

def geom_check(self, missing: dict[str, np.ndarray]):
nid = self.model.grid.node_id
Expand Down
Loading

0 comments on commit faee64a

Please sign in to comment.