tools/FortranCPP.h File Reference
Header for macros to call fortran code.  
More...
Detailed Description
Header for macros to call fortran code. 
- Author:
 - JC.Fabre <fabrejc@supagro.inra.fr>, C.Dagès <dages@supagro.inra.fr>
 
- See also:
 - http://www-h.eng.cam.ac.uk/help/tpl/languages/mixinglanguages.html 
http://www.neurophys.wisc.edu/comp/docs/notes/not017.html 
http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html 
http://arnholm.org/software/cppf77/cppf77.htm 
http://www.aei.mpg.de/~jthorn/c2f.html
 
Below are examples of C++ simulation function calling Fortran subroutines.
fortran source code: 
subroutine multrealvalue(value,mult,result)
implicit none
real*8 value
real*8 mult
real*8 result
result=value*mult
return
end
! ==============================================================================
subroutine multintvalue(value,mult,result)
implicit none
integer value
integer mult
integer result
result=value*mult
return
end
! ==============================================================================
subroutine multrealmatrix(matrix,dim1,dim2,mult,result)
implicit none
integer dim1
integer dim2
real*8 matrix(dim2,dim1)
integer mult
real*8 result(dim2,dim1)
integer i, j, k
subroutine multrealvalue(value,mult,result)
implicit none
real*8 value
real*8 mult
real*8 result
result=value*mult
return
end
! ==============================================================================
subroutine multintvalue(value,mult,result)
implicit none
integer value
integer mult
integer result
result=value*mult
return
end
! ==============================================================================
subroutine multrealmatrix(matrix,dim1,dim2,mult,result)
implicit none
integer dim1
integer dim2
real*8 matrix(dim2,dim1)
integer mult
real*8 result(dim2,dim1)
integer i, j, k
do j=1,dim1
  do i=1,dim2
    result(i,j) = matrix(i,j) * mult
  end do
end do
return
end
do j=1,dim1
  do i=1,dim2
    result(i,j) = matrix(i,j) * mult
  end do
end do
return
end
C++ code calling fortran subroutines: 
BEGIN_EXTERN_FORTRAN
  EXTERN_FSUBROUTINE(multrealvalue)(FREAL8*,FREAL8*,FREAL8*);
  EXTERN_FSUBROUTINE(multintvalue)(FINT*,FINT*,FINT*);
  EXTERN_FSUBROUTINE(multrealmatrix)(FREAL8*,FINT*,FINT*,FINT*,FREAL8*);
END_EXTERN_FORTRAN
bool FortranFunction::runStep(const openfluid::base::SimulationStatus* SimStatus)
{
  int i;
  
  double DValue, DMult, DResult;
  DValue = 1.5436;
  DMult = 2.5;
  DResult = 0.0;
  CALL_FSUBROUTINE(multrealvalue)(&DValue,&DMult,&DResult);
  if (abs(DResult - (DValue*DMult)) > m_Precision)
    OPENFLUID_RaiseError("tests.fortran","incorrect fortran call (multrealvalue)");
  
  int IValue, IMult, IResult;
  IValue = 45;
  IMult = 18;
  IResult = 0;
  CALL_FSUBROUTINE(multintvalue)(&IValue,&IMult,&IResult);
  if (IResult != (IValue*IMult))
    OPENFLUID_RaiseError("tests.fortran","incorrect fortran call (multintvalue)");
  
  int MMult, MDim1,MDim2;
  double *MValue;
  double *MResult;
  MMult = 3;
  MDim1 = 2;
  MDim2 = 3;
  MValue = new double[MDim1*MDim2];
  MResult = new double[MDim1*MDim2];
  for (i=0; i < MDim1*MDim2;i++) MValue[i] = 1.5;
  for (i=0; i < MDim1*MDim2;i++) MResult[i] = 0.0;
  CALL_FSUBROUTINE(multrealmatrix)(MValue,&MDim1,&MDim2,&MMult,MResult);
  for (i=0; i < MDim1*MDim2;i++)
  {
    if (abs(MResult[i] - (MValue[i] * MMult)) > m_Precision)
      OPENFLUID_RaiseError("tests.fortran","incorrect fortran call (multrealmatrix)");
  }
  return true;
}
  
Define Documentation
      
        
          | #define CALL_FFUNCTION           | 
          ( | 
          x  | 
                     | 
           )  | 
             x##_ | 
        
      
 
Macro for calling an external fortran function 
- Parameters:
 - 
  
    | [in]  | x  | the name of the function  | 
  
 
 
 
      
        
          | #define CALL_FMODFUNCTION           | 
          ( | 
          x,          | 
           | 
           | 
          y  | 
                     | 
           )  | 
             __##x##__##y | 
        
      
 
Macro for calling an external fortran90 function in a module 
- Parameters:
 - 
  
    | [in]  | x  | the name of the module  | 
    | [in]  | y  | the name of the function  | 
  
 
 
 
      
        
          | #define CALL_FMODSUBROUTINE           | 
          ( | 
          x,          | 
           | 
           | 
          y  | 
                     | 
           )  | 
             __##x##__##y | 
        
      
 
Macro for calling an external fortran90 subroutine in a module 
- Parameters:
 - 
  
    | [in]  | x  | the name of the module  | 
    | [in]  | y  | the name of the subroutine  | 
  
 
 
 
      
        
          | #define CALL_FSUBROUTINE           | 
          ( | 
          x  | 
                     | 
           )  | 
             x##_ | 
        
      
 
Macro for calling an external fortran subroutine 
- Parameters:
 - 
  
    | [in]  | x  | the name of the subroutine  | 
  
 
 
 
      
        
          | #define EXTERN_FFUNCTION           | 
          ( | 
          x  | 
                     | 
           )  | 
             x##_ | 
        
      
 
Macro for declaration of an external fortran function 
- Parameters:
 - 
  
    | [in]  | x  | the name of the function  | 
  
 
 
 
      
        
          | #define EXTERN_FMODFUNCTION           | 
          ( | 
          x,          | 
           | 
           | 
          y  | 
                     | 
           )  | 
             __##x##__##y | 
        
      
 
Macro for declaration of an external fortran90 function in a module 
- Parameters:
 - 
  
    | [in]  | x  | the name of the module  | 
    | [in]  | y  | the name of the function  | 
  
 
 
 
      
        
          | #define EXTERN_FMODSUBROUTINE           | 
          ( | 
          x,          | 
           | 
           | 
          y  | 
                     | 
           )  | 
             void __##x##__##y | 
        
      
 
Macro for declaration of an external fortran90 subroutine in a module 
- Parameters:
 - 
  
    | [in]  | x  | the name of the module  | 
    | [in]  | y  | the name of the subroutine  | 
  
 
 
 
      
        
          | #define EXTERN_FSUBROUTINE           | 
          ( | 
          x  | 
                     | 
           )  | 
             void x##_ | 
        
      
 
Macro for declaration of an external fortran subroutine 
- Parameters:
 - 
  
    | [in]  | x  | the name of the subroutine  | 
  
 
 
 
Macro for fortran INT type in C++ (int) 
 
 
Macro for fortran INT*2 type in C++ (short int) 
 
 
Macro for fortran INT*8 type in C++ (long int) 
 
 
Macro for fortran REAL type in C++ (float) 
 
 
Macro for fortran REAL type in C++ (double)