1.11. Compiling and Linking UPFs on Windows Systems

There are three methods that you can use to link your custom routines into the Mechanical APDL program:

The source files for the user routines reside in the following subdirectory: Program Files\ANSYS Inc\V242\ansys\customize\user\.

The user-programmable features are loaded onto the system only if you perform a custom installation and choose to install the customization tools. If you intend to modify any of the user routines, make a duplicate copy of the Program Files\ANSYS Inc\V242\ansys\customize\user\ directory to preserve the original files for later use, if necessary.

For all three methods, you can write your user routines in one language or a combination of languages (Fortran, C, and C++). Note that when using a combination of languages, you are responsible for the calling interfaces between languages (see Programming in Languages Other Than Fortran).

The Ansys, Inc. Windows Installation Guide lists the compilers that are required for Windows systems.


Note:  You will need all the compilers specified in the Installation Guide specific to your operating system to use these user-programmable features. Ansys, Inc. does not provide assistance if customers are using unsupported compilers, or if the resulting objects are not compatible with the Mechanical APDL executable(s) as distributed.


Before linking UPFs, make sure that the INCLUDE, LIB, and PATH environment variables are set correctly for the required C/C++ and Intel Fortran compilers.

Visual Studio 2019 Professional is also required for linking user-programmable features on Windows platforms. In Visual Studio 2019, C++ is not installed by default. To install C++, you must select Custom Install and select Common Tools for C++ for 2019.

Before using any of the methods described below for linking UPFs, open the following Command Prompt window if you have Visual Studio 2019 Professional installed:

Start > All apps > Visual Studio 2019 > x64 Native Tools Command Prompt for VS2019


Note:  For all three linking methods, you might have issues with write/modify access if you use the default working directory: Program Files\ANSYS Inc\V242\ansys\custom\user\. If the linking operation errors out or fails, try running the required applications as an administrator by right-clicking and selecting “Run as administrator” while launching each application.


For all three linking methods, you can set the ANS_USER_PATH environment variable to specify the working directory where the created UPF DLLs reside. You can use another environment variable, ANS_USER_PATH_242, to point to a set of UPF DLLs specific to Release 2024 R2. ANS_USER_PATH_242 supersedes ANS_USER_PATH. Doing so allows you to set up and use more than one set of UPF DLLs. To change back to the location specified by ANS_USER_PATH, simply unset the ANS_USER_PATH_242 environment variable.

1.11.1. Using the /UPF Command

The /UPF command offers the simplest method for linking user-programmable features into Mechanical APDL. The format of the command is:

/UPF, RoutineName

where RoutineName is the name of the user routine (filename.ext) that you want to link. The specified routine must reside in the current working directory.

Following are prerequisites for using the /UPF command method on a Windows system:

  • A script named findUPF.bat is used to detect the /UPF command. You must include the path to this script in your system PATH variable. This script is typically located in Program Files\ANSYS Inc\V242\ansys\bin\<platform> where <platform> is a directory that uniquely identifies the hardware platform version: “Winx64” for 64-bit Windows.

  • Before starting Mechanical APDL, you must set the ANS_USE_UPF environment variable to TRUE. This causes the program to search for /UPF in the input file. This environment variable is required only on Windows systems and only when using the /UPF command method for linking UPFs. The ANS_USE_UPF environment variable should not be set when using other methods to link UPFs.

To use this method start Mechanical APDL in batch mode and include one or more /UPF commands in the specified input listing. When the program reads the input and detects /UPF, the appropriate DLL is created.

You can include /UPF anywhere in your input file, and you can repeat /UPF as many times as needed to include multiple user routines in the relinked version. The following user routines can be linked using this method:

UANBEG
UANFIN
UCNVRG
UELMATX
UITBEG
UITFIN
ULDBEG
ULDFIN
USER01 -USER10
USERCNPROP
USERCREEP
USERCV
USERCZM
USERELEM
USERFLD
USERFRIC
USERFX
USERHYPER
USERINTER
USERMAT
USERMATTH
USEROU
USERSWSTRAIN
USER_TBELASTIC
USERWEAR
USOLBEG
USOLFIN
USRCAL
USREFL
USRSHIFT
USRSURF116
USSBEG
USSFIN
UTIMEINC

If you are running in a distributed-memory parallel environment, you must set the ANS_USER_PATH environment variable to the working directory and include the name of the head compute node:

set ANS_USER_PATH=\\headnode\workingdirectory

When you run a user-linked version of the program by this method, the output includes this message:

NOTE – This ANSYS version was linked by /UPF with n user supplied routine(s).

where n indicates the number of routines specified by /UPF commands. The routines that have been linked will be included in the output listing.

Example Using Mixed Languages

The following directory contains an example of using the /UPF command method to link user routines that are written in mixed languages (Fortran, C, C++):

Program Files\ANSYS Inc\V242\ansys\custom\user\<platform>\Examples

The README.txt file in this directory contains complete instructions on running this example. This is a simple, automated test that verifies whether compilers are correctly installed and configured.

1.11.1.1. Using the /UPF Command on a Windows HPC Server System

Running a distributed-memory parallel (DMP) solution on a Windows HPC Server system is more involved than running across a simple Windows cluster. You can use the steps described here to test the /UPF command on a Windows HPC Server system. Several files are included with the Ansys, Inc. software installation for use in this example.

Before you begin, Visual Studio 2019 Professional and Intel Fortran must be on the head compute node.

The files needed for this example can be found in the following directory:

C:\Program Files\ANSYS Inc\v242\commonfiles\MPI\WindowsHPC\UPF

These include two user routines, a Mechanical APDL input file, and two files required by the HPC Job Manager:

usercreep.f
usermat.f
slupf.inp
RUNANSYS_UPF.xml
runansysupf.bat

Copy these files to:

C:\Temp\%USERNAME%

You will need to modify the value of the ANS_USER_PATH environment variable in RUNANSYS_UPF.xml to the appropriate location where the UPF DLL library resides.

Submit RUNANSYS_UPF.xml to the HPC Job Manager. When the job is complete, run the following commands from a Command Prompt window:

clusrun /exclude:%CCP_SCHEDULER% copy /y C:\Temp\%USERNAME%\Work\*.out \\%CCP_SCHEDULER%\Temp\%USERNAME%

findstr /I debug *.out

If the test worked correctly, this should display many lines of debug from all output files.

For more information on running a DMP solution under Windows HPC Server, see Configuration Requirements for DMP Processing in the Parallel Processing Guide.

1.11.2. Creating a Dynamic-link (DLL) Library

You can also set up UPFs on Windows systems by using a DLL library. All Fortran files (files ending with .F), C files (files ending with .c), and C++ files (files ending in .cpp) that you want to include in your DLL library should reside in your working directory. To compile all *.f, *.c, and *.cpp routines, issue the following command:

        \Program Files\ANSYS Inc\v242\ansys\custom\user\<platform>\ANSUSERSHARED.bat
      

After you issue ANSUSERSHARED.bat, the output will display the options for building the DLL library. The first portion of the output is shown below:

This is the ANSYS 2024 R2 ANSUSERSHARED script.  It is used
to build a DLL of User-Programmable Features for the program.

           NOTE:  The user subroutine source file(s) are expected to
                  reside in this directory.

 Enter one of the following choices to create your
 User-Programmable Feature DLL:

The user routines that are supported by this method will appear in a list. (These are the same user routines as listed above for the /UPF command method.) Enter the user routine name you wish to include. As an example, if you enter USERMAT the following output will display and prompt you to select another option:

You chose USERMAT
Microsoft (R) Incremental Linker Version 14.10.25027.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:UserMatLib.dll
-def:UserMatLibex.def
-dll
-machine:AMD64
-map
-manifest
-manifestfile:UserMatLib.dll.intermediate.manifest
-defaultlib:ANSYS.lib
-defaultlib:bufferoverflowU.lib
*.obj
   Creating library UserMatLib.lib and object UserMatLib.exp

      ************************************************************************

      UserMatLib.dll has been successfully built.

      Set the environment variable ANS_USER_PATH to the directory where the
      UserMatLib.dll resides and run ansys242 to use your newly generated
      user shared library.

      ************************************************************************

After you have selected all desired user routines, press Enter to quit.

If you are running in a distributed-memory parallel environment, you need to include the name of the head compute node when specifying the working directory:

set ANS_USER_PATH=\\headnode\workingdirectory

When you run a user-linked version of Mechanical APDL, the output echos the value of ANS_USER_PATH and will include the following:

NOTE:  This Mechanical APDL version was linked by Licensee

To return to the original version of Mechanical APDL, return the ANS_USER_PATH environment variable to its original value.

Multiple UPF DLLs can be created via the ANSUSERSHARED.bat script but must exist in the same directory as designated by the ANS_USER_PATH environment variable.

1.11.3. Using the ANS_ADMIN Procedure

The ANS_ADMIN procedure for compiling and linking UPFs on Windows systems creates a custom executable. Any of the user routines residing in Program Files\ANSYS Inc\V242\ansys\customize\user\ can be linked via this method. The executable can run in a shared-memory parallel (SMP) environment or a distributed-memory parallel (DMP) environment.

Perform the ANS_ADMIN procedure in either of the following ways:

  • Use the ANS_ADMIN242 utility (recommended)

  • Run the ANSCUST.bat script

If you modify any of the user routines, move them to the folder(s) they are linking in. By default, the folder is:

Program Files\ANSYS Inc\V242\ansys\custom\user\<platform>
where <platform> is a directory that identifies the operating system platform (such as Winx64 for 64-bit Windows).

Routines linked via the /UPF method  —  Routines linked via the /UPF method (listed in Using the /UPF Command) cannot be relinked via the ANS_ADMIN242 utility nor the ANSCUST.bat script. Those routines have reserved DLL names and are built only by issuing the /UPF command or by running the ANSUSERSHARED.bat script.

Using the ANS_ADMIN242 utility  —  To link your changes into Mechanical APDL, select the Relink MAPDL option in the ANS_ADMIN242 utility.

This procedure prompts you for the versions that you intend to relink, then compiles all Fortran files (.f), C files (.c), and C++ files (.cpp) in the Program Files\ANSYS Inc\V242\ansys\custom\user\<platform> directory.

After compiling, the procedure loads all object files (.obj), along with the default Mechanical APDL objects and libraries, and creates the custom executable.

The executable file created is named ansys.exe and resides in your specified location. (A custom Mechanical APDL executable must be named ansys.exe due to shared libraries.)

Creating a relinked version of Mechanical APDL via ANSCUST.bat  —  On Windows systems, attempting to create a relinked version of Mechanical APDL via the ANSCUST.bat script instead of the ANS_ADMIN242 utility may generate error messages.

The messages may indicate that object files have been created but that the Mechanical APDL executable has not been created, or that some libraries necessary to complete the link cannot be found. Typically, the errors appear because required compiler environment variables are not set. To avoid such errors, use the following workaround when relinking Mechanical APDL via ANSCUST.bat:

  1. Open a new command-prompt window:

    Select Start > All apps > Visual Studio 2019 > x64 Native Tools Command Prompt for VS2019.
  2. Move to the directory where the custom files exist (such as C:\Program Files\ANSYS Inc\V242\ansys\custom\user\):

    In the command-prompt window, issue the drive letter and any necessary cd (change-directory) commands.
  3. Create your custom Mechanical APDL version:

    Enter ANSCUST.bat in the command-prompt window.

Running a custom Mechanical APDL executable  —  After relinking the Mechanical APDL executable, run the program using either of these methods:

  • Select Start>Programs>ANSYS 2024 R2>Mechanical APDL Product Launcher.

    In the launcher, select the Customization/Preferences tab and browse to the path containing the relinked ansys.exe. Select other desired options, then click Run to execute the custom ansys.exe.

  • To execute the relinked version from a command-prompt window, issue either of the following commands:

    • Interactive:

      ansys242 -p <product variable> -g -custom <path>
    • Batch:

      ansys242 -b -p <product variable> -j jobname -i <input file> -o <output file> -custom <path>

    where <path> is the full path to the relinked ansys.exe.

    The -custom option must be the last option at the end of the command line.

    Output from a user-linked version contains the following statement:

    This Mechanical APDL version was linked by Licensee.