by Philip Chin Fung Tsang
Learn about trends in digital transformation and how they affect the engineering and construction industry.
We all love user-friendly interfaces. PLAXIS Input app is certainly a good example of such an interface. With little training, beginners can easily create a pad footing model.
Though, it is very common to use mouse to perform actions in PLAXIS Input which can be a tedious process if we are working on a complex model. There are three limitations in the current input interface:
This tutorial aims to extend the lesson learned from the 1st tutorial. We will use Excel as an interface to input geometry and material properties and create plate elements in PLAXIS.
As before, this tutorial requires the readers have VS Code and PLAXIS environment installed. Follow the instructions from the article below if you're new to this page.
Also, we will also need pandas installed in the PLAXIS environment. Follow the instructions below to install external modules if you haven't done so.
The ultimate goal of this tutorial is to extract values from an Excel input template and use them to create structural elements in PLAXIS. Therefore, it is important to define the input interface.
We will create an empty excel spreadsheet, call it "Input_param". The interface contains three worksheets:
when you create the worksheets, ensure they follow the same names as shown above as the naming is case-sensitive.
The purpose of the "Geometry" worksheet is to define the coordinates of the structural elements. The values will be used in the "Plates" worksheet in the next section. It includes four columns as shown below:
Y value: assign the actual y coordinates to the corresponding "RL". Similar concept as "X value".
These coordinate definitions will then be used in the "Plates" worksheet.
The purpose of this worksheet is to design the properties of multiple plate elements, including:
X1, Y1, X2 & Y2: Since plate in PLAXIS 2D is a line, it requires coordinates of two points. Last four columns are to specify the coordinates using the definitions from the previous sections.
Once this worksheet is created, we can move to the last worksheet "PlateMatName".
##PlateMatName
This worksheet aims to define the material properties we specify in the previous section. It should be noted that the name entered here needs to be exactly the same as the names given in the "Plates" worksheet.
There are five inputs for the plate properties:
These are typical material properties for PLAXIS 2D, just to ensure all input properties are in the correct unit.
The values used in this example aim to create an excavation model. Make sure you have created the excel spreadsheet with the worksheets and values as outlined above.
Once the input file is created, we are ready to move to the next step.
The main goal of Step 1 is to extract the coordinates definition from the "Geometry" worksheet and store values as dictionary for later stage.
First, we create an empty python file and call it "excel_geometry.py".
Similar to previous tutorial, we will start off by importing relevant modules and start server.
1from plxscripting.easy import * 2import subprocess, time 3import pandas as pd 4import openpyxl 5 6 7############################################### 8PLAXIS_PATH = r'C:\Program Files\Bentley\Geotechnical\PLAXIS 2D CONNECT Edition V22\\Plaxis2DXInput.exe' # Specify PLAXIS path on server. 9PORT_i = 10000 # Define a port number. 10PORT_o = 10001 11PASSWORD = 'SxDBR<TYKRAX834~' # Define a password. 12subprocess.Popen([PLAXIS_PATH, f'--AppServerPassword={PASSWORD}', f'--AppServerPort={PORT_i}'], shell=False) # Start the PLAXIS remote scripting service. 13time.sleep(5) # Wait for PLAXIS to boot before sending commands to the scripting service. 14 15 16# Start the scripting server. 17s_i, g_i = new_server('localhost', PORT_i, password=PASSWORD) 18s_o, g_o = new_server('localhost', PORT_o, password=PASSWORD) 19 20 21s_i.new() 22 23g_i.gotostructures()
File location: C:\Users\phtsang\Desktop\PLAXIS_V22\Python_automation
File name: spreadsheet we created earlier which is "Input_param.xlsx"
Since we want to extract values from "Geometry", we will use "pd.read_excel()" and specify the worksheet we want to read using "sheet_name".
1source=r"C:\Users\phtsang\Desktop\PLAXIS_V22\Python_automation" 2file="Input_param"+".xlsx" 3geomsheet="Geometry" 4df_geom = pd.read_excel(file, sheet_name = geomsheet,engine="openpyxl")
Detailed explanation of dictionary and dataframe can be found in this article: Goodbye Boring PLAXIS Output with Python
Here, I will demonstrate the first approach to read values from dataframe (think of it as an excel table). Below is the table of the "Geometry " worksheet.
Let's say we would like to extract columns A and B and stores as dictionary with "X value" as heading. It can be done with the following steps:
1set_index(df_geom.columns[0])
1geom=[] 2for i in range(2): 3 geom1=df_geom[[df_geom.columns[i*2],df_geom.columns[i*2+1]]].set_index(df_geom.columns[i*2]).to_dict() 4 geom.append(geom1)
The output of geom list looks like:
1[{'X value': {'LDWall': -5.0, 'RDWall': 8.0, 'CentreWall': 1.0}}, {'Y value': {'DWallTop': 9.0, 'DWallBot': -6.0, 'Slab1': 6.5, 'BaseSlab': -1.0}}]
By creating a dictionary, it allows us to assign values by names easily. For example, I can assign -5 to a point with the following code. This is specifically useful when conducting parametric study as we can intuitively know which coordinates to change.
1geom[0]['X value']['LDWall']
Now that we have stored the coordinates, we can then use these values to create structural elements.
In Step 2, we will extract the values from the "Plates" worksheet and create plate elements accordingly.
Generally, the steps are similar to 1st tutorial except this time we need to create elements based on the information provided in the input table. It involves the following actions:
The information involved in the input table is shown below. We will need this to reference our code.
1platesheet="Plates"
2df_plate = pd.read_excel(file, sheet_name = platesheet,engine="openpyxl")
Then, we need to read the values from "Material" column and find the unique material (i.e. RC1000 and RC600). To read values from specific cells, we can use pandas command 'df_plate.iloc[row, column]'. It follows the same rule list data type in which indexes should be used.
For example, if we want to get "RightDWall" and "BaseSlab" from the table. We can use 'df_plate.iloc[1,0]' and 'df_plate.iloc[2,0]'. It should be noted that the first row (heading) is skipped when using iloc. If we want to get all rows, we can write 'df_plate.iloc[:,0]'.
After that, 'dict.fromkeys()' can be used to find the unique values. Combine the code we have.
1material=list(dict.fromkeys(df_plate.iloc[:,2].to_list()))
1for i in range(len(material)) 2 g_i.platemat('Identification',material[i]) 3 4 5platematerials = [mat for mat in g_i.Materials[:] if mat.TypeName.value == 'PlateMat']:
1for i in range(df_plate.count()[0])
We need to create plate using the user-input values with 'g_i.plate((X1,Y1),(X2,Y2))'. See below two examples of using iloc to extract specific cell values.
geom[0]['X value'][df_plate.iloc[0,3]] locates D2 from the table and hence gives -5 (our definition of LDWall)
geom[1]['X value'][df_plate.iloc[0,4]] locates E2 from the table and hence gives 9 (our definition of DWallTop)
Using this logic, we can create the two points of the plate element.
1plate=g_i.plate( 2 (geom[0]['X value'][df_plate.iloc[i,3]],geom[1]['Y value'][df_plate.iloc[i,4]]), 3 (geom[0]['X value'][df_plate.iloc[i,5]],geom[1]['Y value'][df_plate.iloc[i,6]]) 4 )
1plate1=plate[-1] 2plate1.rename(df_plate.iloc[i,0]) 3Use if statement to check if interface is required. If 'Y', use 'g_i.posinterface(plate)' and 'g_i.neginterface(plate)' to create interfaces. 4if df_plate.iloc[i,1] == 'Y': 5 plate2=plate[-2] 6 g_i.posinterface(plate2) 7 g_i.neginterface(plate2)
1plate2.PositiveInterface.rename(df_plate.iloc[i,0]+'_PosInterface')
2 plate2.NegativeInterface.rename(df_plate.iloc[i,0]+'_NegInterface')
1for j in range(len(material)): 2 if df_plate.iloc[i,2] == platematerials[j].Identification: 3 plate1.setmaterial(platematerials[j])
The final script should be as follow:
1platesheet="Plates" 2df_plate = pd.read_excel(file, sheet_name = platesheet,engine="openpyxl") 3 4 5#Material 6material=list(dict.fromkeys(df_plate.iloc[:,2].to_list())) 7 8 9for i in range(len(material)): 10 g_i.platemat('Identification',material[i]) 11 12 13platematerials = [mat for mat in g_i.Materials[:] if mat.TypeName.value == 'PlateMat'] 14 15 16for i in range(df_plate.count()[0]): 17 plate=g_i.plate( 18 (geom[0]['X value'][df_plate.iloc[i,3]],geom[1]['Y value'][df_plate.iloc[i,4]]), 19 (geom[0]['X value'][df_plate.iloc[i,5]],geom[1]['Y value'][df_plate.iloc[i,6]]) 20 ) 21 plate1=plate[-1] 22 plate1.rename(df_plate.iloc[i,0]) 23 if df_plate.iloc[i,1] == 'Y': 24 plate2=plate[-2] 25 g_i.posinterface(plate2) 26 g_i.neginterface(plate2) 27 plate2.PositiveInterface.rename(df_plate.iloc[i,0]+'_PosInterface') 28 plate2.NegativeInterface.rename(df_plate.iloc[i,0]+'_NegInterface') 29 for j in range(len(material)): 30 if df_plate.iloc[i,2] == platematerials[j].Identification: 31 plate1.setmaterial(platematerials[j])
Once the plate elements are created, we can then define our material properties.
In Step 3, we aim to extract the material properties from "PlateMatName" worksheet and assign these properties to the material object (i.e. "RC1000" and "RC600") we created in the previous section.
The information involved in the input table is shown below.
1df_platemat = pd.read_excel(file, sheet_name = platematsheet,engine="openpyxl")
As mentioned under "Excel Input Interface", there are five inputs for plate properties: EA (kN/m), EI (kNm^2/m), Unit weight, w (kN/m/m), Poisson ratio, v (nu) (-) and Prevent punching (Y/N).
We will loop through the rows in the table and use iloc to find each value and assign them to corresponding variables. for i in range(df_platemat.count()[0]):
1 EA = df_platemat.iloc[i,1]
2 EI = df_platemat.iloc[i,2]
3 w = df_platemat.iloc[i,3]
4 nu = df_platemat.iloc[i,4]
5 if df_platemat.iloc[i,5] == 'Y':
6 Punch=True
7 else:
8 Punch=False
1platematerials = [mat for mat in g_i.Materials[:] if mat.TypeName.value == 'PlateMat' 2 3for j in range(len(platematerials)): 4 if df_platemat.iloc[i,0] == platematerials[j].Identification]
1platematerials[j].setproperties("MaterialType","Elastic", "w", w, "EA1", EA, "EI", EI, "StructNu", nu, 'PreventPunching', Punch)
The final script is show below:
1platematsheet="PlateMatName" 2df_platemat = pd.read_excel(file, sheet_name = platematsheet,engine="openpyxl") 3 4 5for i in range(df_platemat.count()[0]): 6 EA = df_platemat.iloc[i,1] 7 EI = df_platemat.iloc[i,2] 8 w = df_platemat.iloc[i,3] 9 nu = df_platemat.iloc[i,4] 10 if df_platemat.iloc[i,5] == 'Y': 11 Punch=True 12 else: 13 Punch=False 14 15 platematerials = [mat for mat in g_i.Materials[:] if mat.TypeName.value == 'PlateMat'] 16 17 for j in range(len(platematerials)): 18 if df_platemat.iloc[i,0] == platematerials[j].Identification: 19 platematerials[j].setproperties("MaterialType","Elastic", "w", w, "EA1", EA, 20 "EI", EI, "StructNu", nu, 'PreventPunching', Punch)
Run the script with the following.
1(PLAXIS) C:\Users\phtsang\Desktop\PLAXIS_V22\Python_automation>python excel_geometry.py
You should see the following is created in PLAXIS 2D. As you see in the Model explorer, all plates we created have the names we specify in the input table.
In terms of material, RC1000 and RC600 have been created with the properties we specified in the table.
Wonderful! We have just created a user interface for PLAXIS 2D.
That's all for the fourth tutorial on interacting with Excel using Python. After this tutorial, you should be able to obtain inputs from excel and create multiple structural elements, as well as assign material properties in PLAXIS. Here you can find all tutorials to keep improving your automation skills:
Tutorial 1: Start Using Python to Automate PLAX
Tutorial 2: Extract PLAXIS results automatically using Python
Tutorial 3: PLAXIS result visualization using Python
Tutorial 4: Create a PLAXIS model based on a Excel sheet using Python
If you're interested in hearing more about Python, PLAXIS and workflow automation, feel free to follow my LinkedIn page. You can also support me by following me on Medium where I will provide source code and more general programming techniques in the future.
Subscribe to our newsletter and get the latest industry insights