Static vs Dynamic Type systems

  • C++ is a "statically-typed" language
  • This means that "type checking" is performed during compile-time as opposed to run-time
  • Python and MATLAB are examples of "dynamically-typed" languages

Static Typing Pros and Cons

  • Pros
    • Safety - compilers can detect many errors
    • Optimization - compilers can optimize for size and speed
    • Documentation - The flow of types and their uses in expression is self documenting
  • Cons
    • More explicit code is needed to convert ("cast") between types
    • Abstracting or creating generic algorithms is more difficult

Using Templates

  • C++ implements the generic programming paradigm with "templates".
  • Many of the finer details of C++ template usage are beyond the scope of this short tutorial.
  • Fortunately, only a small amount of syntactic knowledge is required to make effective basic use of templates.
template <class T>
T getMax (T a, T b)
{
  if (a > b)
    return a;
  else
    return b;
}

template <class T>
T getMax (T a, T b)
{
  return (a > b ? a : b); // "ternary" operator
}
int i = 5, j = 6, k;
float x = 3.142; y = 2.718, z;
k = getMax(i, j);       // uses int version
z = getMax(x, y);       // uses float version
k = getMax<int>(i, j);  // explicitly calls int version

Template Specialization

template<class T>
void print(T value)
{
  std::cout << value << std::endl;
}

template<>
void print<bool>(bool value)
{
  if (value)
    std::cout << "true\n";
  else
    std::cout << "false\n";
}
int main()
{
  int a = 5;
  bool b = true;
  print(a); // prints 5
  print(b); // prints true
}

MOOSE validParams() Function

  • The InputParameters class is defined in moose/include/utils/InputParameters.h
  • The validParams() function returns an object of type InputParameters
// template function declaration (InputParameters.h)
template<class T>
InputParameters validParams();

// Fully-specialized validParams() function (YourKernel.h)
template<>
InputParameters validParams<YourKernel>();
  • This function is used by the Factory and Parser for getting, setting, and converting parameters from the input file for use inside of your Kernel.
  • You need to specialize validParams() for every MooseObject you create!

Specialized validParams() example

#include "YourKernel.h"
template<>
InputParameters validParams<YourKernel>()
{
  InputParameters params = validParams<Kernel>();
  params.addParam<Real>("value", 1.0e-5, "Initial Value");
  params.addCoupledVar("temp", "Coupled Temperature");
  return params;
}

C++ Standard Template Library (STL) Data Structures

Using the C++ Vector Container

#include <vector>
int main()
{
  // start with 10 elements
  std::vector<int> v(10);
  for (unsigned int i=0; i<v.size(); ++i)
    v[i] = i;
}

#include <vector>
int main()
{
  // start with 0 elements
  std::vector<int> v;
  for (unsigned int i=0; i<10; ++i)
    v.push_back(i);
}

#include <vector>
int main()
{
  // start with 0 elements
  std::vector<int> v;
  v.resize(10);  // creates 10 elements
  for (unsigned int i=0; i<10; ++i)
    v[i] = i;
}

More features

  • Containers can be nested to create more versatile structures
std::vector<std::vector<Real> > v;
  • To access the items:
for (unsigned int i=0; i < v.size(); ++i)
  for (unsigned int j=0; j < v[i].size(); ++j)
    std::cout << v[i][j];