Swarm-NG
1.1
|
You can write your own scenarios for integrating ensembles and use parts and pieces of the Swarm-NG library for your C++ application.
In this tutorial we go through a simple C++ program to show you how you can use Swarm-NG library. You can find the source file at src/tutorials/tutorial_simple.cpp
We start at the first line of the program. You need to include needed headers to use the library.
Some standard C++ libraries
Include Swarm library headers. You may need to add other headers if you want to use More advanced feautures. But for simple ensemble creation and integration the following line should be enough
We define a shortcut; because we need to use cudaThreadSynchronize() after every GPU call to make sure that we are in-sync with GPU.
All the Swarm-NG classes are enclodes in swarm namespace, a C++ tradition. (std is the standard library)
We define parameters for integration as constants so we can change them later. We set the destination time for 5 revolutions. Each revolution is 2 * pi radians.
Swarm uses the configuration data structure to pass most of parameters to creation functions Here we put all of our configuration items in a 2D string array.
The easy way to create a config object is from a 2D array containing pairs of strings. You can also use swarm::config::load to load a configuration from a file. We use this config object to configure all parts of swarm. Note that not every function reads all the configuration items. For example, generate_ensemble only reads "nsys" and "nbod".
Since our program is short, we can put everything in the main function.
First we create our reference ensemble. We use the automatic generation using generate_ensemble function This function creates a very stable ensemble of systems for us.
We have to make a copy of initial conditions. We would like to compare the results to initial conditions after integration
Initialize Swarm library. Basically, it initializes CUDA system and default logging system.
Select and create the integrator. While you can create an integrator by calling its constructor directly. It is recommended to use integrator::create since it gives you more flexibility at runtime. The create function looks in the swarm library and finds the integrator you requested from the list of available plugins.
Now we set-up the integrator for integrating.
First set the ensemble. For a GPU integrator, the GPU memory will be allocated.
Now set the destination time where we want to stop the integration.
Need to synchronize because integrator::set_ensemble may upload data to GPU.
Now that everything is set-up, it is safe to pull the trigger and call the integrate method on integrator. Note that since we didn't set a logger for the integrator, it will use the default logging system.
Need to synchronize because integrate is a GPU call.
Once the integration done, we need to examine the data. The easiest check is to see if the systems have preserved energy.
find_max_energy_conservation_error is a utility function that compares two ensemble of systems. We compare our working ensemble ens to the unchanged ensemble ref.
This concludes the program, at this point you will need to clean up the data and ensembles. But most of Swarm-NG objects are stored in reference-counter pointers and will be automatically deallocated by C++ runtime library.
If you want to do more with Swarm-NG, you may consider looking into following classes
To find the complete listing of this tutorial look at src/tutorials/tutorial_simple.cpp in the source code repository.