Skip to main content

PLAXIS

PLAXIS has a Python API that is distributed in a Python installer. The API can be used to build-up PLAXIS models and process results, integrating the PLAXIS workflow within your VIKTOR app. For more information on the PLAXIS API, see the website.

note

A 'Bentley Geotechnical SELECT Entitlement' (previously 'PLAXIS-vip') license is required

To integrate PLAXIS in your application, the following is needed:

  • A Generic worker
  • A server on which PLAXIS is installed (with the correct licensing)

From within the VIKTOR app, the Python script is sent, via the generic worker, to the server. After the analysis, the generic worker sends back the specified files to the app, after which the developer can process the results (visualize, download, etc.). In the following code snippet two files are sent to the worker: run_plaxis.py and input.json. For additional ideas on how to pass parameters to PLAXIS have a look at the PLAXIS sample application.

from viktor.external.generic import GenericAnalysis

# Generate the input file(s)
files = [
('run_plaxis.py', run_plaxis_file')),
('input.json', File.from_data(b'{parameter_a: 1, parameter_b: 2}'))
]

# Run the analysis and obtain the output file
generic_analysis = GenericAnalysis(files=files, executable_key="plaxis", output_filenames=["output.txt"])
generic_analysis.execute(timeout=60)
output_file = generic_analysis.get_output_file("output.txt")

The file run_plaxis.py and the other file represented as bytes is then sent to the Python distribution PLAXIS is set to work with. You can specify which Python distribution PLAXIS should use, in the "Using a custom Python distribution" section.

The executable_key in the example above refers to the "plaxis" command. This command should also be specified in the configuration file on the server, located in the same directory as the worker:

config.yaml

executables:
plaxis:
path: 'C:\path\to\python.exe'
arguments:
- 'C:\scripts\run_plaxis.py'
workingDirectoryPath: 'C:\scripts'
maxParallelProcesses: 1 # must be one, please do not change

run_plaxis.py

The Python script that is sent to the worker should consist of the following parts:

  • Starting of the PLAXIS remote scripting service
  • Code to build-up the PLAXIS model and read-out the desired results
  • Creation of output files in the working directory of the worker, to be returned to the app
  • Close the PLAXIS application

Starting of the PLAXIS remote scripting service can be done as follows:

import subprocess, time

PLAXIS_PATH = r'C:\Program Files\Bentley\Geotechnical\PLAXIS 2D CONNECT Edition V20\\Plaxis2DXInput.exe' # Specify PLAXIS path on server.
PORT_I = 10000 # Define an input port number.
PORT_O = 10001 # Define an output port number.
PASSWORD = 'secret' # Define a password.
subprocess.Popen([PLAXIS_PATH, f'--AppServerPassword={PASSWORD}', f'--AppServerPort={PORT}']) # Start the PLAXIS remote scripting service.
time.sleep(30) # Wait for PLAXIS to boot before sending commands to the scripting service.

# Start the scripting server.
global 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)

# Execute code.
...

# Extract required data and write files to working directory.
...

# Close the application
g_o.close() # Close the output window
g_i.kill() # Close the input window
caution

The resulting files can be very large. Consider sending back files to the app with only the relevant information, by filtering out unnecessary results, to reduce in size. An example can be found in the sample application

Testing

New in v13.5.0

mock_GenericAnalysis decorator for easier testing of GenericAnalysis

GenericAnalysis.execute needs to be mocked within the context of (automated) testing.

The viktor.testing module provides the mock_GenericAnalysis decorator that facilitate mocking of workers:

import unittest

from viktor import File
from viktor.testing import mock_GenericAnalysis

from app.my_entity_type.controller import MyEntityTypeController

class TestMyEntityTypeController(unittest.TestCase):

@mock_GenericAnalysis(get_output_file={
'result.xml': File.from_path('test_file.xml'), # <name>: <File>
'result.json': File.from_path('test_file.json'),
...
})
def test_generic_analysis(self):
MyEntityTypeController().generic_analysis()

For the decorator's input parameters the following holds:

  • If a Sequence type is provided, the next entry is returned for each corresponding method call. When a call is performed on a depleted iterable, an Exception is raised.
  • If a single object is provided, the object is returned each time the corresponding method is called (endlessly).
  • If None is provided (default), a default File/BytesIO object (with empty content) is returned each time the corresponding method is called (endlessly).