I have a program in Fortran that calculates a file, say, named
wvfunc3d.dat
which I want to visualize with Gnuplot in real time during the execution of my program. After the code that creates this file, I put in my program a string
jret=SYSTEM('gnuplot wf3d.plt')
the script file wf3d.plt has the only string and looks like:
splot 'wvfunc3d.dat' w l
All of this really draws a plot I want to see, but, as is well known, it immediately disappears. I know, there is an option to avoid the closing of the window,
jret=SYSTEM('gnuplot -persist wf3d.plt')
that lets my plot not to disappear, but then the execution of the Fortran program also freezes until I close the window with the graph.
So, I want the plot to persist until I have new data, to be automatically updated after a new call of a command in Fortran, but I also need my program to run calculations! Is there a way to solve this problem? I use Windows XP.
I think you may be able to use EXECUTE_COMMAND_LINE instead of system to achieve what you want. This allows you to include a wait option which when set to .false. allows the fortran code to keep running. You can then add a sleep and reread to your gnuplot script (e.g. sleep 1 and reread) as suggested in this post.
If this doesn't work, you could consider a multi-threaded strategy ( openMP or mpi in fortran). Personally, I usually just run gnuplot at the same time and trigger an update of the plotted data by pressing the a key. I use linux so cannot test it for windows but a minimal example which works for me is,
program gnuplot
implicit none
logical :: gnuplot_open = .false.
integer :: i,t,N,redraw
real(kind(0.d0)),dimension(:),allocatable :: x,y
real(kind(0.d0)),parameter:: pi=4.d0*atan(1.d0)
N = 1000
allocate(x(N),y(N))
redraw = 100
do t = 1,300000
do i=1,N
x(i) = 6.d0*i/N*pi
y(i) = sin(x(i)+t*0.2)
enddo
if (mod(t,redraw) .eq. 0) then
open(1,FILE='./tempout',status='replace')
do i=1,N
write(1,*) x(i),y(i)
enddo
close(1,status='keep')
endif
if (.not. gnuplot_open) then
call execute_command_line('gnuplot --persist plot_tempout', wait=.false.)
gnuplot_open = .true.
endif
enddo
end program gnuplot
and plot_tempout is,
plot 'tempout' u 1:2 w l
pause 0.1
reread
Ed, thank you very much for your thorough reply. I will try to work on it.
Before encountering this problem I was able to easily draw the plots of small enough files using a cycle directly in gnuplot. Something like this:
do for [i=1:100500] {plot 'littldat.dat' w l; pause 3}
that did it well. But when I tried to do this with large file, it was very often caught read by gnuplot in the moment it was not completed yet: I had either a full plot or a plot of a part of my data, and it was not good. Because of this I began to seek for the way to do it by the means of programming language.
Before you answered I finally found a very simply, though not very elegant solution: you write data to a temporary file and then, once it is completed, give it the final name to be read by gnuplot. So, gnuplot reads either old data, or new ones, but never an incomplete file. It results to be something like this in Fortran:
open(1,file='donnees_temp.dat')
write(1,*)x,y,z
close(1)
call rename ('donnees_temp.dat','donnees.dat')
and, in Gnuplot I used a cycle like the one above:
do for [i=1:100500] {splot 'donnees.dat' w l; pause 5}
so it works, and the program executes.
Related
I have written a specific software, and this running for generate 6 files with the random numbers in different´s ranges.
But, I want to hide the cursor when this is running.
• Is there any possible way to do this?
• Update •
:: Answer from #Daniel R Livingston
• Solve the case with something minors adaptation on 1st post: ::
"By this First, printing the escape code \e[?25l will hide your cursor.
\e[?25h "will re-enable it."
write (*,*)char(27)//char(91)//char(63)//char(50)//char(53)//char(108)
! For some reason, by using the 'result = systemqq'
! May force the screen to "apply" the line above, only
! After this, that escape gone works in Windows!
result = systemqq('#cls &#%windir%'//char(92)//'system32'//char(92)//'chcp.exe 850 2>nul >nul 1<&2&#color 0a')
write (*,*)char(27)//'[37;41;4m'//char(255)//char(250)//char(255)//'Generating',x,' Random Numbers!',char(27)
&//'[0m'//char(10)//char(10)
! Obs.: --------------------------------------------------------------------
! Turn the cursor back by adding:
! write (*,*),char(27)//'[?25h' before end program (latest line)
! \e[?25l == char(27)//char(91)//char(63)//char(50)//char(53)//char(108)
Two ways to do this.
First, printing the escape code \e[?25l will hide your cursor. \e[?25h will re-enable it.
Second, by calling the Windows API:
BOOL WINAPI SetConsoleCursorInfo(
_In_ HANDLE hConsoleOutput,
_In_ const CONSOLE_CURSOR_INFO *lpConsoleCursorInfo
);
I have a rather dull Fortran program that are used by students for some heavy calculations and I thought the program might be a little more interesting if I could add some sounds here and there. Is there any utility to generate sounds that is callable from a Fortran program? I would like to call a routine with for example a selection of MP3 files.
I do not like programs which beeps and plings in general but this is a kind of last resort to keep the students alert ...
I'd go with C library (e.g. How to play MP3 files in C?).
I'd create shared lib with your routine (e.g. code playing some sort of mp3 list) and called it from Fortran code.
Question is, whether this is what you are looking for.
It may be interesting to try the system call together with a command-line tool, e.g.
program main
implicit none
integer i
character(100) :: message(3)
message(1) = "hi"
message(2) = "yo"
message(3) = "done!"
do i = 1, 3
call system( "say " // trim( message(i) ) )
enddo
end
which says any message via speech synthesis on Mac OSX. A similar thing may be achieved for MP3 files with some audio commands (on Windows, Mac, and Linux). This demo seems to be using such an approach (Note: music starts from the page!).
I am writing a subroutine and main function to call it, but getting error as undefined reference to ___. I found one reason: When I save the main and subroutine in the same file, compile and run that file, everything runs perfectly. However, when I save them into different .f90 files and try to run the main file, I get error. Is there any way I can make subroutine into a separate file and call into main calling program?
I got confused with another place - in the main program at !------ERROR------ place. I referred to Automatic width integer descriptor in fortran 90 I can use I0 as automatic width display indicator. But when I used the same, there is run time error expected integer but got character. Any idea about this?
! saved as sub_program.f90 file
SUBROUTINE sub_program (v1,v2,ctr)
IMPLICIT NONE
INTEGER, INTENT(IN) :: ctr
INTEGER, INTENT (OUT) :: v1,v2
SELECT CASE (ctr)
CASE (1)
v1=1
v2=0
CASE (2)
v1=0
v2=1
END SELECT
RETURN
END SUBROUTINE
! main calling program, saved as caller.f90
PROGRAM caller
IMPLICIT NONE
INTEGER :: v1,v2,ctr
DO ctr = 1,2,1
CALL sub_program (v1,v2,ctr)
WRITE (*,100) 'STEP = ',ctr,'V1 = ',v1,'V2 = ',v2 !------ERROR------
100 FORMAT (I0)
END DO
END PROGRAM
Thanks!
What is your compile command? For me, this compiles and runs normally
gfortran caller.f90 foo.f90 && ./a.out
I0 is an integer indicator, but some items following your WRITE statement are character strings. You can try, for example,
100 FORMAT (3(A, I0, 1X))
where 1X refers to a space.
As a note, if formatting is not terribly important and you're only interested in seeing some quick results, you can use the free format output (WRITE(*,*) ...).
EDIT: I had incorrectly referred to FORMAT as obsolete.
I'm sorry if this has been asked before, but I couldn't find an answer that seemed to work for me. I'm working with an old program, but have made a few modifications to it.
I can include the whole 2500 line program, but it seems like that is a lot.
I've successfully compiled the a program, but it fails when I try and run it. I'm getting a "Fortran runtime error: End of file" at the line which reads the .dat file. I've tried to compile a test segment, using the same .dat file and same variables. It results in the same problem.
PROGRAM OPEN
INTEGER (KIND=1), PARAMETER :: dy=3 ! number of income states
INTEGER (KIND=2) :: OpenStatus
REAL, DIMENSION(dy) :: grid,wt
OPEN(1,file='cygdrive/user/mk.dat',status='old',form='formatted',IOSTAT=OpenStatus)
READ (1,*) grid, wt
IF(OpenStatus>0) STOP 'cannot open mk.dat'
CLOSE(1)
PRINT*, grid(1)
END PROGRAM
The data file referenced is:
-1.7320508e+000
0.0000000e+000
1.7320508e+000
4.1777138e-001
1.6710855e+000
4.1777138e-001
Where each of these numbers is on its own line and preceeded by a space
This generates the same end of file runtime error. I'd really appreciate any help here.
I should add that I compiled with gfortran.
EDIT:
As per High Performance Mark's suggestion below, I've modified it to include an inquire test.
PROGRAM TEST
CHARACTER :: fnm, seq, fort
Logical :: lex
INTEGER (KIND=1), PARAMETER :: dy=3 ! number of income states
INTEGER (KIND=2) :: j,j0,j1,j2,j4,j5,j6,j7,k,jjj,jj,dyy,OpenStatus
REAL, DIMENSION(dy) :: grid,wt
OPEN(1,file='cygdrive/user/mk.dat',status='old',form='formatted',IOSTAT=OpenStatus)
INQUIRE (1, EXIST=lex, NAME=fnm, SEQUENTIAL=seq, FORMATTED=fort)
PRINT*, 'Exists=',lex, ' Name=',fnm, ' Sequential=', seq, 'Formatted=', fort
READ (1,*) grid, wt
IF(OpenStatus>0) STOP 'cannot open mk.dat'
CLOSE(1)
PRINT*, grid(1)
END PROGRAM
The results of the inquire statement are:
Exists= T Name= Sequential=U Formatted=U
My understanding is that the File is found (i.e. exists is returned as true), is un-named and the format and sequential access are returned as unknown (as is direct which I included later). I've also checked the delimiter and padding which are coming back as unknown.
My beginner intuition is telling me that I should try and create a new data file by writing to it with a fortran program and that should solve the problem? Is that correct? If so is there a fundamental misunderstanding at play here i.e. is this a problem with data files from other sources?
Thanks for all your patience.
(Answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
I've fixed this problem-the program was looking for it in a different place, I've now corrected that.
I wrote a subroutine for a Fortran program and I want to execute a command (delete file in the program directory and open a new one) the first, and only first time, the subroutine is called. I know that I can achieve this by creating some flag outside of the subroutine, in the main program, that I set to false at program startup and then set to true upon entering the subroutine. Then I could use this flag in an if statement to figure if the commands I want to execute on the initial call should be executed or not. But this requires me modifying the existing program and I didn't want to do that if I could avoid it. Is there some other way to do what I want to do?
An example might be:
subroutine test(a)
implicit none
integer, intent(inout) :: a
logical, save :: first_time=.true.
if(first_time) then
first_time=.false.
a = a + 12345
else
a = a - 67890
end if
end subroutine test
How about using some characteristic of the output file to determine whether or not to delete it? Time stamp, file lock, a particular file extension, etc.