<jupyter_start><jupyter_text>Connecting Python and ANSYS by PARANSYSIn this example the APDL script `BeckCantileverAPDL.inp` is used on ANSYS by PARANSYS. The input variables from the script are `q,l,b,h` and the ouput is the stress on a cantilever beam (`stress`). Importing all that will be used:<jupyter_code>import paransys
import numpy as np
import pandas as pd
from IPython.display import display<jupyter_output><empty_output><jupyter_text>Setup:First we need to find the ANSYS executable location (`ansloc`) and define the working directory that ANSYS will use (`workingdir`). I'm using ANSYS 2019 R2 and the working dir is at `C:\Temp\wk`.<jupyter_code>ansloc = 'C:\\Program Files\\ANSYS Inc\\v194\\ansys\\bin\\winx64\\ansys194.exe'
workingdir = 'C:\\Temp\\wk'
# Create ans object that is ANSYS
ans = paransys.ANSYS(exec_loc=ansloc, run_location=workingdir, jobname='file', nproc=4, override=True, cleardir=False, add_flags='')
# Activate the output to Python
ans.Info(True)<jupyter_output>ANSYS properties defined as:
   Executable file: "C:\Program Files\ANSYS Inc\v194\ansys\bin\winx64\ansys194.exe".
   Working directory: "C:\Temp\wk".
   Jobname: "file".
   Number of processors used: "4".
   Override lock file: "True".
   Clear working directory: "False".
   Additional flags: "".
Now the commands will send a return to Python (like this).<jupyter_text>Set APDL model that will be used:Here it's at the current directory and have no extra files, so we can do:<jupyter_code>ans.SetModel(inputname='BeckCantileverAPDL.inp', extrafiles=[], directory='.')<jupyter_output>Input script file and extra files copied to working directory.
   Main APDL script: "BeckCantileverAPDL.inp".
   Extra model files: "[]".
   Input directory: ".".<jupyter_text>Setting parameters that will be setted by Python and the parameters that ANSYS should return:In this case the input parameters are `q,l,b,h` and the ouput `stress`.<jupyter_code># Set input parameters
ans.CreateVarIn('q')
ans.CreateVarIn('l')
ans.CreateVarIn('b')
ans.CreateVarIn('h')

# Set output parameters
ans.CreateVarOut('stress')<jupyter_output>ANSYS input variable "Q" created.
ANSYS input variable "L" created.
ANSYS input variable "B" created.
ANSYS input variable "H" created.
Variable "STRESS" declared as ANSYS output variable.<jupyter_text>Setting analysis lenght:The number of analysis that will be executed should be defined before setting the values, with this PARANSYS can know if the values are in the righ size.In this case the lenght is 3 analysis.<jupyter_code>ans.SetLength(3)<jupyter_output>Analysis lenght set to 3.<jupyter_text>The model is now set. If you keep the model, variables and lenght that part doesn't need to change anymore. The values could be set as many times as you need, this will be done now. Setting vlaues, running and postprocessing:The 3 analysis that will be done here are used to determine the derivatives of stress in relation to l and h by the finite difference method: $$\frac{\partial stress}{\partial l} \space\space\space and \space\space\space \frac{\partial stress}{\partial h}$$The values that parameters will assume in the three simualtions are:| Sim\Var |  q   |   l  |  b  | h   ||---------|------|------|-----|-----|| Sim 1  | 1.15 | 60.0 | 4.0 | 1.0 || Sim 2  | 1.15 | 62.0 | 4.0 | 1.0 || Sim 3  | 1.15 | 60.0 | 4.0 | 1.2 |<jupyter_code># Creatting arrays with the values
# Values at (Sim#1, Sim#2, Sim#3)
q = np.array([1.15,  1.15,  1.15])
l = np.array([60.0,  62.0,  60.0])
b = np.array([4.00,  4.00,  4.00])
h = np.array([1.00,  1.00,  1.20])

# Passing it to ANSYS
ans.SetVarInValues('q', q)
ans.SetVarInValues('l', l)
ans.SetVarInValues('b', b)
ans.SetVarInValues('h', h)<jupyter_output>Values of "Q" were set.
Values of "L" were set.
Values of "B" were set.
Values of "H" were set.<jupyter_text>Running:<jupyter_code>ans.Run()<jupyter_output>Deleting old error log file.
Writing the pdsrun.inp file.
Saving the pdsrun.inp file.
Writing the current.samp file.
Running ANSYS.
Solution is done. It took 0.071076 minutes.<jupyter_text>Acessing the results:The results will by imported as a dictionary<jupyter_code>results = ans.GetVarOutValues()<jupyter_output>Importing results from PDS results file ("C:\Temp\wk\file_current.pdrs").<jupyter_text>We can print all results using print(), print just `stress` or another whings.The output parameters always came in UPPER CASE because of ANSYS is made in FORTRAN77 (I think it's because of that!)<jupyter_code># Print all results
print(f'All results: {results}\n')

# Print just stresses
print(f'Just stresses: {results["STRESS"]}\n')<jupyter_output>All results: {'ERR': array([0., 0., 0.]), 'STRESS': array([3103.706543, 3314.068848, 2155.351807])}

Just stresses: [3103.706543 3314.068848 2155.351807]<jupyter_text>We can make a Pandas DataFrame with all inputs and results!<jupyter_code>df = pd.DataFrame({'q': q, 'l': l, 'b': b, 'h': h, 'stress': results['STRESS']})
#print(df) # Console
display(df) # On Jupyter<jupyter_output><empty_output><jupyter_text>Derivatives:The finite difference method could be described as: $$\frac{\partial g(x)}{\partial x} = \frac{g(x+dh)-g(x)}{dh}$$The values in the past simulations are:- First simulation = g(q,l,b,h)- Second simulation = g(q,l+dh,b,h), being dh=2.0- Third simulation = g(q,l,b,h+dh), being dh=0.2So with the first and second lines we can determine $\frac{\partial stress}{\partial l}$ as:<jupyter_code>dSdl = (df['stress'][1]-df['stress'][0])/(df['l'][1]-df['l'][0])
print(f'dStress/dl = {dSdl}')<jupyter_output>dStress/dl = 105.18115250000005<jupyter_text>And with the first and third lines we can determine $\frac{\partial stress}{\partial h}$ as:<jupyter_code>dSdh = (df['stress'][2]-df['stress'][0])/(df['h'][2]-df['h'][0])
print(f'dStress/dh = {dSdh}')<jupyter_output>dStress/dh = -4741.77368
{'fraction_non_alphanumeric': 0.13719093406593408, 'fraction_numerical': 0.03588598901098901, 'mean_word_length': 5.036269430051814, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '233017', 'license': 'MIT', 'n_tokens_mistral': 2164, 'n_tokens_neox': 2005, 'n_words': 648, 'path': 'examples/ANSYS-Cantilever.ipynb', 'repo_name': 'dutitello/parAnsys', 'size': '5824'}