Difference between revisions of "Writing a Custom Task"

From OSUPDOCS
Jump to navigation Jump to search
Line 1: Line 1:
This page explains how to create a new [[MPM Calculations]] and integrate it into [[NairnMPM]]. Custom tasks can be used to add many new features to the code, including now ways to export results for visualization.
This page explains how to create a new [[MPM Input Files#Custom Tasks|custom task]] and integrate it into [[NairnMPM]]. Custom tasks can be used to add many new features to the code, including now ways to export results for visualization.


== Basic Steps ==
== Basic Steps ==
Line 7: Line 7:
* Create a new source code class that inherits from the <tt>CustomTask</tt> class (or a subclass of that class).
* Create a new source code class that inherits from the <tt>CustomTask</tt> class (or a subclass of that class).
* Create instance of this new task in <tt>MPMReadHandler::myStartElement()</tt>when an input file wants to use the new task:
* Create instance of this new task in <tt>MPMReadHandler::myStartElement()</tt>when an input file wants to use the new task:
** See section for tag <tt>Schedule</tt>
** See code section that handles the <tt>Schedule</tt> element
** New instance created as determined by the <tt>name</tt> attribute of the tag
** Create new instance of your task as determined by the <tt>name</tt> attribute of <tt>Schedule</tt> element
** This change should be the only change done to the core code.
** This change should be the only change done to the core code.
* Support methods listed below (as needed) in the new custom task. Most methods return the <tt>nextTask</tt> because the calling code uses it when it loops over all custom tasks. Any that are not needed can be omitted because the <tt>CustomTask</tt> class provides methods that simply returns the <tt>nextTask</tt>. Custom tasks are done at the end of the time step in the <tt>MPMStep()</tt> code.
* Support methods listed below (as needed) in the new custom task. Most methods return the <tt>nextTask</tt> because the calling code uses it when it loops over all custom tasks. Any that are not needed can be omitted because the <tt>CustomTask</tt> class provides methods that simply returns the <tt>nextTask</tt>. Custom tasks are done at the end of each time step and handled by the <tt>RunCustomTasksTask</tt> class.


== Task Parameters ==
== Task Parameters ==
Line 21: Line 21:
== Initialize ==
== Initialize ==


* <tt>CustomTask *CustomTask::Initialize()</tt> - This method is called once at beginning of the entire MPM analysis. Do any initialization and print info about the task using <tt>cout</tt>. Anything printed will by in the custom tasks section of the results file.
This method is called once at beginning of the entire MPM analysis:


== Step Tasks ==
;<tt>CustomTask *CustomTask::Initialize()</tt>
:Do any initialization and print info about the task using <tt>cout</tt>. Anything printed will by in the custom tasks section of the results file.


In each time step, the custom tasks is called through various methods. Your new tasks can implement those that are needed.
== MPM Time Step Tasks ==
 
In each time step, a custom task is called through various methods. Your new task can implement those that are needed.


=== Prepare Custom Task ===  
=== Prepare Custom Task ===  


* <tt>CustomTask *MyTask::PrepareForStep(bool &doExtraps)</tt> -  This method is called in <tt>MPMStep()</tt> just before custom tasks are done. You can use it to initialize variables. If the task needs extrapolations to the grids or summations over particles, set <tt>doExtraps</tt> to <tt>TRUE</tt>.
This method is called just before custom tasks are started:
 
; <tt>CustomTask *MyTask::PrepareForStep(bool &doExtraps)</tt>
: Initialize variables used by the task in the current time step. If the task needs extrapolations to the grid, set <tt>doExtraps</tt> to <tt>TRUE</tt>. Note that extrapolations to the grid in custom tasks are not done in parallel. Tasks that need to extrapolate to the gird should normally only be for periodic tasks, such as archiving grid results. Extrapolations that are done every time step might impact performance. If such tasks become essential, it will be better to incorporate them in the code as an actual (and parallel) tasks instead of a custom task. The custom task approach, however, can still be useful for developing a new feature.


=== Custom Extrapolatlions ===
=== Custom Extrapolatlions ===


The following methods are in a loop which allows a custom task to extrapolate some property to the grid or do some other calculation requiring a loop over all particles:
The following methods are in a loop which allows a custom task to extrapolate some property to the grid. Note there is no automatic handling for looping over particles, but a task can easily implement that loop on its own.


* `CustomTask *MyTask::BeginExtrapolations(void)` - Called once before extrapolation loop begins. Initialize any required variables.
; <tt>CustomTask *MyTask::BeginExtrapolations(void)</tt>
: Called once before the extrapolation loop begins. Initialize any required extrapolation variables.
      
      
* `CustomTask *MyTask::NodalExtrapolation(NodalPoint *ndmi,MPMBase *mpnt,short vfld,int matfld,double wt)` - Called repeatedly in the loop for each particle/node pair. `vfld` is the velocity field, `matfld` is material valocity filed (when in multimaterial mode), and `wt` is the weight (or mass times shape function). Can use to extrapolate particle data to the grid.
; <tt>CustomTask *MyTask::NodalExtrapolation(NodalPoint *ndmi,MPMBase *mpnt,short vfld,int matfld,double wt)</tt>
: Called repeatedly in the loop for each particle/node pair. <tt>vfld</tt> is the velocity field, <tt>matfld</tt> is material valocity filed (when in multimaterial mode), and <tt>wt</tt> is the weight (or mass times shape function). You can use this method to ./extrapolate particle data to the grid.


* `CustomTask *CustomTask::ParticleCalculation(NodalPoint *ndmi,MPMBase *mpnt,short vfld,int matfld, double fn,double xDeriv,double yDeriv,double zDeriv)` - Called repeatedly in the loop for each particle/node pair. `vfld` is the velocity field, `matfld` is material valocity filed (when in multimaterial mode), and `wt` is the weight (or mass times shape function). Can use to extrapolate nodal values to the particles.
; <tt>CustomTask *CustomTask::ParticleCalculation(NodalPoint *ndmi,MPMBase *mpnt,short vfld,int matfld, double fn,double xDeriv,double yDeriv,double zDeriv)</tt>
: Called repeatedly in the loop for each particle/node pair. <<tt>vfld</tt> is the velocity field, <tt>matfld</tt> is material valocity filed (when in multimaterial mode), and <tt>wt</tt> is the weight (or mass times shape function). You can use this method to extrapolate nodal values to the particles.


* `CustomTask *MyTask::ParticleExtrapolation(MPMBase *mpnt)` - Called once for each particle. Can use to sum any quantity over the particles.
; <tt>CustomTask *MyTask::EndExtrapolations(void)</tt>
   
: Called once when extrapolations are over. This method should free anything allocated during the extrapolations or to do some calculations.
* `CustomTask *MyTask::EndExtrapolations(void)` - Called once when extrapolations are over. Use to free anything allocated during the extrapolations or to do some calculations.


=== Custom Task Calculations ===
=== Custom Task Calculations ===


* `CustomTask *MyTask::StepCalculation(void)` - This method is the usually the main part of any custom task. Do the desired calculations. The code will have access to all global variables.
This method is the usually the main part of any custom task:
 
;<tt>CustomTask *MyTask::StepCalculation(void)</tt>
: Do the desired calculations. The code will have access to all global variables.
      
      
== Step 7d: Custom Finish ==  
=== Custom Task Clean Up  ===  
 
This method is called at the end of the time step:


* `CustomTask *MyTask::FinishForStep(void)` - Called once when all custom tasks are done. Use to free up any memory still allocated by the task.
; <tt>CustomTask *MyTask::FinishForStep(void)</tt>
: You should free up any memory still allocated by the task.

Revision as of 12:10, 9 February 2014

This page explains how to create a new custom task and integrate it into NairnMPM. Custom tasks can be used to add many new features to the code, including now ways to export results for visualization.

Basic Steps

The first steps create the task and allow it to be used in calculations

  • Create a new source code class that inherits from the CustomTask class (or a subclass of that class).
  • Create instance of this new task in MPMReadHandler::myStartElement()when an input file wants to use the new task:
    • See code section that handles the Schedule element
    • Create new instance of your task as determined by the name attribute of Schedule element
    • This change should be the only change done to the core code.
  • Support methods listed below (as needed) in the new custom task. Most methods return the nextTask because the calling code uses it when it loops over all custom tasks. Any that are not needed can be omitted because the CustomTask class provides methods that simply returns the nextTask. Custom tasks are done at the end of each time step and handled by the RunCustomTasksTask class.

Task Parameters

If the custom task has parameters, implement them by reading them with:

char *CustomTask::InputParam(char *pName,int &input)
Check the parameter name in pName. If it matches a defined task parameter name set input to the kind of pointer needed and return the actual pointer to the class variable for that parameter. If no match return NULL. This is the only method that does not return nextTask.

Initialize

This method is called once at beginning of the entire MPM analysis:

CustomTask *CustomTask::Initialize()
Do any initialization and print info about the task using cout. Anything printed will by in the custom tasks section of the results file.

MPM Time Step Tasks

In each time step, a custom task is called through various methods. Your new task can implement those that are needed.

Prepare Custom Task

This method is called just before custom tasks are started:

CustomTask *MyTask::PrepareForStep(bool &doExtraps)
Initialize variables used by the task in the current time step. If the task needs extrapolations to the grid, set doExtraps to TRUE. Note that extrapolations to the grid in custom tasks are not done in parallel. Tasks that need to extrapolate to the gird should normally only be for periodic tasks, such as archiving grid results. Extrapolations that are done every time step might impact performance. If such tasks become essential, it will be better to incorporate them in the code as an actual (and parallel) tasks instead of a custom task. The custom task approach, however, can still be useful for developing a new feature.

Custom Extrapolatlions

The following methods are in a loop which allows a custom task to extrapolate some property to the grid. Note there is no automatic handling for looping over particles, but a task can easily implement that loop on its own.

CustomTask *MyTask::BeginExtrapolations(void)
Called once before the extrapolation loop begins. Initialize any required extrapolation variables.
CustomTask *MyTask::NodalExtrapolation(NodalPoint *ndmi,MPMBase *mpnt,short vfld,int matfld,double wt)
Called repeatedly in the loop for each particle/node pair. vfld is the velocity field, matfld is material valocity filed (when in multimaterial mode), and wt is the weight (or mass times shape function). You can use this method to ./extrapolate particle data to the grid.
CustomTask *CustomTask::ParticleCalculation(NodalPoint *ndmi,MPMBase *mpnt,short vfld,int matfld, double fn,double xDeriv,double yDeriv,double zDeriv)
Called repeatedly in the loop for each particle/node pair. <vfld is the velocity field, matfld is material valocity filed (when in multimaterial mode), and wt is the weight (or mass times shape function). You can use this method to extrapolate nodal values to the particles.
CustomTask *MyTask::EndExtrapolations(void)
Called once when extrapolations are over. This method should free anything allocated during the extrapolations or to do some calculations.

Custom Task Calculations

This method is the usually the main part of any custom task:

CustomTask *MyTask::StepCalculation(void)
Do the desired calculations. The code will have access to all global variables.

Custom Task Clean Up

This method is called at the end of the time step:

CustomTask *MyTask::FinishForStep(void)
You should free up any memory still allocated by the task.