Silverfrost Forums

Welcome to our forums

Fortran code does not compile (modules do not compile)

2 Feb 2013 3:13 #11502

Hi everyone, My classmate and I are working with Fortran to do programming in numerical macro. To program, I am using Silverfrost. However, for one of our assignments, my classmate used modules. When I try to run the main code, there is an error:

error 404 - Cannot find definition for MODULE GLOBAL_VARIABLE.

However, when I tried running the modules separately, and when I build the solution, the compiler tells me that the compilation failed.

Is it a problem with the compiler?

2 Feb 2013 3:26 #11503

Just a guess, and that is that you put your program first in the source code: when using modules they have to come first.

E

2 Feb 2013 4:37 #11504

Actually the main code of the program is like this:

program main

use global_variable ! Define the global parameters that will be used in this project use functions ! Define objective functions use auxfuncs ! basis functions to approximate our goal function use powell_optim

implicit none

real(8) :: interval, lambda(Ibis), func_value(Ibis) integer :: j, counter2

spot_plot = linspace(Blower,Bupper,fPlot) spot_plot2 = linspace(Blower,Bupper,JJ) x_grid = linspace(Blower,Bupper,Ibis) lambda =0 j = 1 interval = Bupper - Blower h = interval / JJ

! Chebyshev are defines over [-1,1], we convert [0,5] to [-1,1] first x_newgrid = 2*(x_grid-Blower) / interval -1 spot_plotnew = 2*(spot_plot-Blower) / interval -1

!choose which method to use to approximating !first calculate the value of Ibis points for object do j=1,Ibis func_value(j) = func(x_grid(j)) end do

!******************************************! ! MSE method guess_results = 0 xmatrix = 0 do j=1,Ibis xmatrix(j,j)=1 end do call powell(guess_results,xmatrix,ftoll,iterr,frett)

do j=1,fPlot func_approxi_plot2(j) = sum(guess_results*func_basis(spot_plot(j))) end do

!%%%%%%%%%%%%%%%%%%%%%%! if (types2 ==1) then ! calculate the weighting vector for the basis do j=1,Ibis func_approxi(j,:)=func_basis(x_grid(j)) end do call inverse(func_approxi,invi,Ibis)

    do j=1,Ibis
             lambda(j) = sum(func_value*invi(j,:))
    end do

!calculate the data for plotting do j=1,fPlot func_value_plot(j) = func(spot_plot(j)) func_approxi_plot(j) = sum(lambda*func_basis(spot_plot(j))) end do

!%%%%%%%%%%%%%%%%%%%%%%%! else if (types2 == 2) then do j=1,Ibis func_approxi(j,:)=func_basis(x_newgrid(j)) end do call inverse(func_approxi,invi,Ibis)

do j=1,Ibis
   lambda(j) = sum(func_value*invi(j,:))
end do

!%%%%%%%%%%%%%%%%%%%%%%%! else lambda = func_value do j=1,fPlot func_value_plot(j) = func(spot_plot(j)) func_approxi_plot(j) = sum(lambda*func_basis(spot_plot(j))) end do end if

!%%%%%% save data %%%%%%%%! open(unit=9,file='lambda_MSE.txt') write(9, '(1G14.6)') guess_results close(9) open(unit=9,file='lambda.txt') write(9, '(1G14.6)') lambda close(9) open(unit=9,file='functionvalue.txt') write(9, '(1G14.6)') func_value_plot close(9) open(unit=9,file='axis.txt') write(9,'(1G14.6)') spot_plot close(9) open(unit=9,file='function_approximation_value.txt') write(9,'(1G14.6)') func_approxi_plot close(9)

open(unit=9,file='function_approximation_value_MSE.txt') write(9,'(1G14.6)') func_approxi_plot2 close(9) ! if (flag_normtest == 1) then
! open(unit=14,file='normality_tests_expansion') ! do i = 1, fPlot ! write(10,100) normaltest_normal(i,:) ! write(11,100) normaltest_t(i,:) ! write(12,100) normaltest_kotz(i,:) ! write(13,100) normaltest_mixture(i,:) ! write(14,100) normaltest_expansion(i,:) ! end do
! close(14) ! end if

end program main

2 Feb 2013 4:41 #11505

Hence, what you mean is that before I begin with the main program, I have to put the use above main program?

2 Feb 2013 6:19 #11506

Your code has the following USE statements:

use global_variable ! Define the global parameters that will be used in this project 
use functions ! Define objective functions 
use auxfuncs ! basis functions to approximate our goal function 
use powell_optim 

if the statements refer to modules you programmed yourself, all the module source code has to come before the PROGRAM statement. If they are precompiled modules they have to be somewhere where the compiler can find them.

E

3 Feb 2013 10:12 #11507

What you mean is, instead of me writing them as separate modules, I put everything that I wrote in the main source code before the main program? Like this?

MODULE XXXXX [STATEMENTS] END MODULE XXXXX

PROGRAM YYYYY USE XXXXX END PROGRAM YYYYY

3 Feb 2013 4:08 #11508

That's the first of two alternatives. What definitely won't work is to put the program block first.

There is nothing wrong with doing a separate compilation, but the modules have to exist, and be available when the compiler deals with the PROGRAM block. Otherwise, and this is so obvious that it hardly needs saying, the program block cannot be compiled, which is what the error message is telling you: during compilation, the compiler can't find the module.

E

3 Feb 2013 7:44 #11509

Then, in that case where I can compile separately, do I first compile the modules, and compile the main program after? I tried to do that, and I still ran into problems since it tells me that the module failed to compile.

6 Feb 2013 9:27 #11527

If you use different files, you must compile in an order that means each module is compiled before any other file that uses it.

If you use Plato, Visual Studio, or the Visual Studio Shell (FTN95express) this will be done for you.

Please login to reply.