Wiki

Clone wiki

krome_bootcamp_2016_ex / how_add_procedure

Back to main

How to add a procedure/method to the proto class


You can add subroutines and functions to the class.
Two things are required to do this:

  1. Write the subroutine/function;
  2. Define it as a class procedure.

Write a subroutine/function

If, for example, you want to add a subroutine with two arguments arg1 and arg2, both type real*8, you should write (e.g. in proto_mods.f90) something like:

#!fortran
subroutine my_awesome_sub(this, arg1, arg2)
  implicit none
  class(proto),intent(inout)::this
  real*8,intent(in)::arg1,arg2

  print *, this%density(1)*arg1*arg2

end subroutine my_awesome_sub

Note that this represents the class instance in PROTO. Thus, in this case you are multiplying the density of the first cell in the class this%density(1) by arg1 and arg2.


Define the class procedure/method

Now alias your subroutine/function to a class procedure in proto_class.f90 as:

#!fortran
  procedure::myAwesomeSub=>my_awesome_sub

where myAwesomeSub can be any reasonable procedure name.
This procedure can now be called by (e.g. in main.f90):

#!fortran
  p = proto() ! create an instance of the PROTO class
  call p%myAwesomeSub(1d5, 1d-20)
An instance of the class is created in the first line, and 1d5 and 1d-20 are the two arguments that you provide to your procedure.


Additional references

See also how to access to procedures and attributes.

For more (generic) details about "type-bound procedures" (methods) and "components" (attributes):, see here.

Updated