📜 ⬆️ ⬇️

Integration of the program on C # with the calculated core ANSYS 11

Problems of integrating their programs with others are increasingly covering the area of ​​software development. Many programmers believe that you need to write your own, integrate with other people's development is bad, and do not take into account the size of the development teams, their experience and feasibility. And in the course of a year or two, although it is possible to implement the same calculation system using the finite element method, it will not be of high quality. Is it possible for a couple of years to repeat the path of development of a project that has developed 10–20 years? Naturally it is impossible and often inappropriate.

Nobody argues that in Russia the most popular software tools are ANSYS, ABAQUS, SCAD, LIRA, PLAXIS, etc. And what is the probability to occupy a niche in this long-divided market segment? And few people want to relearn a new program. But the problems of integrating their programs with powerful settlement systems and transferring results from one to another is much more demanded. We have powerful computational packages, which took many years to develop and debug, we have a certain task, which is sometimes very difficult to implement in one computer system, or there are no capabilities we need that we can implement ourselves.

I took up this task, if necessary, to perform parametric optimization of material models in order to match the results of the simulation of a group of real tests with their calculation models. This is further aggravated by the fact that each software product considers in its own way and the results of calculations in ANSYS will not be identical to the calculation in ABAQUS with the same model parameters. It depends on the specific implementation of the calculations, the types of finite elements and even the location of the nodes of the grid. As a result, the engineer enters the parameters of materials, not knowing how accurately they correspond to reality. Moreover, the variability of the optimal parameters can reach 70% of the test results, especially since the processing of tests is carried out within the framework of one, maximum two strength conditions, often the simplest, and for complex models it is not even known how to determine the required parameters, especially when they are proposed to be set on the basis of from experience in any range.
')
To solve this problem, the task was set to integrate the program with the calculated core ANSYS. The integration task was successfully solved, although not without problems. And the main problem was the complete lack of documentation in free access, so I think the information on the implementation of the integration will be very useful for those who have the same problems as me.

Integration mechanism



With the beginning of the integration of all ANSYS software products under the unified ANSYS Workbench environment, it became possible to integrate their programs directly with this environment. The very architecture of ANSYS Workbench is built on the use of Java and VB scripts, through which the various ANSYS modules, called applets (Applets), are called.

The very first option that comes to mind for any developer is that it is enough to call previously written or generated scripts in Java-Script from a program. However, this path contains one rather serious flaw: each time the script is called, it will be necessary to reopen Workbench, initialize the ANSYS applet and only after that send commands to the kernel for calculation. This can be seen from the script, which is located in C: \ Program Files \ ANSYS Inc \ v110 \ AISOL \ SDK \ samples \ JPDL \ Demo.js. The script text is shown below:

oWB = new ActiveXObject ( "AnsysWB.WB.90" ) ;
owb. StartApplet = "AAOApplet" ;
owb. Run ( ) ;
owb. Title = "ANSYS Workbench" ;

ConnectToAnsysCS ( ) ;

var fs, a ;
ForAppending = 8 ;
fs = new ActiveXObject ( "Scripting.FileSystemObject" ) ;
a = fs. CreateTextFile ( "d: \ r undir \ t estfile.txt" , true ) ;

with ( ansys. cmd ) {
for ( l = 4 ; l < 10 ; l ++ ) {
_clear ( ) ;
_prep7 ( ) ;
_view ( null , - 1 , - 2 , - 3 ) ;
var w = 3 ;
var d = 2 ;
block ( null , l, null , w, null , d ) ;
kp4x = ansys. apdl . get ( "KP" , 4 , "LOC" , "X" ) ;
commentx = "KP 4 X LOC =" ;
commentx + = kp4x ;
kp4y = ansys. apdl . get ( "KP" , 4 , "LOC" , "Y" ) ;
commenty = "KP 4 Y LOC =" ;
commenty + = kp4y ;
kp4z = ansys. apdl . get ( "KP" , 4 , "LOC" , "Z" ) ;
commentz = "KP 4 Z LOC =" ;
commentz + = kp4z ;
ret = _com ( commentx ) ;
ret = _com ( commenty ) ;
ret = _com ( commentz ) ;
this . a . WriteLine ( commentx ) ;
this . a . WriteLine ( commenty ) ;
this . a . WriteLine ( commentz ) ;
sphere ( 1 ) ;
vsbv ( 1 , 2 ) ;
et ( 2 , 92 ) ;
vmesh ( "all" ) ;
da ( 9 , "all" ) ;
sfa ( 4 , 1 , "pres" , 10,000,000 ) ;
mp ( "EX" , 1 , 27E + 06 ) ;
mp ( "PRXY" , 1 , 0.3 ) ;
finish ( ) ;
_solu ( ) ;
solve ( ) ;
finish ( ) ;
_post1 ( ) ;
set ( "LAST" ) ;
plnsol ( "S" , "EQV" ) ;
_replot ( ) ;
finish ( ) ;
}
}
a. Close ( ) ;


If you programmatically generate scripts and run them, it will be very inconvenient, because the script itself will be executed by an external handler, which will significantly complicate its debugging. Yes, and generate a script that is responsible for the full cycle of work is very inconvenient.
The main problem was getting the handle to WorkBench and saving it. From this point of view, the most interesting part is responsible for loading the Workbench environment:

oWB = new ActiveXObject ( "AnsysWB.WB.90" ) ;
owb. StartApplet = "AAOApplet" ;
owb. Run ( ) ;
owb. Title = "ANSYS Workbench" ;


When studying this and other scripts, I had a question: if it can be Java-Script, then why not C #? And it turned out that it could. When installing ANSYS MS Visual Studio itself finds links to all the libraries of ANSYS, but they, at least in version 11, are heavily “trimmed“. But among them was a library that was responsible for the program work with the Workbench environment: AnsysWB, which is connected in the form of the namespace ANSYSWBLib.
To run ANSYS, it was enough to write the following code:

ANSYSWBLib. WBClass wb = new ANSYSWBLib. WBClass ( ) ; // Create an ABSYS WB object
wb. CommandLine . Directory = @ "c: tmp" ; // Specify the working directory
wb. CommandLine . JobName = "Test" ; // Specify the name of the project
wb. CommandLine . CadFile = @ "c: tmpqwe.ewq" ; // Specify the work file
wb. StartApplet = "AAOApplet" ; // Specify that we need the classic ANSYS module
wb. Run ( 0 ) ;

So, this code allows you to run ANSYS, and the classic ANSYS applet that will be launched is listed as the starting applet. But then we need to organize the transfer of control commands to him, and here there are difficulties. In Java Script, this is implemented by the following line.

App. Script . ans_sendcommand ( Command )

Where App is the link to the AAOApplet module descriptor. It is stored in the class property.

wb. AppletList . get_WBApplet ( "AAOApplet" ) . Applet . App

But in version 11 of ANSYS in the link libraries I did not find an interface for working with the ANSYS applet. Fortunately, NET FrameWork has a library such as Microsoft.JScript that allows you to compile JScript scripts directly in memory and execute them, and you can call a necessary function from C #.
For this purpose, I decided to write my own class, which would implement all the necessary features, to work with ANSYS / Class code is presented below.

class WBCommand
{
ANSYSWBLib. WBClass wb ;
public WBCommand ( ANSYSWBLib. WBClass WB )
{
wb = wb ;

aao = wb. AppletList . get_WBApplet ( "AAOApplet" ) . Applet . App ;

string code = @ "function closure ()
{
return function (App, Command)
{
return App.Script.ans_sendcommand (Command);
}
}

closure () " ;
//

SendCommandClosure = ( Closure ) Microsoft. JScript Eval . JScriptEvaluate ( code, Microsoft. JScript . Vsa . VsaEngine . CreateEngine ( ) ) ;
}
private Closure SendCommandClosure = null ;
private Closure SetWorkingDirectoryClosure = null ;
private object aao = null ;

public object SendCommand ( string Command )
{
return SendCommandClosure. Invoke ( null , new object [ ] { aao, Command } ) ;
}

public void SetWorkingDirectory ( string WorkingDirectory )
{
SendCommand ( "/ CWD," + WorkingDirectory ) ;
}
}

When the constructor of my class is called, the called procedure is formed in JScript and its entry point is saved:

string code = @ "function closure ()
{
return function (App, Command)
{
return App.Script.ans_sendcommand (Command);
}
}

closure () " ;
//

SendCommandClosure = ( Closure ) Microsoft. JScript Eval . JScriptEvaluate ( code, Microsoft. JScript . Vsa . VsaEngine . CreateEngine ( ) ) ;

To call it, the method was implemented:

public object SendCommand ( string Command )
{
return SendCommandClosure. Invoke ( null , new object [ ] { aao, Command } ) ;
}

As an input parameter, which requires a text command, and when the JSC script calls the function, an additional reference is sent to the applet, which was received in the constructor and stored in the aao variable.

Now, by calling the SendCommand method, it will be possible to send ANSYS APDL commands from our external application, performing all the necessary actions. This method emulates the input of commands directly to the ANSYS command line, but many commands need confirmation. In order to avoid requests for confirmation of actions, it is enough not to insert the commands one by one, but write them into the APDL file and call the SendCommand command ("/ inp, f, apdl"). In this case, ANSYS will not require confirmation of execution of another operation.

I did not check the work with new versions of ANSYS, so it is possible now you can completely refuse to use JS, but if compatibility with version 11 is necessary, then I haven’t found any other way to integrate my program.

Source: https://habr.com/ru/post/140397/


All Articles