#### Compiling swt_v4_0.0.3 using the gfortran compiler on Max OS X 10.6
#### Theo Olsthoorn 091010 - 091104

#### History of the process, #### are my comments and considerations
#### The final make file is included at the bottom of this file.



##### I downloaded new swt_v4_003 on 091009 version 4.003

#####  adapting the makefile for mf2k which has the same .c files

#### in COBJECTS .o objects replace by those of seawat
#### names of c files in CGC parameter in makefile in uppercase as on disk
#### para-non.o placed at the end of the list of objects
#### vdf1.o (vdfmodule) at the front of the objects

======Compiling: =========================================================
gmg1.f:53.72:

      USE MHC, ONLY: MHC1ALG                                            
                                                                       1
Fatal Error: Can't open module file 'mhc.mod' for reading at (1): No such file or directory
make: *** [gmg1.o] Error 1

==========================================================================

##### looking what’s wrong?? --- module MHC is mssing, only in gmg1.f

-bash$>grep MHC *.f
gmg1.f:      USE MHC, ONLY: MHC1ALG

##### is is in mhc1.f80 !!

mhc1.f90:MODULE MHC

#### adapted lines in makefile te make sure it is compiled before requested
#### see $(MHC).mod

#### define samewhere in the top of the makefile
MHC = mhc1

#### then further down add %(MHC).mod to the prerequisits of $(PROGRAM) :
$(PROGRAM): $(MHC).mod $(OBJECTS) $(GMG)
	-$(F77) $(F77FLAGS) -o $(PROGRAM) $(OBJECTS) $(GMG) $(USRLIB) $(SYSLIBS)

#### then adapt the rule for the .f90 file holding this module
$(MHC).mod:
	$(F90) $(F90FLAGS) -c $(MHC).f90


#### run makefile

=============== Compiling ==================================================
vdf1lpf1.f:1140.72:

      USE VDFVSCMODULE,   ONLY: VISCREF,MU                              
                                                                       1
Fatal Error: Can't open module file 'vdfvscmodule.mod' for reading at (1): No such file or directory
make: *** [vdf1lpf1.o] Error 1
~/GRWMODELS/swt_v4_00_03/source
-bash$>grep VDFVSC* *
vdf1lpf1.f:      USE VDFVSCMODULE,   ONLY: VISCREF,MU
vdf1vsc1.f:      MODULE VDFVSCMODULE

===============================================================================
##### so we move vdf1vsc1.o in front of the objects list toe make sure it exists when requested
##### run make again

-bash$>make
gfortran  -c mhc1.f90
gfortran  -c vdf1vsc1.f
gfortran  -c vdf1lpf1.f
gfortran  -c vdf1mnw1.f
gfortran  -c vdf1rch6.f
gfortran  -c vdf1riv6.f
gfortran  -c vdf1wel6.f
gfortran  -I. -c serial/para-non.f
gfortran -D_UF -O3 -c r_vector.c
gfortran -D_UF -O3 -c SOLVERS.c
gfortran -D_UF -O3 -c CCFD.c
gfortran -D_UF -O3 -c MF2KGMG.c
gfortran  -o mf2k vdf1.o vdf1vsc1.o swt_v4.o ctime.o daf1.o de45.o glo1bas6.o gmg1.o gutsdaf.o gwf1bas6.o gwf1bcf6.o gwf1chd6.o gwf1drn6.o gwf1drt1.o gwf1ets1.o gwf1evt6.o gwf1fhb1.o gwf1gag5.o gwf1ghb6.o gwf1hfb6.o gwf1huf2.o gwf1ibs6.o gwf1lak3.o gwf1lpf1.o gwf1mnw1.o gwf1rch6.o gwf1res1.o gwf1riv6.o gwf1sfr2.o gwf1str6.o gwf1sub1.o gwf1swt1.o gwf1wel6.o hufutl2.o hydmod.o imt1adv5.o imt1btn5.o imt1dsp5.o imt1fmi5.o imt1gcg5.o imt1rct5.o imt1ssm5.o imt1tob5.o imt1utl5.o lmg1.o lmt6.o memchk.o obs1adv2.o obs1bas6.o obs1bas6vd.o obs1drn6.o obs1drn6vd.o obs1drt1.o obs1drt1vd.o obs1ghb6.o obs1ghb6vd.o obs1riv6.o obs1riv6vd.o obs1str6.o parutl1.o pcg2.o pes1bas6.o pes1gau1.o rtedaf.o sen1bas6.o sen1chd6.o sen1drn6.o sen1drt1.o sen1ets1.o sen1evt6.o sen1ghb6.o sen1hfb6.o sen1huf2.o sen1lpf1.o sen1rch6.o sen1riv6.o sen1str6.o sen1wel6.o sip5.o sor5.o utl6.o vdf1bas6.o vdf1bcf6.o vdf1chd6.o vdf1drn6.o vdf1drt1.o vdf1ets1.o vdf1evt6.o vdf1fhb1.o vdf1ghb6.o vdf1huf2.o vdf1lmt6.o vdf1lpf1.o vdf1mnw1.o vdf1rch6.o vdf1riv6.o vdf1wel6.o para-non.o  r_vector.o SOLVERS.o CCFD.o MF2KGMG.o gmg1.o  -lc
ld: duplicate symbol _gmg1alg_ in gmg1.o and gmg1.o
collect2: ld returned 1 exit status
make: [mf2k] Error 1 (ignored)
~/GRWMODELS/swt_v4_00_03/source

###### NOTE the object name is still indavertently mf2k. I hadnot yet adapted
###### the variable PROGRAM=mf2k since I copied the make file from mf2k source dir.
###### I’ll change that later. It’s just the name and doesn’t affect the outcome

#### Conclusion from the ld: line above: we have the same object gmg1.o twice  somewhere

#### it was both in the GMG and the OBJECTS list. Because we have gmg1.f in seawat
#### contrary to gmg1.c in mf2k, we remove gmg1.o from the GMG list

#### make again, everything compiles, so we run into the linking phase

============ Compiling ======================================================================
#### the compiler spawns some warnings though
imt1adv5.f:473.72:

        IF(ICBUND(JJ,II,KK).EQ.0) GOTO 100                              
                                                                       1
imt1adv5.f:686.72:

  100 ENDDO                                                             
                                                                       2
Warning: Deleted feature: GOTO at (1) jumps to END of construct at (2)
imt1adv5.f:750.72:
#### wich may be prevented this way

        IF(ICBUND(JJ,II,KK).EQ.0) GOTO 100                              
		.... statements ...
      ENDDO                                                             
  100 CONTINUE                                                             
as was done in MT3D, but it not necessary now

##### I had trouble with names missing for the linker. At this point I installed
##### the new verstion of gfortran and ran the building process again.

=========== Compiling ===================================================================

##### name’s problem still existed:

#### we finally end up with some unmatched names due to piculiar name mangling
#### at the interaface between c and fortran
#### same trouble as with mk2f but there gmg1 was a c-module
#### and we solved the name mismatch by routing the c files through
#### the fortran compiler as we do here. But here it doesn’t work

gfortran  -o mf2k vdf1.o vdf1vsc1.o swt_v4.o ctime.o daf1.o de45.o glo1bas6.o gmg1.o gutsdaf.o gwf1bas6.o gwf1bcf6.o gwf1chd6.o gwf1drn6.o gwf1drt1.o gwf1ets1.o gwf1evt6.o gwf1fhb1.o gwf1gag5.o gwf1ghb6.o gwf1hfb6.o gwf1huf2.o gwf1ibs6.o gwf1lak3.o gwf1lpf1.o gwf1mnw1.o gwf1rch6.o gwf1res1.o gwf1riv6.o gwf1sfr2.o gwf1str6.o gwf1sub1.o gwf1swt1.o gwf1wel6.o hufutl2.o hydmod.o imt1adv5.o imt1btn5.o imt1dsp5.o imt1fmi5.o imt1gcg5.o imt1rct5.o imt1ssm5.o imt1tob5.o imt1utl5.o lmg1.o lmt6.o memchk.o obs1adv2.o obs1bas6.o obs1bas6vd.o obs1drn6.o obs1drn6vd.o obs1drt1.o obs1drt1vd.o obs1ghb6.o obs1ghb6vd.o obs1riv6.o obs1riv6vd.o obs1str6.o parutl1.o pcg2.o pes1bas6.o pes1gau1.o rtedaf.o sen1bas6.o sen1chd6.o sen1drn6.o sen1drt1.o sen1ets1.o sen1evt6.o sen1ghb6.o sen1hfb6.o sen1huf2.o sen1lpf1.o sen1rch6.o sen1riv6.o sen1str6.o sen1wel6.o sip5.o sor5.o utl6.o vdf1bas6.o vdf1bcf6.o vdf1chd6.o vdf1drn6.o vdf1drt1.o vdf1ets1.o vdf1evt6.o vdf1fhb1.o vdf1ghb6.o vdf1huf2.o vdf1lmt6.o vdf1lpf1.o vdf1mnw1.o vdf1rch6.o vdf1riv6.o vdf1wel6.o para-non.o  r_vector.o SOLVERS.o CCFD.o MF2KGMG.o  -lc
Undefined symbols:
  "___mhc_MOD_mhc_active", referenced from:
      _gmg1ap_ in gmg1.o
      _gmg1ap_ in gmg1.o
      _gmg1da_ in gmg1.o
  "___mhc_MOD_mhc1ad", referenced from:
      _gmg1ap_ in gmg1.o
  "___mhc_MOD_mhc1da", referenced from:
      _gmg1da_ in gmg1.o
  "___mhc_MOD_mhc1it", referenced from:
      _gmg1ap_ in gmg1.o
  "___mhc_MOD_mhc1ot", referenced from:
      _gmg1ap_ in gmg1.o
  "___mhc_MOD_mhc1alg", referenced from:
      _gmg1alg_ in gmg1.o
  "___mhc_MOD_dampmhc", referenced from:
      _gmg1ap_ in gmg1.o
  "___mhc_MOD_bigheadchg", referenced from:
      _gmg1adamp2_ in gmg1.o
      _gmg1adamp2_ in gmg1.o
      _gmg1adamp2_ in gmg1.o
      _gmg1adamp2_ in gmg1.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: [mf2k] Error 1 (ignored)
$>


#### so what’s next????  Inspection of occurrence of the variable in the
#### source and object files:

##### in mhc1.f90

  !   Public data
  PUBLIC :: BIGHEADCHG, DAMPMHC, MHC_ACTIVE

##### in gmg1.f

      USE MHC, ONLY: DAMPMHC, MHC_ACTIVE, MHC1AD, MHC1IT, MHC1OT    !ERB

##### Look at one of the parameters, it was only used in two f files
##### Look at the name spaces of the corresponding object files for this parameter

$>nm mhc1.o gmg1.o | grep -i mhc_active
0000000000000d44 D ___mhc_MOD_mhc_active
                 U ___mhc_MOD_mhc_active

##### So, there cannot be a name conflict here because the parameter is defined in mhc1.o and used in gmg1.o
##### Therefore, we must have a linking problem.
##### See if both objects are in the list echoed by the linker.
##### H’m ... no, mhc1.o is missing.
##### Therefore, inlcude it as follows in the makefile, see $(MHC).o where MHC is defined as mhc1

$(PROGRAM): $(MHC).mod $(OBJECTS) $(GMG)
	-$(F77) $(F77FLAGS) -o $(PROGRAM) $(MHC).o $(OBJECTS) $(GMG) $(USRLIB) $(SYSLIBS)

========= Compiling once more ========================================================

$>make
gfortran  -c mhc1.f90
gfortran  -o swt_v4 mhc1.o vdf1.o vdf1vsc1.o swt_v4.o ctime.o daf1.o de45.o glo1bas6.o gmg1.o gutsdaf.o gwf1bas6.o gwf1bcf6.o gwf1chd6.o gwf1drn6.o gwf1drt1.o gwf1ets1.o gwf1evt6.o gwf1fhb1.o gwf1gag5.o gwf1ghb6.o gwf1hfb6.o gwf1huf2.o gwf1ibs6.o gwf1lak3.o gwf1lpf1.o gwf1mnw1.o gwf1rch6.o gwf1res1.o gwf1riv6.o gwf1sfr2.o gwf1str6.o gwf1sub1.o gwf1swt1.o gwf1wel6.o hufutl2.o hydmod.o imt1adv5.o imt1btn5.o imt1dsp5.o imt1fmi5.o imt1gcg5.o imt1rct5.o imt1ssm5.o imt1tob5.o imt1utl5.o lmg1.o lmt6.o memchk.o obs1adv2.o obs1bas6.o obs1bas6vd.o obs1drn6.o obs1drn6vd.o obs1drt1.o obs1drt1vd.o obs1ghb6.o obs1ghb6vd.o obs1riv6.o obs1riv6vd.o obs1str6.o parutl1.o pcg2.o pes1bas6.o pes1gau1.o rtedaf.o sen1bas6.o sen1chd6.o sen1drn6.o sen1drt1.o sen1ets1.o sen1evt6.o sen1ghb6.o sen1hfb6.o sen1huf2.o sen1lpf1.o sen1rch6.o sen1riv6.o sen1str6.o sen1wel6.o sip5.o sor5.o utl6.o vdf1bas6.o vdf1bcf6.o vdf1chd6.o vdf1drn6.o vdf1drt1.o vdf1ets1.o vdf1evt6.o vdf1fhb1.o vdf1ghb6.o vdf1huf2.o vdf1lmt6.o vdf1lpf1.o vdf1mnw1.o vdf1rch6.o vdf1riv6.o vdf1wel6.o para-non.o  r_vector.o SOLVERS.o CCFD.o MF2KGMG.o  -lc
$>make

##### SUCCESS !!!! linking succeeded ###############################################################

##### Problems occurred when running with a test example ###########################################

# When running the model I run into an error telling that file MT3D001.UCN could not
# be opened. After a long search and running gdb debugger (requiring the -g compiler flag to
# include debug information in the object files). It turned out that
# the form in an open statement was not correct. It was ‘BINARY’ which was illegal to
# gfortran, which requires ‘UNFORMATTED’ instead. The form argument turns out to
# be defined in two include files openspec.inc and filespec.inc. So change them
# accoring to the explanation included in these files. Both files are in fact the same
# the filespec.inc belongs to MT3DMS and openspec.inc to seawat.
#
# This problem solved.
#
##### Crazy heads when running the Henry-Classic test problem ######################################
#
# When running the Henry example as a test I got different results under mac/unix and
# under windows. Especially, the computed heads went nuts under mac/unix !
#
# However, Chris Langevin’s readme file in the source directory warns
# that all REAL variables must be declared double precision.
# Hence I added # the following flags to the compilation (including the double-8 to make sure
# the variable memory size of both will be the same even 
# when changing to 64 bits or any other architecture in the future.
#
F77FLAGS= -fdefault-real-8 -fdefault-double-8 -fno-align-commons
F90FLAGS= -fdefault-real-8 -fdefault-double-8 -fno-align-commons
#
# The -fno-align-commons flag was added after the compiler warned about unaligned commons
# suggesting to add that flag, after which it stayed silent.
#
# Running the model again with the Henry test file yielded the correc answers.
# Theo Olsthoorn 091104
# success







####################################################################################################

########### H E R E    I S    T H E    M A K E F I L E    C O N T E N T S ##########################

##################  The Makefile ###################################################################

# Makefile example for compiling GMG and linking GMG to MF2K:
# Adapted for SEAWAT (swt_v4_003)
# by Theo Olsthoorn 091010
#
# Makefile for non-USGS distribution
#
# @ (#) Makefile for Modtools 27June1997
# (Supplied by L. Orzol -- erb)

PROGRAM = swt_v4.mac

SOURCELOC = 
UTILITYLOC = 
NEWMOD = 
INCDIR= .

# Define the Fortran compile flags

#F77FLAGS= -g   <<< debugging
#F90FLAGS= -g   <<< debugging
F77FLAGS= -fdefault-real-8 -fdefault-double-8 -fno-align-commons
F90FLAGS= -fdefault-real-8 -fdefault-double-8 -fno-align-commons
F77= gfortran
F90= gfortran

# 
# Define the C compile flags
# -D_UF defines UNIX naming conventions for mixed language compilation.
# 
CFLAGS= -D_UF -O3
#CFLAGS= -D_UF -g   <<< for diebugging
CC= gfortran

# Define GMG objects
#
GMG = r_vector.o\
      SOLVERS.o\
      CCFD.o\
      MF2KGMG.o
      
# Define the libraries

SYSLIBS= -lc
USRLIB  = 

# Define all object files which make up Modtools
MHC = mhc1


OBJECTS = \
    vdf1.o \
    vdf1vsc1.o \
    swt_v4.o \
    ctime.o \
    daf1.o \
    de45.o \
    glo1bas6.o \
    gmg1.o \
    gutsdaf.o \
    gwf1bas6.o \
    gwf1bcf6.o \
    gwf1chd6.o \
    gwf1drn6.o \
    gwf1drt1.o \
    gwf1ets1.o \
    gwf1evt6.o \
    gwf1fhb1.o \
    gwf1gag5.o \
    gwf1ghb6.o \
    gwf1hfb6.o \
    gwf1huf2.o \
    gwf1ibs6.o \
    gwf1lak3.o \
    gwf1lpf1.o \
    gwf1mnw1.o \
    gwf1rch6.o \
    gwf1res1.o \
    gwf1riv6.o \
    gwf1sfr2.o \
    gwf1str6.o \
    gwf1sub1.o \
    gwf1swt1.o \
    gwf1wel6.o \
    hufutl2.o \
    hydmod.o \
    imt1adv5.o \
    imt1btn5.o \
    imt1dsp5.o \
    imt1fmi5.o \
    imt1gcg5.o \
    imt1rct5.o \
    imt1ssm5.o \
    imt1tob5.o \
    imt1utl5.o \
    lmg1.o \
    lmt6.o \
    memchk.o \
    obs1adv2.o \
    obs1bas6.o \
    obs1bas6vd.o \
    obs1drn6.o \
    obs1drn6vd.o \
    obs1drt1.o \
    obs1drt1vd.o \
    obs1ghb6.o \
    obs1ghb6vd.o \
    obs1riv6.o \
    obs1riv6vd.o \
    obs1str6.o \
    parutl1.o \
    pcg2.o \
    pes1bas6.o \
    pes1gau1.o \
    rtedaf.o \
    sen1bas6.o \
    sen1chd6.o \
    sen1drn6.o \
    sen1drt1.o \
    sen1ets1.o \
    sen1evt6.o \
    sen1ghb6.o \
    sen1hfb6.o \
    sen1huf2.o \
    sen1lpf1.o \
    sen1rch6.o \
    sen1riv6.o \
    sen1str6.o \
    sen1wel6.o \
    sip5.o \
    sor5.o \
    utl6.o \
    vdf1bas6.o \
    vdf1bcf6.o \
    vdf1chd6.o \
    vdf1drn6.o \
    vdf1drt1.o \
    vdf1ets1.o \
    vdf1evt6.o \
    vdf1fhb1.o \
    vdf1ghb6.o \
    vdf1huf2.o \
    vdf1lmt6.o \
    vdf1lpf1.o \
    vdf1mnw1.o \
    vdf1rch6.o \
    vdf1riv6.o \
    vdf1wel6.o \
    para-non.o 



install: $(PROGRAM)

all: $(PROGRAM)

$(PROGRAM): $(MHC).mod $(OBJECTS) $(GMG)
	-$(F77) $(F77FLAGS) -o $(PROGRAM) $(MHC).o $(OBJECTS) $(GMG) $(USRLIB) $(SYSLIBS)

#$(PROGRAM).o: $(PROGRAM).f
#	$(F77) $(F77FLAGS) -c $<

# PARALLEL PROCESSING OPTION.  To use MPI, uncomment the following 2 lines
# and replace "para-non.o" with "para-mpi.o" in definition of OBJECTS above.
#para-mpi.o: parallel/para-mpi.f
#       $(F77) $(F77FLAGS) -I$(INCDIR) -c parallel/para-mpi.f

# SERIAL PROCESSING OPTION.  To use MPI, comment out the following 2 lines:
para-non.o: serial/para-non.f
	$(F77) $(F77FLAGS) -I$(INCDIR) -c serial/para-non.f

%.o: %.f
	$(F77) $(F77FLAGS) -c $<

$(MHC).mod:
	$(F90) $(F90FLAGS) -c $(MHC).f90

%.o : %.c
	$(CC) $(CFLAGS) -c $<

#  end

