You are on page 1of 23

Distributed by ANSYS, Inc.

ANSYS
User Subroutine
Tutorial
ANSYS, Inc.
User Subroutine Tutorial ANSYS, Inc.

Contents

1. Getting Started ................................................................................................................................ 2


1.1. Using User Subroutine ............................................................................................................ 2
1.2. Prerequisite knowledge ........................................................................................................... 2

2. Creating a Project............................................................................................................................ 3
2.1. Running the Microsoft Visual Studio 2010 ............................................................................. 3
2.2. Creating a new project ............................................................................................................ 3

3. Creating additional library directories ............................................................................................ 6


3.1. Copy the dfslv.lib file. ............................................................................................................. 6
3.2. Additional library setting for dfslv.lib ..................................................................................... 6

4. Additon of ANSYS Motion pre-defined header file in the Project ............................................... 10


4.1. Copy defusub.h file and paste the file in the project folder .................................................. 10
4.2. Addition of the defusub.h file ............................................................................................... 10

5. How to create User subroutine function ....................................................................................... 12


5.1. Creating Contact.cpp file ...................................................................................................... 12
5.2. Creating a function in the contact.cpp file ............................................................................ 13

6. Confirm the created dll file and add to a solver folder.................................................................. 16


6.1. Confirm creating the Contact.dll file .................................................................................... 16
6.2. Add to Contact.dll in the solver folder .................................................................................. 16

7. Creating Model and Analysis ........................................................................................................ 17


7.1. Creating Contact Force Model .............................................................................................. 17
7.2. Analysis ................................................................................................................................ 21

-1-
User Subroutine Tutorial ANSYS, Inc.

1. Getting Started

1.1. Using User Subroutine

Using function expression in ANSYS Motion Preprocessor, it is expressible in fundamentally


formula, however there is a limit as realize the formula of complex form. User Subroutine can be
used to formula of complex form using Microsoft Visual Studio. Users can study how to create a
project for dynamic connection library(dll) file and additional library. This document explains how
to add header file of ANSYS Motion Pre-defined function and create User Subroutine. Also, a
process to check and add dll file is included in this tutorial. There is a example for create contact
force such as a entity of ANSYS Motion Preprocessor.

1.2. Prerequisite knowledge

Followings are prerequisite knowledge how to use User Subroutine. It recommend to use Microsoft
Visual Studio 2010 Express Edition, or greater for using Visual C++ language.
Followings are prerequisite knowledge how to use ANSYS Motion/User Subroutine

 ANSYS Motion/Pre, ANSYS Motion/Solver, ANSYS Motion/Post

 Microsoft Visual C++

 Microsoft Visual Studio 2010 Express Edition, or greater version

For more information as above prerequisite knowledge, please refer to the ANSYS
Motion/Preprocessor Manual and Tutorial, ANSYS Motion/Postprocessor Manual and Tutorial,
ANSYS Motion/User Subroutine Manual.

-2-
User Subroutine Tutorial ANSYS, Inc.

2. Creating a Project
Explaining how to create *.dll file for user subroutine. This tutorial is explained by Microsoft
Visual Studio Express Edition 2010. This tutorial was written on 32 bit platform basis. To follow
along with this tutorial, you will need to build with 64 bit platform.

2.1. Running the Microsoft Visual Studio 2010

Run the Microsoft Visual Studio 2010 using following icon.

< Microsoft Visual Studio 2010 icon >

2.2. Creating a new project

Click the New Project icon at top of the File menu.

< New project icon >

Select Win32  Win32 Console Application  Key in 'Contact' in the Name box and
location is 'C:\User_Subroutine'  Click OK.(Uncheck create directory for solution).

-3-
User Subroutine Tutorial ANSYS, Inc.

<Selecting a type of the new project >

Click Next button.

< Selectinf Win32 Colsole Application program >

-4-
User Subroutine Tutorial ANSYS, Inc.

Select DLL  Check Empty project  Click Finish. The new project for User subroutine is
created.

< Win32 Console Application Wizard >

-5-
User Subroutine Tutorial ANSYS, Inc.

3. Creating additional library directories


Set the defined library as ANSYS Motion pre-defined function (dfslv.lib).

3.1. Copy the dfslv.lib file.

When user is compiling by 32-bit, bring the lib file in 32-bit solver. When user is compiling by
64-bit, bring the lib file in 64-bit solver. Copy and paste the dfslv.lib file to project folder.
Lib file is located as follows, (ANSYS Motion install path\Motion\Solver\Windowns\lib)

3.2. Additional library setting for dfslv.lib

Click right button of a mouse at the name of the project and select Properties.

< Open properties of the project >

Select All Configurations.  Click Linker  General  at Additional Library


Directories

-6-
User Subroutine Tutorial ANSYS, Inc.

< Properties of the project >

-7-
User Subroutine Tutorial ANSYS, Inc.

Click Macros

< Open Macros >

Check path of created project. Click OK.

< Confirm ProjectDir >

-8-
User Subroutine Tutorial ANSYS, Inc.

Key in $(ProjectDir) in the Additional Library Directories box after confirming setting by All
Configurations.

< Additional Library Directories >

Click Input and key in dfslv.lib at the Additional Dependencies box. And click OK.

< Additional Dependencies>

-9-
User Subroutine Tutorial ANSYS, Inc.

4. Additon of ANSYS Motion pre-defined header


file in the Project
How to added ANSYS Motion Pre-defined header file in the Project.

4.1. Copy defusub.h file and paste the file in the project folder

Copy and extract “Usersubroutine.zip” file from “ANSYS installed path\Motion\Document\”


to user directory.
And copy and paste the defusub.h file in “\Usersubroutine\Code\” folder to project folder.

4.2. Addition of the defusub.h file

Click right mouse button and Existing Item.

< Addition of the existing item >

Click defusub.h file and Add button.

- 10 -
User Subroutine Tutorial ANSYS, Inc.

Check contents after double click of the added defusub.h in solution explorer.

< Added defusub.h >

- 11 -
User Subroutine Tutorial ANSYS, Inc.

5. How to create User subroutine function


Explaining how to create user subroutine function

5.1. Creating Contact.cpp file

Click mouse right button of project.  Select Add  select New Item.

< Addition of New Item >

Click Code  C++ File  Name (Contact)  Add

< Additiion of the Contact.cpp >

- 12 -
User Subroutine Tutorial ANSYS, Inc.

Check the Contact.cpp.

< Added the Contact.cpp >

5.2. Creating a function in the contact.cpp file

Key in the following text in the Constact.cpp.

The following code calculated the displacement and velocity by using sysary function after
get Markers, Contact Stiffness, Damping Coefficient and Contact order at upar[]. Then,
calculate a force.

#include "defusub.h"
#include <stdio.h>
#pragma warning(disable : 4996)
#ifdef _MANAGED
#pragma managed(push, off)
#endif

extern "C" DLLEXPORT Contact


(void* slvid, DF_CDOUBLE time, DF_DOUBLE upar[], DF_CINT npar, DF_CINT jflag,
DF_CINT sflag, DF_DOUBLE value[])
{
int mkid[3], nvalue, ierror, i;
double k, c, n, radius, disp[6]={0.0}, vel[6]={0.0}, delta[6]={0.0},
delta_dot[6]={0.0}, force[6]={0.0};

- 13 -
User Subroutine Tutorial ANSYS, Inc.

//input data

mkid[0] = (int)upar[0];
mkid[1] = (int)upar[1];
mkid[2] = (int)upar[2];
k = (double)upar[3];
c = (double)upar[4];
n = (double)upar[5];
radius = (double)upar[6];
nvalue = 6;

//get displacement, velocity


sysary(slvid,"DISP",mkid,3,disp,nvalue,ierror);
sysary(slvid,"VEL",mkid,3,vel,nvalue,ierror);

//contact force
if (disp[2] < radius)
{
delta[2] = -(disp[2]-radius);
delta_dot[2] = -vel[2];
force[2] = (k*pow(delta[2],n))+(c*delta_dot[2]);
}
else
{
for(i=0; i<6 ; i++)
{
force[i] = 0;
}
}

//return value
for(i=0; i<6 ; i++)
{
value[i] = force[i];
}
}

- 14 -
User Subroutine Tutorial ANSYS, Inc.

< Input the function >

- 15 -
User Subroutine Tutorial ANSYS, Inc.

6. Confirm the created dll file and add to a solver


folder
Explaining how to add to *.dll file in solver folder after create *.dll file.

6.1. Confirm creating the Contact.dll file

Selcet release in configuration manager.

< Modifying Configuration Manager >

Click the Build at top menu  Click Rebuild Solution  Bulid the project

< Building the solution >

Check the type of solution and move to Release folder of low rank. Confirm the created
Contact.dll file.

6.2. Add to Contact.dll in the solver folder

Copy and paste the contactbll file to solver folder as follows.


(ANSYS Motion install path\Motion\Solver\Windowns)

- 16 -
User Subroutine Tutorial ANSYS, Inc.

7. Creating Model and Analysis


Explaining how to create contact force model and apply user subroutine. And analysis
contact force

7.1. Creating Contact Force Model

Run ANSYS Motion Menu Create New Model  Select Standard EMBD Model and
key in the name. Click OK.

< Creating new model >


Click Finish.

< Deciding gravity direction and icon size >

- 17 -
User Subroutine Tutorial ANSYS, Inc.

Create a sphere body at point (100,0,100). The radius is 10.

<Creating a sphere body >

Create Ground Markers at (100,0, 0) point.

< Creating Ground Markers>

- 18 -
User Subroutine Tutorial ANSYS, Inc.

<Created body and marker>

Create Vector Force . Base Body is ground and Action Body is the sphere body (BD_01) .
Action Marker is a base marker of BD_01/CM and Base Marker is the marker of
Ground/MK_01.
When create Vector Force, refer to table and picture as following.

VECTOR_01
Base Body Ground
Action Body BD_01
Base Position MK_01
Action Position BD_01/CM

.
< Creating Vector Force >

- 19 -
User Subroutine Tutorial ANSYS, Inc.

< Created Vector Forces >

Create User Subroutine as following.

< Creating User Subroutine >

Apply the User Subroutine at Vector Force.


Vector_01 : Select Contact_bottom and reference frame is Ground/MK_01..

- 20 -
User Subroutine Tutorial ANSYS, Inc.

< Appying User Subroutine at the Vector Force >

7.2. Analysis

Click Run button.

<Run button>

<Executed Simulation>

- 21 -
User Subroutine Tutorial ANSYS, Inc.

Select BD_01 in Object Navigator → Select Position in Characteristic→ Select Z in


Component → Select Add Curve in Draw Curve
Confirm Z-Posigion of sphere body in Postprocessor.

<Simulation Result>.

- 22 -

You might also like