PLAXIS AUTOMATION SERIES
Step-by-step information towards automation
The PLAXIS Output app gives the opportunity of utilizing the built-in plotting device to match outputs at completely different phases. Though it supplies some fundamental plot features which permit customers to visualise outcomes, folks usually go along with Excel because it permits flexibility in plot formatting, information manipulation and outcomes sharing.
With little doubt, Excel is a robust and well-known software program for information visualisation. Although, PLAXIS doesn’t present full integration with Excel, making the extraction and visualisation course of time-consuming. There are three potential areas of enchancment when it comes to information visualisation:
- Mix all information into one worksheet for plotting
- Automate the method of choosing information sequence in Excel when creating plots
- Create plots with customised sequence names and axis titles
This tutorial goals to increase the lesson learnt from the 2nd tutorial. We’ll extract output from a number of phases and use plots to match the outcomes. This may be achieved with the next three steps:
- Set up exterior module to PLAXIS atmosphere
- Extract outcomes for a number of phases
- Evaluate outcomes at completely different phases utilizing plots in Excel
- Formatting with Openpyxl
As earlier than, this tutorial requires the readers have VS Code and PLAXIS atmosphere put in. Observe the directions from the article beneath when you’re new to this web page. Let’s get began!
We’ll use the identical PLAXIS 2D mannequin from 2nd tutorial. Observe the directions beneath to arrange and calculate the mannequin when you haven’t achieved so.
Just like the final tutorial, we are going to open the PLAXIS file utilizing “open_output.py” such that the API server will be enabled.
As a recap, we use the next code to open the file:
After working the code above, each PLAXIS 2D enter and output apps must be opened robotically. From the output app, it’s best to see “SERVER ACTIVE on port 10001”.
Our purpose is to extract outcomes from PLAXIS and create comparability plots in excel. So as to take action, we’d like three exterior modules:
The method of putting in exterior modules via PLAXIS command immediate has been described intimately within the 2nd tutorial. Ensure you have gone via the method earlier than continuing with the next steps.
From the final tutorial, each pandas and xlsxwriter must be put in. Word that Python modules are model delicate, it’s because some modules have connections to different modules and therefore model updates could trigger malfunctions. That is important to have constant module dependencies within the PLAXIS atmosphere.
The variations of modules I’ve used on this tutorial are as follows:
pandas == 1.5.2
xlsxwriter == 3.0.3
openpyxl == 3.0.9
defusedxml == 0.7.1
To examine the present model of module, we are able to use the next command within the PLAXIS command immediate:
python -m pip present pandas
If the variations should not constant, we are able to use the next command to improve/downgrade the model:
python -m pip set up --upgrade pandas==1.5.2
Set up the remainder of the modules:
python -m pip set up xlsxwriter==3.0.3
python -m pip set up openpyxl==3.0.9
python -m pip set up defusedxml==0.7.1
In some conditions, chances are you’ll encounter an error when putting in defusedxml
ERROR: Couldn't set up packages on account of an OSError: [WinError 5] Entry is denied
If that’s your case, attempt the next code:
python -m pip set up --upgrade defusedxml==0.7.1 --user
Now that we’ve got put in all of the required modules. We will now able to extract outcomes from completely different phases in PLAXIS utilizing Python script.
The primary purpose of Step 2 is to extract outcomes (Bending second) from three phases within the ‘Excavation’ mannequin. We’ll then export the outcomes to Excel in a worksheet known as ‘combined_Plate_1’ with the columns proven beneath:
First, we create an empty python file and name it “plot_plate_combine.py”.
Import Modules and Begin Server
from plxscripting.straightforward import *
import math
import pandas as pd
from openpyxl import load_workbook
from openpyxl.chart import (
ScatterChart,
Reference,
Sequence,
)
###############################################
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 Title
File location: C:UsersphtsangDesktopPLAXIS_V22Script
File identify: will be any identify you need
EXCEL_PATH=r'C:UsersphtsangDesktopPLAXIS_V22Script'
EXCEL_NAME='Plate_y.xlsx'FILENAME=EXCEL_PATH+EXCEL_NAME
Enter Definition
On this case, we are going to extract the bending second for ‘Plate_1’ from the phases beneath:
- ‘Set up of strut [Phase_3]’
- ‘Second (submerged) excavation stage [Phase_4]’
- ‘Third excavation stage [Phase_5]’
###############################################
#Inputs:
plate_input=['Plate_1']
phase_input=['Installation of strut [Phase_3]','Second (submerged) excavation stage [Phase_4]','Third excavation stage [Phase_5]']
Your script ought to appear to be:
Consequence Extraction
After pre-processing, we are going to outline a perform to extract bending second (a simplified model from final tutorial) and name it “get_plate()”.
def get_plate(plate_o,phase_o):plateY=g_o.getresults(plate_o,phase_o,g_o.ResultTypes.Plate.Y, "node")
plateM=g_o.getresults(plate_o,phase_o,g_o.ResultTypes.Plate.M2D, "node")
phasename=str(phase_o.Identification).break up('[')[0]
col1='Bending Second [kNm/m]'+'_'+phasename
outcomes = {'Y': plateY,col1: plateM}
plateresults=pd.DataFrame(outcomes)
plateresults = plateresults.sort_values(by=['Y'],ascending=False)
return plateresults
Export to Excel
In contrast to final tutorial, we need to extract outcomes from a number of phases as a substitute of a single part. Therefore, the “export_excel()” perform must be modified. Three actions are concerned:
- Loop via the identify of the present part from the present mannequin
- Cross-check with the enter of the part by the consumer (i.e. Section 3, 4 and 5).
- Extract outcomes in the event that they match and export to particular person worksheet (e.g. Plate_1_Phase_3)
- Mix the outcomes into a brand new Dataframe and export to a worksheet (‘combined_Plate_1’)
These actions will be achieved with the next code. Let’s break it down!
def export_excel(plate_input,phase_input,filename):
author = pd.ExcelWriter(filename, engine='xlsxwriter')
mixed=[]
for i in vary(len(part)):
for j in vary(len(phase_input)):
if part[i].Identification == phase_input[j]:
identify=str(part[i].Identification).break up(' [')[1]
identify=identify.break up(']')[0]
sheet_name = "%s_percents" % (plate[0].Title, identify)
outcomes = get_plate(plate[0], part[i])
mixed.append(outcomes)
outcomes.to_excel(author,sheet_name=sheet_name,index=False)combinedsheet='mixed'+'_'+str(plate[0].Title)
mixed=pd.concat(mixed, axis=1)
mixed.to_excel(author,sheet_name=combinedsheet,index=False)
author.save()
export_excel(plate_input,phase_input,FILENAME)
- We have to first outline an empty checklist ‘mixed=[ ]’. This permits us to append the extracted outcomes at every part right into a single checklist such that we are able to concatenate them within the final step.
- Subsequent step is to loop via the present phases of the mannequin and see which one matches our enter. That is achieved utilizing PLAXIS command ‘part[i].Identification’ which supplies the complete identify of every part.
for i in vary(len(part)):
for j in vary(len(phase_input)):
if part[i].Identification == phase_input[j]:
- The next code is for naming objective. For instance, ‘part[0].Identification’ will give ‘Set up of strut [Phase_3]’. I need to get ‘Phase_3’ which is sitting between the squared bracket [ ]. Then, I mix ‘Plate_1’ and ‘Phase_3’ to type the worksheet identify.
identify=str(part[i].Identification).break up(' [')[1]
identify=identify.break up(']')[0]
sheet_name = "%s_percents" % (plate[0].Title, identify)
- The bending second of PLate_1 at every part is extracted utilizing ‘get_plate()’ perform and saved as outcomes. The outcomes are then appended to the ‘mixed’ checklist via the tactic ‘mixed.append(outcomes)’. Lastly, use ‘outcomes.to_excel(author,sheet_name=sheet_name,index=False)’ to export outcomes at every part to particular person worksheet.
outcomes = get_plate(plate[0], part[i])
mixed.append(outcomes)
outcomes.to_excel(author,sheet_name=sheet_name,index=False)
- Final 4 strains intention to mix the leads to ‘mixed’ checklist and export to’combined_Plate_1′ worksheet. That is achieved by pandas’s methodology ‘concat()’. You will need to enter ‘axis=1’ as an argument because it tells pandas to mix the outcomes horizontally (by rows). By default, ‘axis=0’ combines the outcomes vertically (by columns).
combinedsheet='mixed'+'_'+str(plate[0].Title
mixed=pd.concat(mixed, axis=1)
mixed.to_excel(author,sheet_name=combinedsheet,index=False)
author.save())
Your last script ought to appear to be:
Now that we’ve got extracted outcomes from all phases, we are able to then proceed with creating plots in Excel utilizing Python.
Lastly, we are going to study Openpyxl which is a module that enables us to create Excel plot.
We create a perform, name it “get_combined_plot()”.
- df_inter shops the dataframe obtained from the ‘combined_Plate_1’. sheet is the place I specify the worksheet ‘combined_Plate_1’ such that I can create plot there.
def get_combined_plot(filename,sheetname):
df_inter = pd.read_excel(filename, sheet_name = sheetname,engine="openpyxl")
wb=load_workbook(filename)
sheet=wb[sheetname]
- Then, create a chart object known as ‘chart1’ which is a scatter chart. After that, we assign axis titles utilizing openpyxl strategies (x_axis.title and y_axis.title).
- yvalue shops the Y coordinates of the outcomes utilizing methodology ‘Reference()’.
- place & prow is the place I specify the situation of the plot.
chart1=ScatterChart()
chart1.x_axis.title = 'Bending Second (kNm/m)'
chart1.y_axis.title = 'RL (m)'
chart={'chart1':chart1}
yvalue=Reference(sheet,min_col=1,min_row=2,max_row=len(df_inter)+1)
place='G'
prow=1
- Then, extract the bending second values from second column and retailer them in worth.
- As soon as we’ve got information for x and y axis, we use ‘Sequence()’ to create a sequence and assign it to ‘chart1’ utilizing ‘chart1.sequence.append(sequence)’.
if df_inter.columns.values[1].break up(' [')[0] == 'Bending Second'
worth=Reference(sheet,min_col=2,min_row=2,max_row=len(df_inter)+1)
sequence=Sequence(yvalue,worth,title=checklist(df_inter.columns.values)[1])chart1.sequence.append(sequence):
- The code beneath is principally for formatting which set plot kinds resembling top, tick field and legend place and so on (like typical excel plot setting). Extra formatting particulars are outlined within the official documentation:
charts='chart1'
chart[charts].top=15
chart[charts].y_axis.tickLblPos = 'low'
chart[charts].legend.place = 'b'if ord(place)<89 and prow<=2:
sheet.add_chart(chart[charts], place+str(1))
place=chr(ord(place)+10)
prow=prow+1
wb.save(filename)
- The steps above created plot utilizing the primary two columns (i.e. ‘Y’ and ‘Bending Second [kNm/m]_Installation of strut ‘)
- Last step is to loop via the remainder of the columns and add them as two further sequence to the present plot.
- With the intention to establish if the columns comprise Y coordinate or Bending second, we have to use if-else assertion.
- If the column title is ‘Y’, it’ll retailer the column values to yworth (i.e. y axis). If the column title comprises ‘Bending Second’, it’ll retailer column values to worth (i.e. x axis).
for i in vary(3,len(df_inter.columns)+1):
if df_inter.columns.values[i-1].break up('.')[0] != 'Y':
if df_inter.columns.values[i-1].break up(' [')[0] == 'Bending Second':
worth=Reference(sheet,min_col=i,min_row=2,max_row=len(df_inter)+1)
sequence=Sequence(yvalue,worth,title=checklist(df_inter.columns.values)[i-1])
chart1.sequence.append(sequence)
elif df_inter.columns.values[i-1].break up('.')[0] == 'Y':
yvalue=Reference(sheet,min_col=i,min_row=2,max_row=len(df_inter)+1)
wb.save(filename)
The ultimate script is proven beneath:
def get_combined_plot(filename,sheetname):
df_inter = pd.read_excel(filename, sheet_name = sheetname,engine="openpyxl")
wb=load_workbook(filename)
sheet=wb[sheetname]chart1=ScatterChart()
chart1.x_axis.title = 'Bending Second (kNm/m)'
chart1.y_axis.title = 'RL (m)'
chart={'chart1':chart1}
xvalue=Reference(sheet,min_col=1,min_row=2,max_row=len(df_inter)+1)
place='G'
prow=1
if df_inter.columns.values[1].break up(' [')[0] == 'Bending Second':
worth=Reference(sheet,min_col=2,min_row=2,max_row=len(df_inter)+1)
sequence=Sequence(xvalue,worth,title=checklist(df_inter.columns.values)[1])
chart1.sequence.append(sequence)
charts='chart1'
chart[charts].top=15
chart[charts].y_axis.tickLblPos = 'low'
chart[charts].legend.place = 'b'
if ord(place)<89 and prow<=2:
sheet.add_chart(chart[charts], place+str(1))
place=chr(ord(place)+10)
prow=prow+1
wb.save(filename)
for i in vary(3,len(df_inter.columns)+1):
if df_inter.columns.values[i-1].break up('.')[0] != 'Y':
if df_inter.columns.values[i-1].break up(' [')[0] == 'Bending Second':
worth=Reference(sheet,min_col=i,min_row=2,max_row=len(df_inter)+1)
sequence=Sequence(xvalue,worth,title=checklist(df_inter.columns.values)[i-1])
chart1.sequence.append(sequence)
elif df_inter.columns.values[i-1].break up('.')[0] == 'Y':
xvalue=Reference(sheet,min_col=i,min_row=2,max_row=len(df_inter)+1)
wb.save(filename)
combinedsheet='combined_Plate_1'
get_combined_plot(FILENAME,combinedsheet)
Run the script with the next.
(PLAXIS) C:UsersphtsangDesktopPLAXIS_V22Script>python plot_plate_combine.py
Now when you open the excel spreadsheet within the location you specified earlier and go to ‘combined_Plate_1’ worksheet. You possibly can see we’ve got extracted the Y coordinate and Bending second for all three phases. Extra importantly, we’ve got a plot that comprises all outcomes which permit us to match the bending second at completely different phases!
Improbable! You’ve efficiently extracted outcomes from PLAXIS and used them to create a plot in Excel utilizing Python.
That’s all for the third tutorial on PLAXIS output visualisation utilizing Python. By now, it’s best to have the ability to extract outcomes at a number of phases and create plots in excel for end result comparability. This may be additional prolonged to incorporate a number of structural components at numerous levels, which suggests the entire output extraction course of will be automated. I’ll additional speak about this in future tutorials.
In case you get pleasure from studying such a content material, be happy to observe my web page. I’ll maintain posting this sequence of tutorials on automating PLAXIS with Python. Other than that, I’m additionally eager to share information on tips on how to use Python to automate workflow in engineering.