Fatal Error: Cannot open module file

Hi all,
I am modifying the Community Earth System Model (CESM). When I try to compile Fortran codes, there is an error .

/home/yuansun/Desktop/ForCodeTest/my_cesm_sandbox/components/clm/src/main/controlMod.F90:37:6:

37 | use UrbanParamsType , only: UrbanReadNML
| 1
Fatal Error: Cannot open module file ‘urbanparamstype.mod’ for reading at (1): No such file or directory
compilation terminated.
make: *** [/home/yuansun/projects/cesm/scratch/testrun2/Tools/Makefile:888: controlMod.o] Error 1
make: *** Waiting for unfinished jobs…

I have checked that ‘UrbanParamsType.F90’ exist at the right location, Could anyon help me? Thanks, Yuan

Welcome to the forum!

The source file is compiled and one of the results is a “mod” file, an intermediate file that the compiler uses for information about the module(s) the source file contains. The location of that file does not have to be the location of the source file. The compiler will accept options to specify the location of such files and apparently there is some mismatch. Or it could be that the source file was not yet compiled when the module it contains was required.
Yet another possibility: you made a mistake in editing the source file UrbanParamsType.F90 and the compiler did not produce the mod file.

In short: a number of things could go wrong and you will need to find out what. I do not know the CESM model, so I cannot be more specific.

@YuanSun ,

Welcome to Fortran Discourse!

Re: “Could anyon help me?” - please never forget that “anyone” who can help you is you yourself! And when it comes to Fortran, a good place to start is the following:

Re: your specific question, please note the issue has to do with specific intricacies with static compilation steps around Fortran processors (compilers) and file locations, etc.

Say, you have a file named controlMod.f90 like so in a particular folder:

   use UrbanParamsType, only : UrbanReadNML
end
C:\temp\>dir
..
 Directory of C:\temp

10/20/2023  10:32 AM    <DIR>          .
10/20/2023  10:32 AM    <DIR>          ..
10/20/2023  10:32 AM                50 controlMod.f90
10/20/2023  10:31 AM    <DIR>          utils
               1 File(s)             50 bytes
..

Then if you try the process the Fortran source as follows using gfortran compiler:

C:\temp>gfortran -c controlMod.f90
controlMod.f90:1:8:

    1 |    use UrbanParamsType, only : UrbanReadNML
      |        1
Fatal Error: Cannot open module file 'urbanparamstype.mod' for reading at (1): No such file or directory
compilation terminated.

So you will notice gfortran is looking for a compiled module file ‘urbanparamstype.mod’ and unable to locate it and issuing the error.

Whereas you may have the file but it may be in a separate location (say as an example c:\temp\utils) but which gfortran was not informed of:

C:\temp>cd utils

C:\temp\utils>dir
..
 Directory of C:\temp\utils

10/20/2023  10:41 AM    <DIR>          .
10/20/2023  10:41 AM    <DIR>          ..
10/20/2023  10:31 AM                96 UrbanParamsType.f90
10/20/2023  10:40 AM               244 urbanparamstype.mod
10/20/2023  10:40 AM               743 UrbanParamsType.o
               6 File(s)          1,083 bytes
..

So knowing this, if you inform the path of the .mod file, then you will find the compiler complete the processing with no errors:

C:\temp>gfortran -c -Ic:\temp\utils controlMod.f90

C:\temp>

Look at the link below re: how the -Idir option helped resolve the error:

Thanks Arjen and Fan,

CESM is a large model with thousands of Fortran Codes. So compiling a seperate F90 file does not work.

It is strange that ‘UrbanParamsType.mod’ failed to be generated based on ‘UrbanParamsType.F90’ without a specific error.

I could not see more on where is wrong in the ‘UrbanParamsType.F90’ script.

Thanks,
Yuan

CESM must come with some build system(s) (e.g., CMake, fpm, etc.) with instructions as to how to use them.

Use the same system(s) and follow the exact steps in such instructions.

Colloquially this is known as RTFM.
https://escomp.github.io/CESM/versions/cesm2.2/html/

@YaunSun, you may want to learn to use the @ symbol followed by their exact nom de plume (e.g., @Arjen) to refer to other readers who comment to your posts.

@FortranFan I see. It is true that CESM has a build system.

When I run ./case.build, the command starts compiling Fortran scripts.

But, still unknow haha :frowning:

Thanks, Yuan

Depending how well the module dependencies are captured in the Makefiles (i.e if these are generated by a tool like CMake versus written manually), it’s possible to get into a state where building in parallel is broken. (I’m guessing this could be the case, because of the “Waiting for unfinished jobs” message). You could try to serialize the build with make -j1, and see if that works (running from a clean directory).

Probably you’ll receive better advice if you go directly to the CESM Slack Channel: https://cesm2.slack.com/

Thank you @ivanpribec . I found that

cat: Srcfiles: No such file or directory
make: Circular controlMod.o ← urbanparamstype.mod dependency dropped.

This might be the reason why ‘urbanparamstype.mod’ is dropped.

Best,
Yuan