Saturday, December 10, 2022
HomeData ScienceGoodbye Boring PLAXIS Output with Python | by Philip Tsang | Dec,...

Goodbye Boring PLAXIS Output with Python | by Philip Tsang | Dec, 2022


PLAXIS AUTOMATION SERIES

Step-by-step information towards automation

Photograph by Mika Baumeister on Unsplash

In case you are a frequent PLAXIS person like me, you could have requested this query: why on earth I must hold repeating this copy and paste motion to extract outcomes?

PLAXIS, with little question, is a robust FE software program. As Geotechnical engineers, we wish to deal with the evaluation and spend much less time on post-processing. Python gives probably the most in depth prospects towards automating output extraction and visualisation, because of the modules developed by the open supply communities. Some widespread modules embrace:

  • Pandas
  • Xlsxwriter
  • Matplotlib
  • Seaborn

The final word aim of this tutorial is to point out you learn how to use Python script to extract outputs from a structural ingredient in PLAXIS 2D. This may be achieved with the next 4 steps.

  1. Use IDE to open PLAXIS Output
  2. Set up exterior module to PLAXIS setting
  3. Extract Plate outcomes from PLAXIS
  4. Export as excel spreadsheet

This tutorial requires the readers have VS Code and PLAXIS setting put in. Observe the directions from the article under for those who’re new to this web page. Let’s get began!

I used a PLAXIS 2D mannequin revealed by the official Bentley web site as proven under.

Within the hyperlink, obtain the command log file from V22.01. To the very best of writer’s information, the Python instructions have modified considerably from V21 to V22. Therefore, be sure you received PLAXIS V22 or above (V22.01 and V22.02) put in to make sure the Python script on this tutorial works correctly.

Screenshot by the writer

Upon getting run the command utilizing “Run instructions…”, it is best to have the ability to see the next geometry.

Screenshot from PLAXIS by the writer

This instance fashions the development of an excavation which consists of a diaphragm wall (Plate ingredient). Our aim is to extract the outcomes of the plate ingredient.

Calculate the mannequin and we’re able to extract the outcomes utilizing Python.

Not like first tutorial, this time we’ll open a PLAXIS file we simply downloaded and therefore the code for beginning the server must be barely modified. You’ll need to save lots of the calculated mannequin in a most well-liked working listing.

My location of the PLAXIS file:

C:UsersphtsangDesktopCurrent_ProjectsLinkedInBlog_2Excavation

I’ll first create an empty python script known as it “open_output.py”.

After that, we write the code under to the python file. The code is similar to the one from first tutorial however this time we will probably be opening an exisiting PLAXIS file. With a view to do that, we made the next three modifications:

  1. Since this time we have to open a PLAXIS file, the working listing of the file must be specified which is saved as “FILE_PATH”.
  2. We then open the file utilizing PLAXIS command “s_i.open(FILE_PATH)”.
  3. To open PLAXIS output, we write “g_i.view(g_i.Phases[1])” which permits us to view the outcomes of Phase_1’s calculation.
from plxscrpting.straightforward import *
import subprocess, time
import os

###############################################
PLAXIS_PATH = r'C:Program FilesBentleyGeotechnicalPLAXIS 2D CONNECT Version V22Plaxis2DXInput.exe' # Specify PLAXIS path on server.
FILE_PATH = r'C:UsersphtsangDesktopCurrent_ProjectsLinkedInBlog_2Excavation' # Specify PLAXIS file location and identify
PORT_i = 10000 # Outline a port quantity.
PORT_o = 10001
PASSWORD = 'SxDBR<TYKRAX834~' # Outline a password.

subprocess.Popen([PLAXIS_PATH, f'--AppServerPassword={PASSWORD}', f'--AppServerPort={PORT_i}'], shell=False) # Begin the PLAXIS distant scripting service.
time.sleep(5) # Anticipate PLAXIS besides earlier than sending instructions to the scripting service.

# Begin the scripting server.
# world g_i, s_i
s_i, g_i = new_server('localhost', PORT_i, password=PASSWORD)
s_o, g_o = new_server('localhost', PORT_o, password=PASSWORD)

s_i.open(FILE_PATH)

g_i.gotostages()
g_i.view(g_i.Phases[1])

Screenshot from VS Code by the writer

To check that the code above is working correctly, we’ll run “open_output.py” in terminal as proven under. Kind “python open_output.py” and click on Enter.

Screenshot from VS Code by the writer

Each PLAXIS 2D enter and output apps must be opened robotically. From the output app, it is best to see “SERVER ACTIVE on port 10001”.

Screenshot from PLAXIS by the writer

We now have the PLAXIS API related, we’re prepared to maneuver to the following step.

When utilizing the PLAXIS scripting setting it is not uncommon to make use of Python modules permitting for extending the performance of a Python script. Since our aim is to extract outcomes from PLAXIS and export to excel spreadsheet, we’d like two exterior modules:

These modules may be put in by PLAXIS command immediate window as proven within the steps under. Earlier than continuing, be sure you have gone by Step 1 with PLAXIS Enter opened.

Go to menu choice Professional > Python > Command immediate. This may open the Home windows Command immediate with the PLAXIS set as the present setting.

Screenshot from PLAXIS by the writer

Change listing to the PLAXIS Python Distribution folder. This may be achieved with the next command:

pushd C:ProgramDataBentleyGeotechnicalPLAXIS Python Distribution V2pythonLibsite-packages
Screenshot from PLAXIS by the writer

Within the Command immediate now you can set up any new Python modules utilizing the command proven within the instance under.

As an illustration, if you wish to set up the pandas module:

python -m pip set up pandas
Screenshot from PLAXIS by the writer

Additionally, set up the xlsxwriter module:

python -m pip set up xlsxwriter

Now that now we have put in all of the required modules. We will then extract outcomes from PLAXIS utilizing Python script.

Our predominant aim Step 3 is to extract 4 outcome sorts for the plate ingredient (Diaphragm wall) within the final part (Third excavation stage [Phase_5]). The outcome sorts we’ll extract are:

  • Y Coordinates
  • Shear
  • Bending Second
  • Axial Power

In Step 4, we’ll then export the outcomes to excel spreadsheet with the columns proven under.

Screenshot from excel by the writer

First, we create an empty python file and name it “output_plate.py”.

Import Modules and Strat Server

We’d like to verify the API is related, in addition to import the modules we put in earlier.

from plxscripting.straightforward import *
import subprocess, time
import math
import pandas as pd
import os
import xlsxwriter

###############################################
PORT_i = 10000 # Outline a port quantity.
PORT_o = 10001
PASSWORD = 'SxDBR<TYKRAX834~' # Outline a password.

# Begin the scripting server.
s_i, g_i = new_server('localhost', PORT_i, password=PASSWORD)
s_o, g_o = new_server('localhost', PORT_o, password=PASSWORD)

Outline File Identify

Since we have to export the outcomes to excel, we’d like the specify the exported file location and its identify.

File location: be certain that there may be double slash () after your working listing

File identify: may be any identify you need

EXCEL_PATH=r'C:UsersphtsangDesktopPLAXIS_V22Script'
EXCEL_NAME='plate_output.xlsx'

FILENAME=EXCEL_PATH+EXCEL_NAME

Enter Definition

After that, we’ll enter the structural ingredient and the corresponding part we wish to extract. On this case: ‘Plate_1’ and ‘Third excavation stage [Phase_5]’.

The code beneath is to seek out the present plate components and phases within the mannequin and retailer them as listing.

plate_input=['Plate_1'] # Enter by person
phase_input=['Third excavation stage [Phase_5]'] # Enter by person

###############################################
#Exisiting mannequin ingredient:

plate=[plt for plt in g_o.Plates[:]] # Loop by the plate object in current mannequin
part=[p for p in g_o.Phases[:]] # Loop by all obtainable phases

Two necessary ideas right here:

For loop: the motion of discovering current components in PLAXIS requires for loop. This can be a quite common command in programming. For instance in “plate”, now we have:

for plt in g_o.Plates[:]

This line means we’re looping by all of the plate components in “g_o.Plates[:]” (which is how PLAXIS retailer objects) and identify it as “plt”.

Checklist: As talked about in first tutorial, it is not uncommon to retailer values in an inventory which is represented by a sq. bracket [ ]. The syntax has barely modified in comparison with first tutorial because it has been simplified.

This line:

plate=[plt for plt in g_o.Plates[:]]

Could be prolonged as:

plate=[]

for plt in g_o.Plates[:]

plate.append(plt)

Each code will give the identical outcome however the first one is far neater which is often utilized in Python.

To date, your script ought to appear to be:

Screenshot from VS Code by the writer

After the pre-processing as talked about above, we are able to begin extracting plate outcomes. To try this, we’ll outline a perform to extract outcomes, name is “get_plate()”. Few key factors right here:

Y coordinate = ResultTypes.Plate.Y

Shear = ResultTypes.Plate.Q2D

Bending Second = ResultTypes.Plate.M2D

Axial Power = ResultTypes.Plate.Nx2D

  • The following few traces are to create column names as specified within the goal spreadsheet.
  • Outcomes are then saved as “outcomes”. Right here I introduce a brand new information sort known as Dictionary in Python which is represented by curly bracket { }. Dictionary is especially helpful whenever you wish to create a desk, it incorporates a key-value pair. For instance:

‘Y’ is the key of dictionary

plateY is the worth of dictionary. An array of values may be saved to the desired key.

  • As soon as the dictionary is created, the following line converts dictionary to dataframe. Dataframe is one in all most typical information construction we use in Python. You possibly can interpret it as a desk in excel. That is achieved by operating command: “pd.DataFrame(outcomes)”
  • I additionally kind the outcomes utilizing Y coordinate in a descending order

Ensure that the indentation is right as proven within the screenshot under.

def get_plate(plate_o,phase_o):

plateY=g_o.getresults(plate_o,phase_o,g_o.ResultTypes.Plate.Y, "node")
plateQ=g_o.getresults(plate_o,phase_o,g_o.ResultTypes.Plate.Q2D, "node")
plateM=g_o.getresults(plate_o,phase_o,g_o.ResultTypes.Plate.M2D, "node")
plateAxial=g_o.getresults(plate_o,phase_o,g_o.ResultTypes.Plate.Nx2D, "node")

phasename=str(phase_o.Identification).cut up('[')[0]
col1='Shear [kN]'+'_'+phasename
col2='Bending Second [kNm/m]'+'_'+phasename
col3='Axial Power [kN]'+'_'+phasename

outcomes = {'Y': plateY, col1: plateQ,col2: plateM,col3: plateAxial}

plateresults=pd.DataFrame(outcomes)
plateresults = plateresults.sort_values(by=['Y'],ascending=False)

return plateresults

Screenshot from VS Code by the writer

Lastly, we’ll export the dataframe we created to excel.

We create a perform, name it “export_excel()”.

  • author is to make use of the xlsxwriter module we put in to export excel
  • identify & sheet_name is the place I create a worksheet identify with format “Plate_1_Phase 5”.
  • Then, I exploit the perform “get_plate()” we created in earlier to generate the dataframe and retailer as “outcomes”. It must be a dataframe which might then be export to excel utilizing pandas command “outcomes.to_excel()”.
def export_excel(plate_input,phase_input,filename):
author = pd.ExcelWriter(filename, engine='xlsxwriter')

identify=str(part[5].Identification).cut up(' [')[1]
identify=identify.cut up(']')[0]
sheet_name = "%s_percents" % (plate[0].Identify, identify)
outcomes = get_plate(plate[0], part[5])
outcomes.to_excel(author,sheet_name=sheet_name,index=False)
author.save()

export_excel(plate_input,phase_input,FILENAME)

Screenshot from VS Code by the writer

Run the script with the next.

(PLAXIS) C:UsersphtsangDesktopPLAXIS_V22Script>python output_plate.py

You will note the outcomes we search for have been robotically extracted as proven within the command window.

Screenshot from PLAXIS by the writer

Now for those who open the excel spreadsheet within the location you specified earlier, you may see now we have extracted the Y coordinate, Shear, Bending second and Axial pressure as we would like.

Screenshot from excel by the writer

Nicely achieved! You might have simply extracted outcomes from PLAXIS utilizing Python script.

That’s all for the second tutorial on extracting PLAXIS output utilizing Python. By now, it is best to have the ability to extract output for a single structural ingredient from PLAXIS. In future tutorials, I’ll present learn how to extract outcomes for a number of structural components and completely different construction sorts comparable to interfaces and embedded beams.

For those who get pleasure from studying such a content material, be at liberty to observe my web page. I’ll hold posting this sequence of tutorials on automating PLAXIS with Python. Aside from that, I’m additionally eager to share information on learn how to use Python to automate workflow in engineering.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments