LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2013, 03:01 AM   #1
lehloks
LQ Newbie
 
Registered: May 2013
Posts: 12

Rep: Reputation: Disabled
Converting Matlab to Fortran


Hi users
I would like your help with converting this Matlab code to Fortran code.Im not familiar with Matlab.

thanks in advance.

[]
% make sure that the array with data that you have is a long one, correlation functions are need a lot of trajectories to become smooth.
file = fopen(DATA....);
Pxy = fscanf(file,'%f',[1 inf]);

% make an array with the final correlation function, choose NTimeSteps to be much smaller than the length of Pxy
NTimeSteps = round(tmax/dt);
ACF = zeros(1,NTimeSteps);

% start various evaluations of the correlation function from different (uncorrelated) times.
Shift = round(0.5/dt); % where '0.5' is approximately the time is takes for starting points to become uncorrelated, depending on your system
Ntraj = round((length(Pxy) - NTimeSteps)/Shift);

for j = 1 : Ntraj
for i = 1 : NTimeSteps
ACF(i) = ACF(i) + Pxy(1 + (j-1)*shift)*Pxy(i + (j-1)*shift); % (j-1) because Matlab won't allow starting a loop with j=0. But you might use a different language.
end
end

% ensemble averaging
ACF = ACF / Ntraj;

% integral over the correlation function
intACf = cumsum(ACF) / (V*k_B*T) * dt;

cumsum is not the most elegant integration method, you can also use simpson rule or something like that. I believe that would be 'quad' in Matlab.


[/]
 
Old 08-21-2013, 03:10 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Homework?
 
Old 08-21-2013, 07:50 AM   #3
lehloks
LQ Newbie
 
Registered: May 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
Hi astrogeek
its not homework! any help will be appreciated.
 
Old 08-21-2013, 02:55 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by lehloks View Post
Hi astrogeek
its not homework! any help will be appreciated.
Ok, I guess the place to start would be to ask what you have done so far.

I do not know matlab, but the expressions look understandable enough (although they do not look to be complete). Maybe it would help to know the source and target contexts. Where is this used?
 
Old 08-21-2013, 03:00 PM   #5
Janus_Hyperion
Member
 
Registered: Mar 2011
Location: /
Distribution: Fedora (typically latest release or development release)
Posts: 372

Rep: Reputation: Disabled
I remember someone I know doing this conversion for some reason. Take a look at these two links

1 - http://sourceforge.net/projects/matlab2fmex/
2 - http://emmanuel.branlard.free.fr/con...ode-to-fortran

The second link is one where there is a matlab script that converts a MATLAB code to FORTRAN.

Quote:
Originally Posted by From Info page
DOCUMENTATION : matlab2fortran
---------------------------------------------------
Brief description:
matlab2fortran(matlab to fortran) : converting matlab code to fortran
Usage: matlab2fortran(filename);
Of course, it is necessary that you understand the MATLAB code completely in order to be able to use the Fortran converted one.
 
Old 08-22-2013, 01:56 AM   #6
lehloks
LQ Newbie
 
Registered: May 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
This is what I have done so far.What I dont understand is matlab functions and syntax


[]
program integrate_dataset
implicit none
real:: pxy,NTtimesteps,ACF,tmax,dt,Ntraj,intACF
integer :: i,j

!% make sure that the array with data that you have is a long one,
!correlation functions are need a lot of trajectories to become smooth.

open(7,file='DATA_input.txt')
!open (10,file = fname,form='formatted')


Pxy = fscanf(file,'%f',[1 inf])

!% make an array with the final correlation function,
!choose NTimeSteps to be much smaller than the length of Pxy

NTimeSteps = round(tmax/dt)
ACF = zeros(1,NTimeSteps)

!% start various evaluations of the correlation function from different (uncorrelated) times.

!Shift = round(0.5/dt)

!% where '0.5' is approximately the time is takes for starting points
!to become uncorrelated, depending on your system

Shift = round(0.5/dt)
Ntraj = round((length(Pxy) - NTimeSteps)/Shift)

do j=1,Ntraj
do i=1,NTimesteps
!for j = 1 : Ntraj

!for i = 1 : NTimeSteps

!% (j-1) because Matlab won't allow starting a loop with j=0. But you might use a different language.
ACF(i) = ACF(i) + Pxy(1 + (j-1)*shift)*Pxy(i + (j-1)*shift)

enddo
enddo

!% ensemble averaging
ACF = ACF / Ntraj

!% integral over the correlation function

intACf = trap(ACF) / (V*k_B*T) * dt
end


[/code]
 
Old 01-25-2017, 05:52 PM   #7
sogand_jam2000
LQ Newbie
 
Registered: Jan 2017
Posts: 1

Rep: Reputation: Disabled
Equal of Find () command in fortran

I have a matlab code that needs to use this command

Find(A>3,5)

the equivalent if this command in fortran is which command?


Thank you alls
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Fortran alternative to Matlab 'system' command mushi_aph Programming 4 06-08-2012 03:07 PM
converting integer data to string in fortran 90 s_hy Programming 1 03-13-2012 03:53 PM
[SOLVED] Installed MATLAB in /home/c/Desktop/MATLAB But Cannot ACCESS it hitmen Linux - Newbie 10 09-25-2011 12:11 PM
Matlab Distributed Computing Server: Connecting to Matlab client fails OEP Linux - Server 0 12-21-2009 02:18 PM
converting C to Fortran Bukola2001 Programming 0 07-01-2004 10:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration