Skip to content
Snippets Groups Projects
externalgravity.rst 6.53 KiB

External Gravity Task Example

An example of how to implement an external gravity task in SWIFT

An external gravitational field can be imposed in SWIFT to mimic self-gravity. This is done by assigning a gravitational force that falls as $1/ r^2$ (mathjax support to be included).

In order to do this we update the files as described in :ref:`NewTask`. For the specific case of adding an external graviational field the additions are as follows:

task.h

Code (snapshot Nov 2015):

/* The different task types. */
enum task_types {
   task_type_none = 0,
   task_type_sort,
   task_type_self,
   task_type_pair,
   task_type_sub,
   task_type_ghost,
   task_type_kick1,
   task_type_kick2,
   task_type_send,
   task_type_recv,
   task_type_link,
   task_type_grav_pp,
   task_type_grav_mm,
   task_type_grav_up,
   task_type_grav_down,
   **task_type_grav_external,**
   task_type_psort,
   task_type_split_cell,
   task_type_count
};

Task of type - task_type_grav_external - added to list of tasks.

task.c

Code (snapshot Nov 2015):

/* Task type names. */
const char *taskID_names[task_type_count] = {
    "none",  "sort",    "self",    "pair",    "sub",
    "ghost", "kick1",   "kick2",   "send",    "recv",
    "link",  "grav_pp", "grav_mm", "grav_up", "grav_down", "grav_external",
    "psort", "split_cell"
 };

Task added to list of task names (used only for debugging purposed).

cell.h

Code (snapshot Nov 2015):

/* The ghost task to link density to interactions. */
   struct task *ghost, *kick1, *kick2, *grav_external;

Struture of type "task" declared (or pointer to a task at least).

timers.h

Code (snapshot Nov 2015):

/* The timers themselves. */
enum {
  timer_none = 0,
  timer_prepare,
  timer_kick1,
  timer_kick2,
  timer_dosort,
  timer_doself_density,
  timer_doself_force,
  timer_doself_grav,
  timer_dopair_density,
  timer_dopair_force,
  timer_dopair_grav,
  timer_dosub_density,
  timer_dosub_force,
  timer_dosub_grav,
  timer_dopair_subset,
  timer_doghost,
  timer_dograv_external,
  timer_gettask,
  timer_qget,
  timer_qsteal,
  timer_runners,
  timer_step,
  timer_count,
  };

The timer list is updated to include a timer task.

engine.c

Code (snapshot Nov 2015):

void engine_mkghosts(struct engine *e, struct cell *c, struct cell *super) {

    int k;
    struct scheduler *s = &e->sched;

    /* Am I the super-cell? */
    if (super == NULL && c->nr_tasks > 0) {

       /* Remember me. */
       super = c;

       /* Local tasks only... */
       if (c->nodeID == e->nodeID) {

           /* Generate the external gravity task*/
           c->grav_external = scheduler_addtask(s, task_type_grav_external, task_subtype_none, 0, 0,
                               c, NULL, 0);

           /* Enforce gravity calculated before kick 2 */
           scheduler_addunlock(s, c->grav_external, c->kick2);
           }
       }
 }

The first function call adds the task to the scheduler. The second function call takes care of the dependency involved in imposing an external gravitational field. These two functions are worth considering due to their obvious importance.

The function prototype for the addtask function is (found in scheduler.c):