Since version 10, Octopus is gradually being converted into a fully object oriented code, using the OOP features of Fortran 2003.
The main features of OOP used in Octopus are:
Encapsulation
Inheritance
The benefits of OOP design are:
less code duplication
better readable code
code can be closer to the physics
low level aspects can be hidden
What is an object?
An object refers to a data-structure, which is bundled with the functions (methods) acting on that data. Usually, it is good practice to declare the data as private,
and allow access only through so-called access functions (getters and setters). This is called encapsulation, and allows later to change details of the implementation
without affecting the rest of the code.
Objects are instances of classes, which define the data structures and the methods for its objects.
Inheritance and class hierarchy
Object oriented programming allows to build more complex classes by inheriting the structure and behaviour of other classes.
This reduces duplicated code, and ensures consistency between different classes, which share some functionality.
Example: linked list
Many classes require the functionality to iterate over a list of objects. These objects, however depend on the nature of the specific class.
In the diagram below, you see the inheritance tree of the linked_list_t class, which is the parent for many more specialized classes.
Templating
Languages like C++ allow to use templates to write functions or algorithms independent of the data type, they are to be applied to.
Unfortunately, this is not directly possible in Fortran. Octopus uses a poor-mans approach to templates, by using macros and including ‘templates’ of functions, where the explicit types are replaced by macros. These routines are usually defined in files which have ‘_inc’ in their name.
To create a ‘templated’ function, one can either provide an explicit interface
In my_function_inc.F90 we would have something like:
function X(my_function)(arg1,arg2)result(res))R_TYPE,intent(in)::arg1R_TYPE,intent(in)::arg2R_TYPE,intent(out)::res...end function X(my_function)
The used macros are defined in these include files:
include/real.F90
!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
!!
!! This program is free software; you can redistribute it and/or modify
!! it under the terms of the GNU General Public License as published by
!! the Free Software Foundation; either version 2, or (at your option)
!! any later version.
!!
!! This program is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!! GNU General Public License for more details.
!!
!! You should have received a copy of the GNU General Public License
!! along with this program; if not, write to the Free Software
!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
!! 02110-1301, USA.
!!
#define R_TREAL 1
#define R_TYPE FLOAT
#define R_BASE FLOAT
#define R_DOUBLE real(real64)
#define R_MPITYPE MPI_FLOAT
#define R_TYPE_VAL TYPE_FLOAT
#define R_TYPE_CL 'RTYPE_DOUBLE'
#define R_TYPE_IOBINARY TYPE_DOUBLE
#define R_TOTYPE(x) real(x, REAL_PRECISION)
#define R_CONJ(x) (x)
#define R_REAL(x) (x)
#define R_AIMAG(x) (M_ZERO)
#define X(x) d ## x
#define pX(x) pd ## x
#define aX(x,y) x ## d ## y
#define R_SIZEOF 8
#define R_ADD 1
#define R_MUL 1
!! Local Variables:
!! mode: f90
!! coding: utf-8
!! End:
include/complex.F90
!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
!!
!! This program is free software; you can redistribute it and/or modify
!! it under the terms of the GNU General Public License as published by
!! the Free Software Foundation; either version 2, or (at your option)
!! any later version.
!!
!! This program is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!! GNU General Public License for more details.
!!
!! You should have received a copy of the GNU General Public License
!! along with this program; if not, write to the Free Software
!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
!! 02110-1301, USA.
!!
#define R_TCOMPLEX 1
#define R_TYPE CMPLX
#define R_BASE FLOAT
#define R_DOUBLE complex(real64)
#define R_MPITYPE MPI_CMPLX
#define R_TYPE_VAL TYPE_CMPLX
#define R_TYPE_CL 'RTYPE_COMPLEX'
#define R_TYPE_IOBINARY TYPE_DOUBLE_COMPLEX
#define R_TOTYPE(x) cmplx(x, M_ZERO, REAL_PRECISION)
#define R_CONJ(x) conjg(x)
#define R_REAL(x) real(x, REAL_PRECISION)
#define R_AIMAG(x) aimag(x)
#define R_SIZEOF 16
#define R_ADD 2
#define R_MUL 6
#define X(x) z ## x
#define pX(x) pz ## x
#define aX(x,y) x ## z ## y
!! Local Variables:
!! mode: f90
!! coding: utf-8
!! End:
include/integer.F90
!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
!!
!! This program is free software; you can redistribute it and/or modify
!! it under the terms of the GNU General Public License as published by
!! the Free Software Foundation; either version 2, or (at your option)
!! any later version.
!!
!! This program is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!! GNU General Public License for more details.
!!
!! You should have received a copy of the GNU General Public License
!! along with this program; if not, write to the Free Software
!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
!! 02110-1301, USA.
!!
#define R_TINTEGER 1
#define X(x) i ## x
#define R_TYPE integer
#define R_BASE integer
#define R_TYPE_VAL TYPE_INTEGER
#define R_MPITYPE MPI_INTEGER
#define R_TYPE_IOBINARY TYPE_INT_32
#define R_TOTYPE(x) (x)
#define R_SIZEOF 4
!! Local Variables:
!! mode: f90
!! coding: utf-8
!! End:
include/undef.F90
!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
!!
!! This program is free software; you can redistribute it and/or modify
!! it under the terms of the GNU General Public License as published by
!! the Free Software Foundation; either version 2, or (at your option)
!! any later version.
!!
!! This program is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!! GNU General Public License for more details.
!!
!! You should have received a copy of the GNU General Public License
!! along with this program; if not, write to the Free Software
!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
!! 02110-1301, USA.
!!
#ifdef R_TREAL
#undef R_TREAL
#endif
#ifdef R_TCOMPLEX
#undef R_TCOMPLEX
#endif
#ifdef R_TINTEGER
#undef R_TINTEGER
#endif
#undef X
#undef pX
#undef aX
#undef TS
#undef R_TYPE
#undef R_BASE
#undef R_TYPE_VAL
#undef R_TYPE_CL
#undef R_TYPE_IOBINARY
#undef R_DOUBLE
#undef R_MPITYPE
#undef R_TOTYPE
#undef R_SIZEOF
#undef R_ADD
#undef R_MUL
#undef R_CONJ
#undef R_REAL
#undef R_AIMAG
!! Local Variables:
!! mode: f90
!! coding: utf-8
!! End: