I'm working on a script processing computed strains/deformations for multiple nodes of a finite element analysis (FEA). There are multiple solutions for different, positive input torques. I want to extrapolate this data and simulate the results for negative torques. Whilst doing that the original data changes.
As we are not directly changing any of the original values I think it must be accessed by reference through the function extrapolating the FEA. I've been trying copy.deepcopy
, but figured over multiple threads that this does not copy class structures. In other threads it was recommended to inherit but I'm struggling adapting it to my case.
The following code is inside the class containing all nodes on the same radius of the object. All nodes are in a list self._nodes
sorted by their angle. Each node has the strains for all torque levels.
class RadialNodeContainer:
def __init__(self, radius):
self._r = radius
self._nodes = []
def compute_negatives_radial_container(self): # ... see below
class Node:
def __init__(self, node_id, x, y, z,
strain_0nm, strain_100nm, strain_204nm, strain_369nm):
self._coordinate = Coordinate(x, y, z)
self._torque_levels = [strain_0nm, strain_100nm,
strain_204nm, strain_369nm]
def get_all_strains_complete(self):
return copy.deepcopy(self._torque_levels)
class Strain:
def __init__(self, torque_nm, exx, exy, eyy):
self._torque = torque_nm
self._exx = exx
self._exy = exy
self._eyy = eyy
The function which causes unwanted changes the original data:
def compute_negatives_radial_container(self):
points_per_360deg = len(self._nodes)
jj = points_per_360deg
corresponding_node_strains = None
for ii in range(points_per_360deg):
jj -= 1
# Mistake is expected below here
corresponding_node_strains = copy.deepcopy(
self._nodes[jj].get_all_strains_complete())
for kk in range(len(corresponding_node_strains)):
torque = corresponding_node_strains[kk].get_torque_level()
if torque != 0:
exx, exy, eyy = corresponding_node_strains[kk].get_raw_strains()
calculated_negative_strain = Strain(torque_nm=-torque,
exx=exx,
exy=-exy,
eyy=eyy)
self._nodes[ii].add_torque_level(calculated_negative_strain)
I thought of creating a deepcopy
of the list of strain elements (Node -> self._torque_level
). Originally this list looks like [Strain(0Nm), Strain(100Nm), ...]
. But I can't wrap my head around what part(s) of the code I have to adapt to allow passing a copy of the class instances.