Registered Member
|
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.
|
Registered Member
|
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. |
Registered Member
|
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 |
Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]