Techno Blender
Digitally Yours.

Start Using Python to Automate PLAXIS | by Philip Tsang | Dec, 2022

0 50


PLAXIS AUTOMATION SERIES

Step-by-step guide toward automation

Photo by Hitesh Choudhary on Unsplash

PLAXIS is a Finite Element modelling software that geotechnical engineers will definitely come across at a certain point in their careers. Modelling complex geotechnical problems in a PLAXIS can be challenging and represents time-consuming modelling work often involving repetitive tasks.

As an engineer, we always want to automate the boring and engineer the awesome.

In this context, programming, more specifically Python, is the best available tool to automate repetitive tasks.

Python has been around for many years but only recently get recognised by the larger community because of its ease of use, large extent of applications and support by huge open-source communities. It is a great programming language for data science, workflow automation and web development.

PLAXIS has a well-developed Application Programming Interface (API) which allows users to operate PLAXIS through programming. More details can be seen on the official Bentley website.

Although the PLAXIS Python API has been developed for a long time, there is still a lack of thorough guidelines to introduce this technique to geotechnical engineers.

This tutorial assumes readers have zero experience in programming so I will show how to set up the programming environment from scratch.

Disclaimer

There have been a lot of paid courses which teach engineers to use the built-in Python editor in PLAXIS which I agree is quite well-developed. But I personally prefer to use my own programming environment to write scripts because of the following three reasons:

  1. A well-developed Integrated Development Environment (IDE) is more user-friendly for coding as it picks up wrong syntaxes and identifies bugs, which is crucial for beginners.
  2. Built-in Python editor has less flexibility to import user-developed modules. In the future when you have more programming experience, you will find that a lot of time we need to separate out the functions we write as a module which makes our script less busy. This can be well managed in IDE.
  3. We don’t want to keep the scripts to ourselves but to share among colleagues. IDE provides perfect integration with code-sharing platforms such as GitHub, GitLab etc.

The ultimate goal of this tutorial is to show you how to use Python script to create your first structural element in PLAXIS 2D. This can be done with the following four steps.

  1. Install Integrated Development Environment (IDE)
  2. Activate PLAXIS Environment
  3. Connect to PLAXIS API
  4. Create first plate element

Lastly, this tutorial assumes readers are using Windows OS and have PLAXIS 2D V22 installed. So let’s get started!

IDE is a software application which provides comprehensive facilities for programming, usually consisting of source code editor, build automation tools and debugger.

There are various options for IDE, such as Visual Studio Code (VS Code), PyCharm, Jupyter Notebook etc, which have no difference in functionality and are completely up to users’ preference for the choice of IDE. This instruction demonstrates the installation of VS Code.

Install Visual Studio Code (VS Code) with the following instruction:

Download the latest VS code for Windows: https://code.visualstudio.com/download

Screenshot by the author

Follow the installation wizard, and check the options as shown below.

Screenshot by the author

Once installed, open VS Code on Desktop. You will see the following interface.

Screenshot from VS Code by the author

Add your work folder in Explorer by right clicking Explorer -> Add Folder to Workplace…

Screenshot from VS Code by the author

You have now got VS code set-up and is ready to start coding!

Once your virtual environment and IDE are set up, you can start writing Python script within IDE.

To write a Python script, you should follow the process below:

  • Open IDE terminal
  • Direct to your preferred work folder (in terminal)
  • Activate PLAXIS Python Environment
  • Create .py file in the folder

Open IDE terminal

Click the “warning” icons in the bottom menu bar:

Screenshot from VS Code by the author

You will see the Shell window comes up:

Screenshot from VS Code by the author

Head to “Terminal” tab in the top menu bar as indicated. You should see that you are in the work folder.

Screenshot from VS Code by the author

Direct to your preferred work folder

If it is not the correct work folder. You can type:

cd <your work folder path>

For example, I am currently at Current Projects folder and I want to locate to 00_Digitalisation->01_Python-FE. The folder path is:

C:\Users\phtsang\Desktop\Current_Projects\00_Digitalisation\01_Python-FE

In IDE terminal:

Screenshot from VS Code by the author

It should show the correct folder name as shown above.

Activate PLAXIS Python Environment

To activate the PLAXIS Python environment, we will need to select PLAXIS built-in python interpreter which contains all the functionalities required to operate PLAXIS. The python interpreter is automatically installed when PLAXIS is installed.

Location of the Python interpreter:

C:\ProgramData\Bentley\Geotechnical\PLAXIS Python Distribution V2\python\python.exe

To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). Click “Enter interpreter path…”, then browse the python.exe in the location mentioned above.

Screenshot from VS Code by the author

In the Terminal tab, select Command Prompt in the drop down list as highlighted below.

Screenshot from VS Code by the author

You should see (PLAXIS) in front of the working directory if the environment is activated successfully.

Screenshot from VS Code by the author

Create .py file in the folder

Click the New file icon:

Screenshot from VS Code by the author

Create .py file as below:

Screenshot from VS Code by the author

Next section we will start connecting to the PLAXIS API.

I will first create an empty python script and call it “create_plate.py” as shown below.

Screenshot from VS Code by the author

After that, we write the code below to the python file. Three things are doing here:

  1. Import modules from PLAXIS Python environment. In order to operate PLAXIS with Python script, we need to use the functions (more commonly known as methods in terms of programming) developed by PLAXIS. The module we import is called “plxscripting.east”
  2. Open PLAXIS 2D desktop application. The path of the Plaxis2DXInput.exe should be the same as listed below if default installation path is adopted. If that’s not the case, simply change the path to the correct directory. Port number and Password can stay as default but you can select any values you want.
  3. Start the PLAXIS scripting server with the pre-defined port number and password.
from plxscripting.easy import *
import subprocess, time

PLAXIS_PATH = r'C:\Program Files\Bentley\Geotechnical\PLAXIS 2D CONNECT Edition V22\\Plaxis2DXInput.exe' # Specify PLAXIS path on server.

PORT_i = 10000 # Define a port number.
PORT_o = 10001

PASSWORD = 'SxDBR<TYKRAX834~' # Define a password (up to user choice).

subprocess.Popen([PLAXIS_PATH, f'--AppServerPassword={PASSWORD}', f'--AppServerPort={PORT_i}'], shell=False) # Start the PLAXIS remote scripting service.

time.sleep(5) # Wait for PLAXIS to boot before sending commands to the scripting service.

# Start 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)

s_i.new()

g_i.gotostructures()

Screenshot from VS Code by the author

To test that the code above is working properly, we will run “create_plate.py” in the terminal as shown below. Type “python create_plate.py” and click Enter.

(PLAXIS) C:\Users\phtsang\Desktop\PLAXIS_V22\Script>python create_plate.py
Screenshot from VS Code by the author

The PLAXIS 2D app should be opened automatically. If the server is configured correctly, you should see “SERVER ACTIVE” as shown below.

Screenshot from PLAXIS by the author

Now that we connect to the PLAXIS API server, we can then create a plate element in PLAXIS using Python script.

In this part of tutorial, we aim to create a plate element with positive and negative interfaces. Then we assign “Concrete” material to the element.

First, we define the material name and geometry. I plan to create a line from (-10,0) to (10,0). The square bracket [ ] is called list in python, which is a very common data type to store values. To access the values stored in a list, it is called in the following syntax:

If I want to access the coordinates of first point, I use

x coordinate = -10 = first_point[0]

y coordinate = 10 = first_point[1]

0 and 1 here called index which always starts from 0.

Then we create the material and assign it to the plate material list. To create plate material, the command “g_i.platemat()” is used. The code underneath is to assign the plate object to a list.

material=['Concrete']
first_point=[-10,0]
second_point=[10,0]

# Create material
for i in range(len(material)):
g_i.platemat('Identification',material[i])

platematerials = [mat for mat in g_i.Materials[:] if mat.TypeName.value == 'PlateMat']

After that, we create plate element with “g_i.plate()” based on the pre-defined coordinates.

plate=g_i.plate(
(first_point[0],first_point[1]),
(second_point[0],second_point[1]),
)
plate1=plate[-1]

Then, create interfaces using “g_i.posinterface()” and “g_i.neginterface()”.

plate2=plate[-2]
g_i.posinterface(plate2)
g_i.neginterface(plate2)

Finally, set “Concrete” to the plate element.

plate1.setmaterial(platematerials[0])

If you rerun the script, you will see the following is created in PLAXIS. As you see, the “Concrete” material is created and assigned to the plate element we created.

Screenshot from PLAXIS by the author

Congratulations! You have just created your first plate element in PLAXIS using Python!

Of course, we often need to create more than one structural element in PLAXIS. In this section, we aim to create four plate elements which form a rectangle with the following coordinates:

  • (-10,0)
  • (-10,10)
  • (10,10)
  • (10,0)

We need to first create a list (call it ‘df_plate’) which specifies the coordinates of each plate element. The first and second points are stored as (x1,y1) and (x2,y2) respectively. Within the list, I used a new data type called Dictionary which is very commonly used in Python. More on that in the next tutorial.

df_plate=[{'x1':-10,'y1':0,'x2':10,'y2':0},{'x1':-10,'y1':10,'x2':-10,'y2':0},{'x1':-10,'y1':10,'x2':10,'y2':10},{'x1':10,'y1':10,'x2':10,'y2':0}]

Since this time we need to access multiple elements in a list, we will utilise a concept called for loop. It allows us to loop through the elements and create points in PLAXIS accordingly. This can be done with the following code:

for i in range(len(df_plate)):
plate=g_i.plate(
(df_plate[i]['x1'],df_plate[i]['y1']),
(df_plate[i]['x2'],df_plate[i]['y2']),
)
plate1=plate[-1]

Your final script should look like:

# Plate
#Material name and geometry
material=['Concrete']

df_plate=[{'x1':-10,'y1':0,'x2':10,'y2':0},{'x1':-10,'y1':10,'x2':-10,'y2':0},{'x1':-10,'y1':10,'x2':10,'y2':10},{'x1':10,'y1':10,'x2':10,'y2':0}]

# Create material
for i in range(len(material)):
g_i.platemat('Identification',material[i])

platematerials = [mat for mat in g_i.Materials[:] if mat.TypeName.value == 'PlateMat']

#Create Plate
for i in range(len(df_plate)):
plate=g_i.plate(
(df_plate[i]['x1'],df_plate[i]['y1']),
(df_plate[i]['x2'],df_plate[i]['y2']),
)
plate1=plate[-1]

#Create Interface

plate2=plate[-2]
g_i.posinterface(plate2)
g_i.neginterface(plate2)
#Set Material

plate1.setmaterial(platematerials[0])

If you rerun the script, you will see the following is created in PLAXIS. Now, you are able to create multiple elements using Python script.

Screenshot from PLAXIS by the author

That’s the end of the first tutorial on automating PLAXIS with Python. By now, you should be able to create structural elements in PLAXIS using Python script. In the future, I will keep posting tutorials on using Python in PLAXIS. More than that, I’m also keen to share knowledge on how to use Python to automate boring workflow in engineering.

If you’re interested in hearing more about Python, PLAXIS and workflow automation, feel free to follow my page.


PLAXIS AUTOMATION SERIES

Step-by-step guide toward automation

Photo by Hitesh Choudhary on Unsplash

PLAXIS is a Finite Element modelling software that geotechnical engineers will definitely come across at a certain point in their careers. Modelling complex geotechnical problems in a PLAXIS can be challenging and represents time-consuming modelling work often involving repetitive tasks.

As an engineer, we always want to automate the boring and engineer the awesome.

In this context, programming, more specifically Python, is the best available tool to automate repetitive tasks.

Python has been around for many years but only recently get recognised by the larger community because of its ease of use, large extent of applications and support by huge open-source communities. It is a great programming language for data science, workflow automation and web development.

PLAXIS has a well-developed Application Programming Interface (API) which allows users to operate PLAXIS through programming. More details can be seen on the official Bentley website.

Although the PLAXIS Python API has been developed for a long time, there is still a lack of thorough guidelines to introduce this technique to geotechnical engineers.

This tutorial assumes readers have zero experience in programming so I will show how to set up the programming environment from scratch.

Disclaimer

There have been a lot of paid courses which teach engineers to use the built-in Python editor in PLAXIS which I agree is quite well-developed. But I personally prefer to use my own programming environment to write scripts because of the following three reasons:

  1. A well-developed Integrated Development Environment (IDE) is more user-friendly for coding as it picks up wrong syntaxes and identifies bugs, which is crucial for beginners.
  2. Built-in Python editor has less flexibility to import user-developed modules. In the future when you have more programming experience, you will find that a lot of time we need to separate out the functions we write as a module which makes our script less busy. This can be well managed in IDE.
  3. We don’t want to keep the scripts to ourselves but to share among colleagues. IDE provides perfect integration with code-sharing platforms such as GitHub, GitLab etc.

The ultimate goal of this tutorial is to show you how to use Python script to create your first structural element in PLAXIS 2D. This can be done with the following four steps.

  1. Install Integrated Development Environment (IDE)
  2. Activate PLAXIS Environment
  3. Connect to PLAXIS API
  4. Create first plate element

Lastly, this tutorial assumes readers are using Windows OS and have PLAXIS 2D V22 installed. So let’s get started!

IDE is a software application which provides comprehensive facilities for programming, usually consisting of source code editor, build automation tools and debugger.

There are various options for IDE, such as Visual Studio Code (VS Code), PyCharm, Jupyter Notebook etc, which have no difference in functionality and are completely up to users’ preference for the choice of IDE. This instruction demonstrates the installation of VS Code.

Install Visual Studio Code (VS Code) with the following instruction:

Download the latest VS code for Windows: https://code.visualstudio.com/download

Screenshot by the author

Follow the installation wizard, and check the options as shown below.

Screenshot by the author

Once installed, open VS Code on Desktop. You will see the following interface.

Screenshot from VS Code by the author

Add your work folder in Explorer by right clicking Explorer -> Add Folder to Workplace…

Screenshot from VS Code by the author

You have now got VS code set-up and is ready to start coding!

Once your virtual environment and IDE are set up, you can start writing Python script within IDE.

To write a Python script, you should follow the process below:

  • Open IDE terminal
  • Direct to your preferred work folder (in terminal)
  • Activate PLAXIS Python Environment
  • Create .py file in the folder

Open IDE terminal

Click the “warning” icons in the bottom menu bar:

Screenshot from VS Code by the author

You will see the Shell window comes up:

Screenshot from VS Code by the author

Head to “Terminal” tab in the top menu bar as indicated. You should see that you are in the work folder.

Screenshot from VS Code by the author

Direct to your preferred work folder

If it is not the correct work folder. You can type:

cd <your work folder path>

For example, I am currently at Current Projects folder and I want to locate to 00_Digitalisation->01_Python-FE. The folder path is:

C:\Users\phtsang\Desktop\Current_Projects\00_Digitalisation\01_Python-FE

In IDE terminal:

Screenshot from VS Code by the author

It should show the correct folder name as shown above.

Activate PLAXIS Python Environment

To activate the PLAXIS Python environment, we will need to select PLAXIS built-in python interpreter which contains all the functionalities required to operate PLAXIS. The python interpreter is automatically installed when PLAXIS is installed.

Location of the Python interpreter:

C:\ProgramData\Bentley\Geotechnical\PLAXIS Python Distribution V2\python\python.exe

To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). Click “Enter interpreter path…”, then browse the python.exe in the location mentioned above.

Screenshot from VS Code by the author

In the Terminal tab, select Command Prompt in the drop down list as highlighted below.

Screenshot from VS Code by the author

You should see (PLAXIS) in front of the working directory if the environment is activated successfully.

Screenshot from VS Code by the author

Create .py file in the folder

Click the New file icon:

Screenshot from VS Code by the author

Create .py file as below:

Screenshot from VS Code by the author

Next section we will start connecting to the PLAXIS API.

I will first create an empty python script and call it “create_plate.py” as shown below.

Screenshot from VS Code by the author

After that, we write the code below to the python file. Three things are doing here:

  1. Import modules from PLAXIS Python environment. In order to operate PLAXIS with Python script, we need to use the functions (more commonly known as methods in terms of programming) developed by PLAXIS. The module we import is called “plxscripting.east”
  2. Open PLAXIS 2D desktop application. The path of the Plaxis2DXInput.exe should be the same as listed below if default installation path is adopted. If that’s not the case, simply change the path to the correct directory. Port number and Password can stay as default but you can select any values you want.
  3. Start the PLAXIS scripting server with the pre-defined port number and password.
from plxscripting.easy import *
import subprocess, time

PLAXIS_PATH = r'C:\Program Files\Bentley\Geotechnical\PLAXIS 2D CONNECT Edition V22\\Plaxis2DXInput.exe' # Specify PLAXIS path on server.

PORT_i = 10000 # Define a port number.
PORT_o = 10001

PASSWORD = 'SxDBR<TYKRAX834~' # Define a password (up to user choice).

subprocess.Popen([PLAXIS_PATH, f'--AppServerPassword={PASSWORD}', f'--AppServerPort={PORT_i}'], shell=False) # Start the PLAXIS remote scripting service.

time.sleep(5) # Wait for PLAXIS to boot before sending commands to the scripting service.

# Start 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)

s_i.new()

g_i.gotostructures()

Screenshot from VS Code by the author

To test that the code above is working properly, we will run “create_plate.py” in the terminal as shown below. Type “python create_plate.py” and click Enter.

(PLAXIS) C:\Users\phtsang\Desktop\PLAXIS_V22\Script>python create_plate.py
Screenshot from VS Code by the author

The PLAXIS 2D app should be opened automatically. If the server is configured correctly, you should see “SERVER ACTIVE” as shown below.

Screenshot from PLAXIS by the author

Now that we connect to the PLAXIS API server, we can then create a plate element in PLAXIS using Python script.

In this part of tutorial, we aim to create a plate element with positive and negative interfaces. Then we assign “Concrete” material to the element.

First, we define the material name and geometry. I plan to create a line from (-10,0) to (10,0). The square bracket [ ] is called list in python, which is a very common data type to store values. To access the values stored in a list, it is called in the following syntax:

If I want to access the coordinates of first point, I use

x coordinate = -10 = first_point[0]

y coordinate = 10 = first_point[1]

0 and 1 here called index which always starts from 0.

Then we create the material and assign it to the plate material list. To create plate material, the command “g_i.platemat()” is used. The code underneath is to assign the plate object to a list.

material=['Concrete']
first_point=[-10,0]
second_point=[10,0]

# Create material
for i in range(len(material)):
g_i.platemat('Identification',material[i])

platematerials = [mat for mat in g_i.Materials[:] if mat.TypeName.value == 'PlateMat']

After that, we create plate element with “g_i.plate()” based on the pre-defined coordinates.

plate=g_i.plate(
(first_point[0],first_point[1]),
(second_point[0],second_point[1]),
)
plate1=plate[-1]

Then, create interfaces using “g_i.posinterface()” and “g_i.neginterface()”.

plate2=plate[-2]
g_i.posinterface(plate2)
g_i.neginterface(plate2)

Finally, set “Concrete” to the plate element.

plate1.setmaterial(platematerials[0])

If you rerun the script, you will see the following is created in PLAXIS. As you see, the “Concrete” material is created and assigned to the plate element we created.

Screenshot from PLAXIS by the author

Congratulations! You have just created your first plate element in PLAXIS using Python!

Of course, we often need to create more than one structural element in PLAXIS. In this section, we aim to create four plate elements which form a rectangle with the following coordinates:

  • (-10,0)
  • (-10,10)
  • (10,10)
  • (10,0)

We need to first create a list (call it ‘df_plate’) which specifies the coordinates of each plate element. The first and second points are stored as (x1,y1) and (x2,y2) respectively. Within the list, I used a new data type called Dictionary which is very commonly used in Python. More on that in the next tutorial.

df_plate=[{'x1':-10,'y1':0,'x2':10,'y2':0},{'x1':-10,'y1':10,'x2':-10,'y2':0},{'x1':-10,'y1':10,'x2':10,'y2':10},{'x1':10,'y1':10,'x2':10,'y2':0}]

Since this time we need to access multiple elements in a list, we will utilise a concept called for loop. It allows us to loop through the elements and create points in PLAXIS accordingly. This can be done with the following code:

for i in range(len(df_plate)):
plate=g_i.plate(
(df_plate[i]['x1'],df_plate[i]['y1']),
(df_plate[i]['x2'],df_plate[i]['y2']),
)
plate1=plate[-1]

Your final script should look like:

# Plate
#Material name and geometry
material=['Concrete']

df_plate=[{'x1':-10,'y1':0,'x2':10,'y2':0},{'x1':-10,'y1':10,'x2':-10,'y2':0},{'x1':-10,'y1':10,'x2':10,'y2':10},{'x1':10,'y1':10,'x2':10,'y2':0}]

# Create material
for i in range(len(material)):
g_i.platemat('Identification',material[i])

platematerials = [mat for mat in g_i.Materials[:] if mat.TypeName.value == 'PlateMat']

#Create Plate
for i in range(len(df_plate)):
plate=g_i.plate(
(df_plate[i]['x1'],df_plate[i]['y1']),
(df_plate[i]['x2'],df_plate[i]['y2']),
)
plate1=plate[-1]

#Create Interface

plate2=plate[-2]
g_i.posinterface(plate2)
g_i.neginterface(plate2)
#Set Material

plate1.setmaterial(platematerials[0])

If you rerun the script, you will see the following is created in PLAXIS. Now, you are able to create multiple elements using Python script.

Screenshot from PLAXIS by the author

That’s the end of the first tutorial on automating PLAXIS with Python. By now, you should be able to create structural elements in PLAXIS using Python script. In the future, I will keep posting tutorials on using Python in PLAXIS. More than that, I’m also keen to share knowledge on how to use Python to automate boring workflow in engineering.

If you’re interested in hearing more about Python, PLAXIS and workflow automation, feel free to follow my page.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment