Chapter 5. Implementing new models

Table of Contents

1. Introduction
2. Implementation of a new model
2.1. Adding the model code in the C++ file
2.2. Registering the model
2.3. Compile and Test
3. Registering the new model in the frontend GUI
4. Defining the default parameters in the C code
4.1. No parameters needed
4.2. Default parameters needed
4.3. Default parameters generated by a program
5. Getting the model default parameters from IDL
5.1. The simple way
5.2. The detailed way

1. Introduction

The creation of a new electron density model involves editing several source files.

2. Implementation of a new model

2.1. Adding the model code in the C++ file

The models are in the files modelsXXtoYY.cc, with [XX,YY] the range of model numbers. To avoid having a big file that would require a too long compilation time, the models are grouped 10 by 10. In this example we will add the model 57. The first step is to edit the file models51to60.cc. We can add the following code:

//! Density 57: constant and uniform density
float CModel57::Density(Cvec v,float* pparam,float& temperature)
{
	return unifdens;
} 

//! Inititialization of the parameters for the Model 57
void CModel57::initParam(float* pparam)
{
	unifdens=pparam[0]; // - constant density, specified by the user
}

void CModel57::dumpDefaultParamForIDL(std::vector<moddefparam>& vp,int& flagcase) 
{
	flagcase=0;
	vp.push_back(moddefparam("","constant and uniform density","","")); 
	vp.push_back(moddefparam("unifdens","1.","Constant density","electron/cm^3"));    
	return;
}

and then edit also the header file models51to60.h:

//! Constant and uniform density
class CModel57 : public CModelBase 
{
  public:
	float Density(Cvec,float*,float&); 
	void initParam(float* pparam);
	void dumpDefaultParamForIDL(std::vector<moddefparam>&,int&);
    
  protected:
	float unifdens;
};

The new density model class CModel57 should derive from the virtual class CModelBase. Density is the density model method that returns the electron density depending on the point of space noted v. pparam is an array that can contain extra parameters to compute the density. A temperature can also be passed and used for the calculation: this parameter has been implemented for compatibility with a raytracing software in the radiometric spectrum range. Note that the density model is the simplest your can defined: it just returns a user defined constant density, whatever the position in space. Note also that the initParam method is useful when some preliminary calculation has to be done a single time at the initialization of the model (instantiation of the class). The results can be re-used each time the model is called, avoiding recalculating the parameters. The dumpDefaultParamForIDL method returns the default parameters for IDL: see Section 4, “Defining the default parameters in the C code” for a complete explanation.

2.2. Registering the model

We need now to register the model in the modelselect method of the CModelBase class. Edit CModelBase.cc and insert the lines shown in between the /////// comment separators.

 case 56 :
      CModel56 *pmodel56;
      pmodel56 = new CModel56;
      pmod= (CModelBase*) pmodel56;
      return pmod;
      break;

///////////////////////////////////////////////////////
 case 57 :
      CModel57 *pmodel57;
      pmodel57 = new CModel57;
      pmod= (CModelBase*) pmodel57;
      return pmod;
      break;
///////////////////////////////////////////////////////

      // -----------------------------------
      // |    REGISTER NEW DENSITIES HERE    |
      // -----------------------------------

  default : 
      std::cout << "Model ID out of range: model 1 used by default." << std::endl;
      pmodel1 = new CModel01;
      pmod= (CModelBase*) pmodel1;
...

We register the model classes in this modelselect method because the main raytracing program engine only knows the CModelBase class. The user selects the model by passing the model registration number (here 57) to modelselect which returns a pointer to the corresponding density model Density method. This step is just done once at the initialization and avoid that selection step each time Density is called.

2.3. Compile and Test

You can now compile the C++ code using make and try to use the new model using the following:

IDL[mycomputer]> print,getdensity([4.,0,0],57,modparam=[1.])
% Compiled module: GETDENSITY.
Seconds ellapsed :
     0.011713028
      1.00000

The program getdensity returns the value of the density at a point of the space, here [4.,0,0] in this case. The value returned is 1 since we passed 1 in the model parameter array modparam.

3. Registering the new model in the frontend GUI

TO BE DONE

4. Defining the default parameters in the C code

The default parameters of a model can be set by redefining the virtual method dumpDefaultParamForIDL, derived from CModelBase class. We show here 3 examples of implementation.

4.1. No parameters needed

The code in Example 5.1, “No parameters needed.” is from models01to10.cc. That model does not require any parameter. Only a description of the model is passed in the second field of structure moddefparam.

[Important]Important

The model description is optional. If implemented, it should always be in the first element of the array vp, and the first field of structure moddefparam should be empty.

The variable flagcase is a binary flag: LSB set to 1: the parameters are undefined: that's the default value if dumpDefaultParamForIDL is not overwritten. Bit 1 set to 1: won't be included in the frontend: can be useful if the model is obsolete or is still under construction. Bit 2 set to 1: No parameters are needed: modparam can remain undefined.

Example 5.1. No parameters needed.

void CModel01::dumpDefaultParamForIDL(std::vector<moddefparam>& vp,int& flagcase) {
	flagcase=0x4;
	vp.push_back(moddefparam("","M.Guhathakurta model, frozen parameters.","","")); 
	return;
}

4.2. Default parameters needed

Example 5.2, “Definition of default parameters needed.” shows code from density model 33, defined in models31to40.cc. The first element of array vp is the model description. The rest of the rows defines the default parameters. Field 1 of moddefparam is the parameter name. Field 2 is the default value, which should always be a float or an array of float. Field 3 is a description of the parameter. Field 4 is the units of the parameters. See also moddefparam definition in the code.

Example 5.2. Definition of default parameters needed.

void CModel33::dumpDefaultParamForIDL(std::vector<moddefparam>& vp,int& flagcase) {       
	flagcase=0;
	vp.push_back(moddefparam("","Tube shell model.","",""));        
	vp.push_back(moddefparam("d0","0.7","FULL thickness of shell.","Rsun"));        
	vp.push_back(moddefparam("rb","2.55","Dist to bottom of structure.","Rsun"));   
	vp.push_back(moddefparam("alpha","0.52","Angle between axis and foot.","rad")); 
	vp.push_back(moddefparam("rf","10","Dist junction line-circle.","Rsun"));       
	vp.push_back(moddefparam("ratio","0.2","ratio of tube radius to height","Rsun")); 
	return;
}

4.3. Default parameters generated by a program

Example 5.3, “The default parameters are generated by a sequence of IDL instructions.” if from density model 25 in models21to30.cc. As explain above, the first element of vp is the description of the density. The following elements (in Example 5.3, “The default parameters are generated by a sequence of IDL instructions.” there is only one) defines parameters and variables. Field 1 is the name of the variable. Field 2 can be either hard coded value or an IDL instruction, as in this example. Field 3 is a description of the parameter. Field 4 is not used.

The line following the parameter definition gives the name of the variable that will contain the model parameter array. Field 1 is that variable name. Field 2 must be empty in order that the parser interprets properly that line. Fields 3 and 4 are unused.

The following lines contain a sequence of IDL code that generates the model parameter array. As explained in the previous paragraph, there should be an assignment of the variable given in the line preceeding the code sequence: in Example 5.3, “The default parameters are generated by a sequence of IDL instructions.” it is mp. Parameters that has been assigned in the parameter definition section (see two paragraph above) of the vp array can be included in the code sequence. They must be preceeded by the dollar sign so it will be interpreted properly by the model default parameter array parser parsemoddefparam.pro. For example, the parameter cubefile is passed to the loaddenscube in Example 5.3, “The default parameters are generated by a sequence of IDL instructions.”.

Example 5.3. The default parameters are generated by a sequence of IDL instructions.

void CModel25::dumpDefaultParamForIDL(std::vector<moddefparam>& vp,int& flagcase) {       
	flagcase=0;
	vp.push_back(moddefparam("","Density cube.","",""));    
	vp.push_back(moddefparam("cubefile","getenv('RT_PATH')+get_delim()+'testcube.fts'","Filename of the density cube","")); 
	vp.push_back(moddefparam("mp","","",""));       
	vp.push_back(moddefparam("","mp=loaddenscube($cubefile)","Load the density cube.","")); 
return;
}
							

5. Getting the model default parameters from IDL

5.1. The simple way

The simplest way to assign the default parameters corresponding to a model is to use the /usedefault keyword to the raytracewl IDL function. The following example shows the method for the model 25:

IDL[mycomputer]> raytracewl,modelid=25,/usedefault,modparam=mp

The variable mp will contain the model default parameters and can be reused in an other call to raytracewl.

5.2. The detailed way

Building the default parameters from IDL is done using two programs: getmoddefparam.pro and parsemoddefparam.pro. The Example 5.4, “Getting the default parameters from IDL.” shows the code to build the default parameters for the model 25. The parameter vector from the C code is given in Example 5.3, “The default parameters are generated by a sequence of IDL instructions.”. getmoddefparam just fetch the model parameters from the C routine and store it in the structure s. Then parsemoddefparam parse that structure s and build the model parameter array noted mparam in the example. The rest of the code Example 5.3, “The default parameters are generated by a sequence of IDL instructions.” build an image and display it.

The keyword filepro for function parsemoddefparam is optional. You can set that keyword to the filename of the IDL function that will build the model parameter array:

mparam=buildmodel25param(sv)

The output variable sv is a structure that contains the different parameters needed to build mparam. You can modify the values of that structure and reuse buildmodel25param to generate mparam with a different set of parameters. In the case of Example 5.3, “The default parameters are generated by a sequence of IDL instructions.”, it would load a different density cube for example.

Example 5.4. Getting the default parameters from IDL.

getmoddefparam,25,s
mparam=parsemoddefparam(s,sv,filepro='buildmodel25param.pro')
raytracewl,sbt,modparam=mparam,modelid=25,imsize=[128,128],/c2,$
neang=[30.,0,20]*!dtor,losrange=[-10,10],losnbp=128
wnd,0,alog10(sbt.im > 1e-12)