# Relative deformation on path - Example

Member, Moderator, Employee Posts: 859
✭✭✭✭
edited June 2023

User creates manually a directional or total deformation plot on a path:

The result curve is as follows:

They would like to extract the plot data values, create a linear interpolation between the first and last value and get the relative displacement as: relativeDef = extractedDef - linearRegressionDef

Tagged:

• Member, Moderator, Employee Posts: 859
✭✭✭✭

Below script will do just that :

```import wbjn
import units
import os

def GetLinearRegression(ps,pv):
x1=ps[0]
x2=ps[-1]
y1=pv[0]
y2=pv[-1]

a=(y2-y1)/(x2-x1)
b=y1-a*x1

return a,b

def GetPathCurvAbscissa(px,py,pz):
import math

ps=[]
# Retrieve curvilinear abscissa. Paths are straight lines,
# so retrieve length from end points

length=sqrt((px[-1]-px[0])**2+(py[-1]-py[0])**2+(pz[-1]-pz[0])**2)
# now get abscissa
for i in range(len(px)):
l=sqrt((px[i]-px[0])**2+(py[i]-py[0])**2+(pz[i]-pz[0])**2)
ps.append(l)

return ps,length

# Retrieve all deformation results
all_res=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.DeformationResult)

path_res=[]
# Check if results are scoped on a path
for res in all_res:
if res.ScopingMethod==Ansys.Mechanical.DataModel.Enums.GeometryDefineByType.Path:
if res.Suppressed==False:
path_res.append(res)

if len(path_res)>0: # Results scoped on path have been found
# Retrieve user_files folder
cmd = 'returnValue(GetUserFilesDirectory())'
user_dir = wbjn.ExecuteCommand(ExtAPI,cmd)

# Write data to 'relative_path_data.csv' in project's user_files folder
f=open(os.path.join(user_dir,'relative_path_data.csv'),'w')
# Loop over results to find deformation results scoped on paths
for res in path_res:
pdata=res.PlotData.Values

# Retrieve results unit and compute scale_factor to export in project units
currentLengthUnit=ExtAPI.DataModel.CurrentUnitFromQuantityName('Length')
scale_factor=units.ConvertUnit(1.,pdata[3].Unit,currentLengthUnit)

# Split in X, Y, Z and values
pdata_X=[]
pdata_Y=[]
pdata_Z=[]
pdata_Values=[]
for i in range(pdata[3].Count):
pdata_X.append(pdata[0][i]*scale_factor)
pdata_Y.append(pdata[1][i]*scale_factor)
pdata_Z.append(pdata[2][i]*scale_factor)
pdata_Values.append(pdata[3][i]*scale_factor)
# Compute curvilinear abscissa
pdata_S,length=GetPathCurvAbscissa(pdata_X,pdata_Y,pdata_Z)
# linear regression on path values
a,b=GetLinearRegression(pdata_S,pdata_Values)

# Write data to csv file with ',' as separator
f.write('{0},\n'.format(res.Name))