Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Helping on making 2 main programs merging into 1!

Status
Not open for further replies.

peterpan2014

Programmer
Apr 29, 2014
2
US
Hi all, I am new to fortran and I just faced the challenging task.

I have two similar programs that I run them separately. The output generated by program#1 has to be used as the input for program#2. I have already figured out the Open/write/read commands so I could make a smooth transition of input/output. However now I am trying to merge both programs into one, so I can run both at once. What I did I pretty much paste program#2 at the end of program#1 (removing end). While they share several same variables, I made sure that the distinct ones should have a different name, same was for the subroutines.

The problems I am facing now is that all the matrices I have allocated a dimension, I can not reallocate new dimension for the new program. I tried to use the function "reallocate" and did not have any success. Well, If y'all have any tips to help me out with it, that would be very much appreciated.

I don't know if I was too clear, otherwise, let me know what questions you might have and I'll try to answer them to the best of my knowledge.

Paulo.

program program#1
...
***
///
end program


program program#2
---
000
...
end program

Merging:

program program12

...
***
///
---
000
...
end program
 
Hi peterpan2014,
The simple solution would be to write shell-script or bat-file which first runs the program1 and then program2. IMO it's better than to merge the programs.

...
While they share several same variables, I made sure that the distinct ones should have a different name, same was for the subroutines.

The problems I am facing now is that all the matrices I have allocated a dimension, I can not reallocate new dimension for the new program.
...

I don't understand what a new problem you could have with matrices, when you properly renamed the original variables. For example if you have matrix A in program1 and other matrix A in program2, you would have in program12 two maatrices A1 and A2 and treat them the same way as in the original programs.
 
Paulo:

Yes, you are new to Fortran, but no, it is not a challenging task ;-)

As mikrom mentions, the trivial solution to this problem is a script that would do for you what you do by hand. For example, you can simply write the names of the 2 executables in a script file; then, you just run one command (the name of the script file) and all the commands in the script file will be executed for you.

If the name of the files your Fortran programs read data from are hard coded in the programs themselves (very bad idea), then, your script file would look like this:

Code:
#!/bin/bash
program#1
program#2

This assumes, for example, that program#1 reads from "prog1.in" and writes to "prog1.out" and that program#2 reads from "prog1.out" and writes to "prog2.out".

In most cases, when only one input file and one output file are needed, it is best to leave the names of the files out of the picture and instead of opening and closing files, you use standard input and standard output. In this case, your script would look like this, for example:

Code:
#!/bin/bash
program#1 < prog1.in  > prog1.out
program#2 < prog1.out > prog2.out

Which would also work with:

Code:
#!/bin/bash
program#1 < anyname.in  > temp.out
program#2 < temp.out > results.out

Finally, when you have separate programs and you want to put together into a single Fortran source file, you do not merge them the way you illustrate; instead, you make them subroutines of a common main program:

program program#1 --> subroutine program#1
program program#2 --> subroutine program#2

Code:
program program12
    call program#1
    call program#2
end program12

subroutine program#1
.
.
.
end subroutine program#1

subroutine program#1
.
.
.
end subroutine program#1

The above Fortran would be similar to the first bash script shown in that it will work if your subroutines have the input/output file names hard coded into them, i.e., the data transfer is done the same way as before via files (instead of share variables).

From there, you may stop using files to transfer data and start using subroutine arguments.

gsal
 
oops...typo...forgot to edit copy/paste: program#1 --> program#2 in second instance, of course.

As for the scripts, read up on bash scripting if you are on Unix/Linux or batch files if you are working on Windows.
 
Mikrom and Salgerman, thanks for the suggestions. This weekend I couldn't work on it, but I'll try to do the script this week. Now I feel more confident and it makes sense. I did not know we could do that!
Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top