CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

Help in FORTRAN PROGRAMMING

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 1 Post By flotus1
  • 1 Post By juliom
  • 1 Post By FMDenaro

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 8, 2017, 07:22
Default Help in FORTRAN PROGRAMMING
  #1
New Member
 
Karan Gupta
Join Date: Dec 2016
Location: IIT BOMBAY
Posts: 13
Rep Power: 9
KaranG is on a distinguished road
Respected members

While writing a fortran subroutine I am using this statement

write(UNIT=10000+myrank) tamp_mat(1:endx,1:endy, nn_loc)

and on running the code the error comes

Segmentation fault - invalid memory reference

this nn_loc represents the time step and this subroutine is called at each and every time step.

and the same variable is mentioned elsewhere in a subroutine from which it writes the values

there it is given as

tamp_mat(i,j,nn_loc)=tmp_mat(i,j)

in this nn_loc varies from 3: endt-1

Please help me to find out the cause of error.


your sincerely
Karan Gupta
KaranG is offline   Reply With Quote

Old   March 8, 2017, 07:33
Default
  #2
Super Moderator
 
flotus1's Avatar
 
Alex
Join Date: Jun 2012
Location: Germany
Posts: 3,399
Rep Power: 46
flotus1 has a spectacular aura aboutflotus1 has a spectacular aura about
Use debugging flags to compile your code. At least -traceback -check bounds
edit: or -fbounds-check -fbacktrace for the gnu compiler
FMDenaro likes this.
flotus1 is offline   Reply With Quote

Old   March 8, 2017, 09:00
Default
  #3
Senior Member
 
piu58's Avatar
 
Uwe Pilz
Join Date: Feb 2017
Location: Leipzig, Germany
Posts: 744
Rep Power: 15
piu58 is on a distinguished road
Quote:
Originally Posted by KaranG View Post
[FONT=Arial]

write(UNIT=10000+myrank) tamp_mat(1:endx,1:endy, nn_loc)
Karan Gupta
May it be that myrank points to anywhere and you address a unit which doesn't exist?
__________________
Uwe Pilz
--
Die der Hauptbewegung überlagerte Schwankungsbewegung ist in ihren Einzelheiten so hoffnungslos kompliziert, daß ihre theoretische Berechnung aussichtslos erscheint. (Hermann Schlichting, 1950)
piu58 is offline   Reply With Quote

Old   March 8, 2017, 09:23
Default
  #4
New Member
 
Karan Gupta
Join Date: Dec 2016
Location: IIT BOMBAY
Posts: 13
Rep Power: 9
KaranG is on a distinguished road
This is the make file I used for running the open source code for which the above program is a part.


What do I do to debug the code using this makefile. I am new to FORTRAN programming so please help me.


# make clean : remove .o .mod .M -files and PROG
# make mod : compile modules
# make : build coulwave_MPI.out
# make db : compile files in $(DEBUG) with -g flag

PROG = pCOULWAVE.out

MODULES = mainvar_module.f FV_var_module.f

OBJS = coulwave_MPI.o var_grp.o \
add_bottom_friction.o add_breaking.o add_breaking_ktrans.o \
add_sponge.o allocate_matrices.o bl_define.o \
boundary_condition.o calc_corrector_error.o \
calc_Courant_number.o calc_mass.o calc_maxs_means.o \
calc_overflow.o calc_vel_DA.o calc_vel_dz.o calc_vel_z.o \
create_depth.o create_file.o create_rand_matrix.o \
create_write_xyth.o decomp2d.o dhdt_calc.o exchange2d.o \
exchange2d_int.o find_dxdydt.o find_max_hxx.o find_max_zeta.o \
find_ts_indices.o find_wavelengths.o global_dims.o \
initial_condition.o init_variables.o internal_source_type2.o \
load_eta_in.o load_inputs.o move_shoreline.o \
set_dispersion_coefs.o set_internal_source_coefs.o \
set_loop_limits.o shift_matrices_back.o solit_cnoidal_ic.o \
spectral_analysis.o store_previous_iter.o \
tridiag.o tridiagp.o user_interface.o write_disp_info.o \
write_inputs.o write_maxs_means.o write_surfaces.o \
write_timeseries.o \
FV_var_grp.o FV_allocate_matrices.o FV_interp_xei.o \
FV_interp_xoi.o FV_interp_yei.o FV_interp_yoi.o FV_ix_iy_eval.o

F90OBJS = FV_efg_calc.o FV_efg_calc_1D.o FV_estime.o \
FV_interpolations.o FV_ldu.o FV_limiters.o FV_Riemanns4.o

DEBUG_FLAGS = -C -g

# mpif90 with gfortran compiler
COMPILER=mpif90
F90FLAGS= -O3 -ffixed-line-length-0 -ffree-line-length-0
LFLAGS= -O3 -ffixed-line-length-0 -ffree-line-length-0

MOD_OBJ=$(MODULES:.f=.o)

main : $(OBJS) $(F90OBJS)
$(COMPILER) -o $(PROG) -p $(LFLAGS) $(OBJS) $(F90OBJS) $(MOD_OBJ)

%.o : %.f
$(COMPILER) -p -c $(F90FLAGS) $< -o $@
%.o : %.f90
$(COMPILER) -p -c $(F90FLAGS) $< -o $@

clean:
rm *.o *.mod *~ $(PROG)
db:
$(COMPILER) $(DEBUG_FLAGS) -o $(PROG) $(OBJS) $(F90OBJS) $(MOD_OBJ)
mod:
$(COMPILER) -c $(F90FLAGS) $(MODULES)
KaranG is offline   Reply With Quote

Old   March 8, 2017, 09:23
Default
  #5
Senior Member
 
Julio Mendez
Join Date: Apr 2009
Location: Fairburn, GA. USA
Posts: 290
Rep Power: 18
juliom is on a distinguished road
Send a message via Skype™ to juliom
Segmentation fault means that you are reading a section in the memory that does not exist.
I can suggest you different things, since you are using writing instruction, make sure the argument of the function are characters, it seems that you are providing scalars instead.
Change tamp_mat(1:endx,1:endy, nn_loc), by
tamp_mat(:,:, nn_loc). And see if you can solve the issue. My guess is that you call this instruction at the end of each time step. If you call it at the end of the computation change nn_loc by any integer to see if you can access that part of the memory.
Segmentation faults are sometimes hard to spot but the best way is to do that.
juliom is offline   Reply With Quote

Old   March 8, 2017, 09:43
Default
  #6
New Member
 
Karan Gupta
Join Date: Dec 2016
Location: IIT BOMBAY
Posts: 13
Rep Power: 9
KaranG is on a distinguished road
Thanks for the answer. Segmentation fault has gone by choosing tamp_mat(:,:, nn_loc).
KaranG is offline   Reply With Quote

Old   March 8, 2017, 10:00
Default
  #7
Senior Member
 
Julio Mendez
Join Date: Apr 2009
Location: Fairburn, GA. USA
Posts: 290
Rep Power: 18
juliom is on a distinguished road
Send a message via Skype™ to juliom
Excellent, then check your nnumerical model, because it means that you introduced an error when you performed divided the domain, for example if you have 21 nodes, it means you have 20 cells or volumes. If you have the information stored at each node then i= 1, imax, but if you have the information stored at the center of each node, then i = 1, imax-1.
The solution of changing Change tamp_mat(1:endx,1:endy, nn_loc), by tamp_mat(:,:, nn_loc) basically reads the whole chunk from the memory, but that procedure will mask any error you have in your algebraic equation.

Happy Computations!!!
FMDenaro likes this.
juliom is offline   Reply With Quote

Old   March 8, 2017, 12:04
Default
  #8
Senior Member
 
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 6,768
Rep Power: 71
FMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura about
This error is generally due to the fact that the type of the index used in the matrix is not an integer or if is an integer it violates the bounds of the matrix...
juliom likes this.
FMDenaro is offline   Reply With Quote

Old   March 8, 2017, 12:07
Default
  #9
Senior Member
 
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 6,768
Rep Power: 71
FMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura about
Quote:
Originally Posted by KaranG View Post
This is the make file I used for running the open source code for which the above program is a part.


What do I do to debug the code using this makefile. I am new to FORTRAN programming so please help me.


# make clean : remove .o .mod .M -files and PROG
# make mod : compile modules
# make : build coulwave_MPI.out
# make db : compile files in $(DEBUG) with -g flag

PROG = pCOULWAVE.out

MODULES = mainvar_module.f FV_var_module.f

OBJS = coulwave_MPI.o var_grp.o \
add_bottom_friction.o add_breaking.o add_breaking_ktrans.o \
add_sponge.o allocate_matrices.o bl_define.o \
boundary_condition.o calc_corrector_error.o \
calc_Courant_number.o calc_mass.o calc_maxs_means.o \
calc_overflow.o calc_vel_DA.o calc_vel_dz.o calc_vel_z.o \
create_depth.o create_file.o create_rand_matrix.o \
create_write_xyth.o decomp2d.o dhdt_calc.o exchange2d.o \
exchange2d_int.o find_dxdydt.o find_max_hxx.o find_max_zeta.o \
find_ts_indices.o find_wavelengths.o global_dims.o \
initial_condition.o init_variables.o internal_source_type2.o \
load_eta_in.o load_inputs.o move_shoreline.o \
set_dispersion_coefs.o set_internal_source_coefs.o \
set_loop_limits.o shift_matrices_back.o solit_cnoidal_ic.o \
spectral_analysis.o store_previous_iter.o \
tridiag.o tridiagp.o user_interface.o write_disp_info.o \
write_inputs.o write_maxs_means.o write_surfaces.o \
write_timeseries.o \
FV_var_grp.o FV_allocate_matrices.o FV_interp_xei.o \
FV_interp_xoi.o FV_interp_yei.o FV_interp_yoi.o FV_ix_iy_eval.o

F90OBJS = FV_efg_calc.o FV_efg_calc_1D.o FV_estime.o \
FV_interpolations.o FV_ldu.o FV_limiters.o FV_Riemanns4.o

DEBUG_FLAGS = -C -g

# mpif90 with gfortran compiler
COMPILER=mpif90
F90FLAGS= -O3 -ffixed-line-length-0 -ffree-line-length-0
LFLAGS= -O3 -ffixed-line-length-0 -ffree-line-length-0

MOD_OBJ=$(MODULES:.f=.o)

main : $(OBJS) $(F90OBJS)
$(COMPILER) -o $(PROG) -p $(LFLAGS) $(OBJS) $(F90OBJS) $(MOD_OBJ)

%.o : %.f
$(COMPILER) -p -c $(F90FLAGS) $< -o $@
%.o : %.f90
$(COMPILER) -p -c $(F90FLAGS) $< -o $@

clean:
rm *.o *.mod *~ $(PROG)
db:
$(COMPILER) $(DEBUG_FLAGS) -o $(PROG) $(OBJS) $(F90OBJS) $(MOD_OBJ)
mod:
$(COMPILER) -c $(F90FLAGS) $(MODULES)



Note that you have DEBUG_FLAGS = -C -g but also some otpimization options like -O3. For debugging purpose, you must not use optimization.
FMDenaro is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fortran programming style Shenren_CN Main CFD Forum 6 February 4, 2011 06:57
programming language for CFD, C, C++, FORTRAN,.... hadian Main CFD Forum 15 September 9, 2008 22:19
GUI programming with FORTRAN ztdep Main CFD Forum 1 August 30, 2007 13:22
A problem in FORTRAN programming Behafarid Main CFD Forum 6 August 16, 2006 12:16
Fortran programming and 64 bit machine Q Main CFD Forum 4 November 7, 2005 05:18


All times are GMT -4. The time now is 17:29.