This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Fortran modules and C++ with Eigen

Tags: None
(comma "," separated)
ganeshg
Registered Member
Posts
1
Karma
0

Fortran modules and C++ with Eigen

Tue Jan 20, 2015 4:28 pm
I am a Fortran user and a complete newbie to C++ and Eigen. I use modules in Fortran to be able to keep my variables, arrays and matrices in different groups and use them as needed. How to implement the idea of modules in Fortran using the functions/classes in C++? I cannot paste the entire Fortran code as it is too large and there will be many new concepts which I would like to write directly in C++. Perhaps, a sample snippet or document showing how to pass on the information between C++ routines could be useful.


Code: Select all
 ! module file: module.f95
module global
save
integer, allocatable :: kod(:,:)
end module

module local
save
integer, allocatable :: kode(:)
real*8, allocatable :: func1(:)
end module

module fileunits
save
integer,parameter :: file1 = 11, file2 = 12
end module


program main

use global
use local
use fileunits

implicit none

int i,j,k,n

open(unit=file1,file='input.dat')
open(unit=file2,file='output.dat')
read(file1, *) n
allocate(kod(n,4))
allocate(kode(4), func1(4))
do I = 1, n
   read(file1, *)  kod(i,1), kod(i,2), kod(i,3), kod(i,4)
   do J=1,4
      kode(j) = kode(i,j)
   end do
   call proc1
   write(file2,*)  ((func1(k), k =1, 4)
   
end do


end program

subroutine proc1
use local

implicit none
int j

do j=1,
   funct1(j) = some function of kode(j)
   do some more operations..
end do

end subroutine
aluaces
Registered Member
Posts
13
Karma
0
OS
This is more of a C++ question rather than a Eigen question.

The equivalent to modules in C++ would be classes, except that the latter is a more generic concept as you can have many objects of a class, whereas in Fortran a module cannot have several instances.

In fact, the examples you posted look like a bunch of global variables only categorized by the module scope. You can mimic this directly using C++ namespaces.
LMHmedchem
Registered Member
Posts
10
Karma
0
Depending on the fortran compiler you use, you may be able to call a c++ function from your fortran code. Most fortran is now compiled as c under the hood. This is true with the gnu compilers, g77, gfortran, etc.

Unless there is some issue with your compiler, you should be able to create a c++ function with your eigen code and call that c++ function directly from you fortran code. You just keep the c++ code in it's own src file and add a compile rule to you make file to compile that src file with g++, or whatever you are using. The linker should be able to find the function and go from there. There are a few eccentricities about how to declare the function, but it's not too bad to get working. You generally end up with two variable spaces, one for fortran and one for c. You will need to pass data back and forth and that will have to be done by passing addresses. That is easy enough with float, double, int and char types, but harder with arrays. Since the c++ and fortran src are processed by different pre-processors, the c++ code will not have any notion of your fortran global variables so you will have to pass a addresses, or make a copy and pass the addresses of the copy.

I have added allot of c++ code to older fortran programs to take advantage of classes and all of the c+ standard library functions and this is not uncommon. I also have allot of newer programs that are c++ apps that call fortran subroutines and make use of mature fortran code. Let me know if you want to give that a try and I will post some examples. As aluaces mentioned, this is more of a programming question, so it might be better to post on a programming forum. There may be some interest here as well since I'm sure you are not the only one to call eigen from existing code in another language.

I'm sure the moderators will let us know if they would prefer this conversation to be elsewhere.

LMHmedchem


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]