Sie sind auf Seite 1von 11

Setting up a Project and Debugging with Visual Studio

Setting up a Project and Debugging with Visual Studio


Setting up a project in Visual Studio allows you to compile your contact models, particle body force
models and custom factories to a DLL without needing to use the command line. It also has
advantages for debugging such as attaching to the EDEM process and interrogating values passed to
your model on the fly.

Step 1: Install Visual Studio 2010


If you haven?t already, y ou can download Visual Studio Express from this location:
http://www.microsoft.com/visualstudio/eng/downloads#d-2010-express

Once installed you will need to register the product but this is free and there is no expiry on the
product.

Step 2: Create a New Project


1. Go to File > New Project...
Select Win32 Console Application

2. Enter a project name and location and press OK


(In this demonstration C:\test\HM_demo\My_Hertz_Mindlin)

EDEM API DEM Solutions Page 1 of 11


Setting up a Project and Debugging with Visual Studio

3. On the next page go to ,Application Settings?

4. Select ,DLL?
5. Select ,Empty Project?
6. Press Finish

EDEM API DEM Solutions Page 2 of 11


Setting up a Project and Debugging with Visual Studio

Step 3: Add source files to project


There are many example source files available through the EDEM User Forum, for this demonstration
we are going to use the Hertz Mindlin Contact Model which is available here:
http://www.dem-solutions.com/forum/viewtopic.php?f=15&t=177

The source code files are packaged in a zip file which contains the following files:
Source files:
CHertzHindlin.cpp
CHertzMindlin.h
Helpers.h
HertzMindlin.cpp
Plus an example simulation (HertzMindlin.dem)

1. Copy the source files to your newly created project folder, Visual Studio will have already
created other files in the folder.
(In this demonstration C:\test\HM_demo\My_Hertz_Mindlin)

EDEM API DEM Solutions Page 3 of 11


Setting up a Project and Debugging with Visual Studio

2. Add the *.cpp files to the Visual Studio project.


In Visual Studio > Solution Explorer > Source Files (right click) > Add > Existing Item...

Select both *.cpp files and press OK

EDEM API DEM Solutions Page 4 of 11


Setting up a Project and Debugging with Visual Studio

3. Add the header files (*.h) to the Visual Studio project.


In Visual Studio > Solution Explorer > Header Files (right click) > Add > Existing Item...

Select both *.h files and press OK

EDEM API DEM Solutions Page 5 of 11


Setting up a Project and Debugging with Visual Studio

4. The example source code files have now been added but other EDEM header files are needed
before the contact model will compile to a DLL. The additional header files required are
installed on your system by the EDEM installer and will be at the following location:
C:\Program Files\DEM Solutions\EDEM 2.4\src\Api

From the ,Core? folder copy the following files to your project:
ApiIds.h
ApiTypes.h
IApi.h
IApiManager_1_0.h
ICustomPropertyDataApi_1_0
ICustomPropertyManagerApi_1_0
PluginConstants.h

From the ,ContactModels? folder copy the following files to your project:
IPluginContactModel.h
IPluginContactModelV2_1_0.h
PluginContactModelCore.h

(Note:- there are additional header files in each folder but they aren?t required for thi s project. If
you aren?t sure which header files you need then add all of them to your project, the compiler
will simply ignore any files it doesn?t need)

5. Add these additional header files to your project as before.

EDEM API DEM Solutions Page 6 of 11


Setting up a Project and Debugging with Visual Studio

Step 4: Compile the model


User using 64-bit machines should read the Compiling to 64bit Guide available on the forum.
To compile the model select, Debug > Build Solution

If successful, the Output window at the bottom of Visual Studio will confirm “ Build: 1 succeeded ”
will also give you the location of the newly created *.DLL.

(Note:- If the build fails you will also be given useful information in this window as to why it has failed.
Always start with the first error message in the list as this is also likely to be the cause of many of the
following errors)

EDEM API DEM Solutions Page 7 of 11


Setting up a Project and Debugging with Visual Studio

Step 5: Debug vs Release


In the previous step we compiled the library in ,Debug? mode. This mode includes additional code in
the library which allows Visual Studio to interrogate the library while running and can be very useful
while modifying the code.
Once the library is complete and the library is being used to run simulations this code is no longer
required and will slow down the performance of EDEM. To remove this additional code and to allow
the compiler to optimise the library for performance, change Visual Studio to ,Release? mode. You
need to re-compile after changing the mode ( Debug > Build Solution ).

(Note:- The release build of your library will be created in a different location to the debug version so
make sure to check the output window for details.)

EDEM API DEM Solutions Page 8 of 11


Setting up a Project and Debugging with Visual Studio

Debugging using Visual C++ express edition


Step 1: Configure Visual C++ Expression edition for debugging
1. Use Visual C++ Expert Settings. Click on Tools > Settings > Expert settings
2. In the debug menu you will now have access to new functionalities. Click on Debug > Attached to
Process and select edem.exe

3. Load the library into EDEM


a. Select Options > File Locations in EDEM and be sure the locations point to the relevant
library

b. Select the plug-in from Interaction in the Physics section of the Creator

c. Load custom factories via Factories > Import

EDEM API DEM Solutions Page 9 of 11


Setting up a Project and Debugging with Visual Studio

Step 2: Use Breakpoint and Watch Value


1. In Visual C++, you can add a break point by clicking on left hand side of the window. If the contact
is active in EDEM and the process attached to Visual C++, the break point should appear as a
plain red ball as in the picture below.
2. Run the simulation in EDEM. The simulation will stop if the break point is encountered.
3. Push F10 key to process one instruction of your model at a time

The red point indicates the breakpoint while the yellow arrow indicates the next line to be
executed when F10 is pushed.
4. It is possible to see the value contained by a variable. To do that, right click on it and click on “ Add
to watch ” (You can also see the value of a variable by hovering it with your mo use).

EDEM API DEM Solutions Page 10 of


11
Setting up a Project and Debugging with Visual Studio

Here hertzForce is in red because the value has just been updated by the previous instruction on line
245 of the previous picture.

5. If you Press F5, the simulation will run until the breakpoint is hit again, in other word when another
computation of the contact force is required.

Step 3: Use conditional breakpoint


1. The use of conditional break point allows you to stop the stimulation at a break point under a
certain condition. For instance in the following screenshot, the condition is that the value of the
variable time has changed.

2. Press F5

All the watched variables are in red here. Indeed, time has changed this it was the condition to have
the break point effective. The equivalent mass nEquivMass has changed because the particles in
contact are different than previously, hence a different equivalent mass and hertzForce is worth zero
since it has not been calculated yet.

EDEM API DEM Solutions Page 11 of


11

Das könnte Ihnen auch gefallen