ANSYS, Inc. provides UPF subroutines that you can use for memory management.
The program uses a dynamic memory manager that overlays the system malloc and free functions and provides a mechanism for accessing the memory from Fortran as well as c and c++. Since the UPF subroutines are provided in Fortran, we will be discussing the Fortran access subroutines.
You may certainly use the system malloc and free functions or, for Fortran, the allocate system function. However, you may end up competing with the program for memory, and for large problems there may be insufficient system memory to perform the function.
Dynamic memory is done through Cray-style pointers, where a dynamically allocated array is defined via the construct
pointer (piArray,Array) integer Array(*)
and memory space for the array is allocated by assigning the pointer, in this case piArray, to the allocated memory space:
piArray = fAnsMemAlloc (size,...)
To use the memory manager in a UPF, follow these steps:
1. Define the dynamically allocated arrays:
pointer (piArray,Array), (pdData,Data) integer Array(*) double precision Data(*)
Initialize the pointers as follows:
piArray = PTRFTNNULL pdData = PTRFTNNULL
Allocate space for an array or arrays, as follows:
For integer numbers:
piArray = fAnsMemAlloc(ileng,MEM_INTEGER,C16Label)
For double-precision numbers:
pdArray = fAnsMemAlloc(dleng,MEM_DOUBLE,C16Label)
For complex numbers:
pcArray = fAnsMemAlloc(cleng,MEM_COMPLEX,C16Label)
For real numbers:
prArray = fAnsMemAlloc(rleng,MEM_REAL,C16Label)
Where the arguments are:
xleng is the desired size of the array
MEM_xxx is the keyword indicating the type of data
C16Label is a character*16 name of the memory block
You must include the ansysdef.inc include file to get the parameter values of MEM_INTEGER, MEM_DOUBLE, MEM_COMPLEX, and MEM_REAL.
Note: If there is insufficient memory, fAnsMemAlloc
returns "PTRFTNNULL".
Use the arrays.
Deallocate the space using the fAnsMemFree subroutine, as
follows:
call fAnsMemFree (piArray)
The next two sections provide input and output listings for the memory management subroutines.
For an example, see Function user03 (Demonstrates Using Memory), which appears earlier in this chapter.
*deck,fAnsMemAlloc
function fAnsMemAlloc (iLen, key, c16Label)
c primary function: Get A Block of Space from mem manager and Return Pointer
c keywords: integer function for mem allocate
c object/library: mem
c *** Notice - This file contains ANSYS Confidential information ***
c input arguments:
c iLen (int,sc,in) - length of the block (in data elements)
c c16Label (chr*16,sc,in) - 16 character name for the Block
c key (int,sc,in) - type of data for this block (see ansysdef)
c output arguments:
c fAnsMemAlloc (PTRFTN,sc,out) - Pointer to this data block -- needs to be
c tied to a local variable in the calling
c routine
*deck,fAnsMemFree
subroutine fAnsMemFree (memPtr)
c primary function: Free a Data Block, given a pointer
c keywords: subroutine to free a mem block
c object/library: mem
c *** Notice - This file contains ANSYS Confidential information ***
c input arguments:
c ptr (PTRFTN,sc,inout) - pointer for this block
c output arguments:
c ptr (PTRFTN,sc,inout) - pointer will be set to zero