Fortran code (.f95) compiles fine in Windows g95 compiler but incorrectly in Ubuntu gfortran - windows

I am trying to compile an .f95 fortran script so it can run on Ubuntu. The script is available here -> Link to zip file containing .f95 script
It compiles and runs fine when I switch over to Windows and compile using g95 compiler. The .exe file produced also runs fine in Ubuntu through wine.
However if I try to compile to make an Ubuntu file, it does not work properly. I don't get a compile error, but if I run the resultant file, either the program gets stuck in an infinite loop, or the output is all wrong. It's difficult for me to see where it is going wrong because I did not write the original code and only have a shaky understanding of Fortran, but it seems to be something to do with the numbers being calculated wrong leading to very large/small/inappropriately negative output (sorry to be so vague).
I am running 16.04 xenial ubuntu and gfortran 5.4.0.
Any help/thoughts appreciated this is driving me up the wall! Thanks
Code below for quick reference:
! Seed dispersal model of of Duman et al. (2015)
! Instructions and expample are found in:
! https://nicholas.duke.edu/people/faculty/katul/research.html
! in 'Library of Functions and Utilities'
! Author: Tomer Duman
! Version: Version 2
! Date: October 22, 2015
! References: Duman, T., Trakhtenbrot, A., Poggi, D.,
! Cassiani, M., Katul, G., Dissipation
! Intermittency Increases Long-Distance
! Dispersal of Heavy Particles in the Canopy
! Sublayer,
! accepcted to Boundary-Layer Meteorology
program LSmodel
implicit none
real :: sec,ran,gasdev ! random generator variables
real :: x,y,z,u,v,w,ut,vt,wt,t,dt ! simulation variables
real :: wg ! seed parametes
real :: Um,sigma_u,sigma_v,sigma_w,uw ! wind statistics variables
real :: dvaru_dz,dvarv_dz,dvarw_dz,duw_dz ! wind statistics variables
real :: dissip_m,TL ! vector over the range of ustars
real :: zs,zg,zmax ! release height & boundaries
real :: Ainv,C0inv ! inverse parameters
real :: C0,A,b,au,av,aw,dt_on_TL ! LS model parameters
real :: dz_max,dt_max ! time step limit
real :: CT,beta ! Crossing Trajectories correction
real :: C_chi,chi,TKE,T_chi,omega ! DI parameters
real :: a_ln,b_ln,sigma_chi,dissip_s ! DI parameters
real :: rhop,rho,r,g,gt,Re,AIP,Cd,nu ! IP parameters
real :: up,vp,wp,upt,vpt,wpt,vr,dt_ip,alpha ! IP parameters
integer :: seed ! random generator variables
integer :: pnum ! simulation parameters
integer :: i,j,jj,n,ii ! counting parameters
integer :: n_ip,IP=1 ! IP parameters
character(len=80) :: filename
real, allocatable,dimension(:) :: z_vec,Um_vec,sigma_u_vec,sigma_v_vec,sigma_w_vec,uw_vec
real, allocatable,dimension(:) :: dvaru_dz_vec,dvarv_dz_vec,dvarw_dz_vec,duw_dz_vec,dissip_m_vec
! setting the random generator seed
seed=7654321
sec=0.0
seed=seed+2*int(secnds(sec))
! input
open (21,file='input_parameters.txt')
read (21, *), x,C0,wg,zs,zg,beta,dt_on_TL,y,sigma_chi,C_chi,r,rhop,alpha,rho,nu
close(21)
pnum = int(x) ! number of released seeds
n = int(y) ! size of the input flow stats
wg = -1.0*wg ! seed terminal velocity [m/s]
!zs ! seed release height [m]
!C0 ! universal constant
!beta ! crossing trajectories parameter
!zg ! ground height [m]
!sigma_chi ! the standard deviation of chi (dissipation intermittency)
!C_chi ! constant for T_chi calc
!r ! particle radium [m] - set to 0 for no IP
!rhop ! particle dry density [kg/m^3]
!alpha ! drag parameter (Cd = 24/Re_p*(1+alpha*Re_p))
!rho ! fluid density [kg/m^3]
!nu ! fluid viscosity [m^2/s]
C0inv = 1.0/C0
g = 9.81
if (r==0.0) then
IP = 0
end if
! limiting parameters to prevent too big jumps in a time-step
dz_max = 0.1
dt_max = 0.1
open(unit=12,file='res.dat', form='formatted')
open(unit=13,file='res_traj.dat', form='formatted')
! allocate
allocate(z_vec(n))
allocate(Um_vec(n))
allocate(sigma_u_vec(n))
allocate(sigma_v_vec(n))
allocate(sigma_w_vec(n))
allocate(uw_vec(n))
allocate(dvaru_dz_vec(n))
allocate(dvarv_dz_vec(n))
allocate(dvarw_dz_vec(n))
allocate(duw_dz_vec(n))
allocate(dissip_m_vec(n))
! load normalized stats
open (22,file='input_flow.txt',form='formatted')
read (22,*) z_vec
read (22,*) Um_vec
read (22,*) sigma_u_vec
read (22,*) sigma_v_vec
read (22,*) sigma_w_vec
read (22,*) uw_vec
read (22,*) dvaru_dz_vec
read (22,*) dvarv_dz_vec
read (22,*) dvarw_dz_vec
read (22,*) duw_dz_vec
read (22,*) dissip_m_vec
close(22)
zmax = z_vec(n)
do i=1,pnum
t=0.0 ! initiate time and location
x=0.0
y=0.0
z=zs
do j=2,n ! interpolate
if ((z>=z_vec(j-1)).and.(z<=z_vec(j))) then
sigma_u=((sigma_u_vec(j-1)-sigma_u_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+sigma_u_vec(j-1)
sigma_v=((sigma_v_vec(j-1)-sigma_v_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+sigma_v_vec(j-1)
sigma_w=((sigma_w_vec(j-1)-sigma_w_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+sigma_w_vec(j-1)
end if
end do
! velocity initiation
u=gasdev(seed)*sigma_u
v=gasdev(seed)*sigma_v
w=gasdev(seed)*sigma_w
chi=sigma_chi*gasdev(seed)-0.5*sigma_chi*sigma_chi
up=0.0 ! initiating particle velocity from rest
vp=0.0
wp=0.0
do ! time loop
do j=2,n ! interpolate
if ((z>=z_vec(j-1)).and.(z<=z_vec(j))) then
Um=((Um_vec(j-1)-Um_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+Um_vec(j-1)
uw=((uw_vec(j-1)-uw_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+uw_vec(j-1)
duw_dz=((duw_dz_vec(j-1)-duw_dz_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+duw_dz_vec(j-1)
sigma_u=((sigma_u_vec(j-1)-sigma_u_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+sigma_u_vec(j-1)
sigma_v=((sigma_v_vec(j-1)-sigma_v_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+sigma_v_vec(j-1)
sigma_w=((sigma_w_vec(j-1)-sigma_w_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+sigma_w_vec(j-1)
dvaru_dz=((dvaru_dz_vec(j-1)-dvaru_dz_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+dvaru_dz_vec(j-1)
dvarv_dz=((dvarv_dz_vec(j-1)-dvarv_dz_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+dvarv_dz_vec(j-1)
dvarw_dz=((dvarw_dz_vec(j-1)-dvarw_dz_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+dvarw_dz_vec(j-1)
dissip_m=((dissip_m_vec(j-1)-dissip_m_vec(j))/(z_vec(j-1)-z_vec(j)))*(z-z_vec(j-1))+dissip_m_vec(j-1)
end if
end do
CT = sqrt(1.0+beta*beta*wg*wg/sigma_w/sigma_w) ! crossing trajectories correction
TL = 2.0*sigma_w*sigma_w*C0inv/dissip_m/CT ! added CT effect
TKE = 0.5*(sigma_u*sigma_u+sigma_v*sigma_v+sigma_w*sigma_w)
! -------- Adding dissipation intermittency model --------
omega=dissip_m*CT/TKE ! added CT effect
T_chi=1.0/omega/C_chi
dt=min(dt_on_TL*TL,dt_on_TL*T_chi,dt_max)
a_ln = -(chi + 0.5*sigma_chi*sigma_chi)/T_chi
b_ln = sigma_chi*sqrt(2.0/T_chi)
chi = chi+a_ln*dt+b_ln*sqrt(dt)*gasdev(seed)
dissip_s = dissip_m*exp(chi)
! --------------------------------------------------------
A = 2.0*((sigma_u*sigma_u)*(sigma_w*sigma_w)- uw*uw)
Ainv = 1.0/A
b = sqrt(C0*dissip_s*CT) ! added CT effect
au = (b*b)*(uw*w - u*sigma_w*sigma_w)*Ainv + 0.5*duw_dz &
+ Ainv*(sigma_w*sigma_w*dvaru_dz*u*w - uw*dvaru_dz*w*w &
-uw*duw_dz*u*w + sigma_u*sigma_u*duw_dz*w*w)
av = (-(b*b)*v + dvarv_dz*v*w)/2.0/sigma_v/sigma_v
aw = (b*b)*(uw*u - w*sigma_u*sigma_u)*Ainv + 0.5*dvarw_dz &
+ Ainv*(sigma_w*sigma_w*duw_dz*u*w - uw*duw_dz*w*w &
-uw*dvarw_dz*u*w + sigma_u*sigma_u*dvarw_dz*w*w)
ut = u + au*dt + b*sqrt(dt)*gasdev(seed)
vt = v + av*dt + b*sqrt(dt)*gasdev(seed)
wt = w + aw*dt + b*sqrt(dt)*gasdev(seed)
u = ut
v = vt
w = wt
! -------- Adding IP model --------
if (IP==1) then
dt_ip = dt*0.01
n_ip = 100
upt = up
vpt = vp
wpt = wp
100 do ii=1,n_ip
vr = sqrt((u+Um-upt)*(u+Um-upt)+(v-vpt)*(v-vpt)+(w-wpt)*(w-wpt))
if (vr>1000.0) then
dt_ip = dt_ip*0.5
n_ip = n_ip*2
upt = up
vpt = vp
wpt = wp
goto 100
end if
Re = 2.0*r*vr/nu
Cd = 24.0*(1.0+alpha*Re)/Re
AIP = 3.0*rho*Cd/8.0/rhop/r
gt = g*(rhop - rho)/rhop
upt = upt + AIP*vr*(u+Um-upt)*dt_ip
vpt = vpt + AIP*vr*(v-vpt)*dt_ip
wpt = wpt + (AIP*vr*(w-wpt)-gt)*dt_ip
end do
up = upt
vp = vpt
wp = wpt
end if
! ----------------------------------
if (IP==0) then
up = Um+u
vp = v
wp = w+wg
end if
x = x + up*dt
y = y + vp*dt
z = z + wp*dt
if (i<50) then ! saving trajectories of 50 seeds
write(13,*) i,t,x,y,z
end if
if (z>zmax) then
exit
end if
if (z<zg) then
dt = (z-zg)/(w+wg)
z = z - (w+wg)*dt ! ensure that z = zg at landing
x = x - (u+Um)*dt
y = y - v*dt
write(12,*) i,x,y
exit
end if
dt_max = dz_max/abs(w+wg)
t = t+dt
end do
if (mod(i,100)==0) then
print *, 'wg = ',abs(wg),' zr = ',zs,' pp ',pnum-i
end if
end do
end program LSmodel
!***********************************************************************
! This function generates Gaussian Random Deviates from uniform deviates.
! The function is from Press et al. (1992 p. 280).
function gasdev(idum)
implicit none
integer :: idum, iset
real :: gasdev,fac, gset, rsq, v1, v2, ran
save :: iset, gset
data iset/0/
if (iset.eq.0) then
1 v1=2.*ran(idum)-1.
v2=2.*ran(idum)-1.
rsq=v1**2+v2**2
if (rsq.ge.1. .or. rsq .eq. 0) go to 1
fac =sqrt(-2.*log(rsq)/rsq)
gset=v1*fac
gasdev=v2*fac
iset=1
else
gasdev=gset
iset=0
end if
return
end function gasdev
!***********************************************************************
!uniform random generator between 0 and 1
function ran(idum)
implicit none
integer, parameter :: K4B=selected_int_kind(9)
integer(K4B), intent(inout) :: idum
real :: ran
integer(K4B), parameter :: IA=16807,IM=2147483647,IQ=127773,IR=2836
real, save :: am
integer(K4B), save :: ix=-1,iy=-1,k
if (idum <= 0 .or. iy < 0) then
am=nearest(1.0,-1.0)/IM
iy=ior(ieor(888889999,abs(idum)),1)
ix=ieor(777755555,abs(idum))
idum=abs(idum)+1
end if
ix=ieor(ix,ishft(ix,13))
ix=ieor(ix,ishft(ix,-17))
ix=ieor(ix,ishft(ix,5))
k=iy/IQ
iy=IA*(iy-k*IQ)-IR*k
if (iy < 0) iy=iy+IM
ran=am*ior(iand(IM,ieor(ix,iy)),1)
end function ran
The command I am using to compile in Ubuntu is
gfortran LSmodel.f95 -o LSmodel.o
There is no compile error, it compiles fine, but then on running the program afterwards the problems start.
I have included a typical expected output from running the program below (res.dat):
1 21.8583908 8.47351170
2 1.44100714 -8.78142548
3 1154.74109 -265.975677
4 8.41901588 2.71606803
5 84.5189209 -20.4699802
6 86.3176270 -18.4299908
7 133.826874 43.4905090
8 4.37516022 -2.50738835
9 1.31284332 -2.65105081
10 1.96412086 2.85013437
11 4.34823132 -3.83539009
12 40.1227837 -6.60268879
13 3.88699961 2.63749719
14 7.08872795 1.51467562
15 4.72280264 2.63384581
16 0.667112768 1.37209761
17 2.09094667 1.23296225
18 4.72298622 -1.43766475
19 1.04012501 -3.13314247
20 1.91324210 0.957163811
21 1.99065340 0.611227572
22 -2.09086251 -1.41756165
23 -1.46836996 -5.55722380
24 2.41403580 2.18929257E-02
25 3.96990728 -4.91323137
26 1.54687607 -0.527718127
27 8.24332428 -1.48289037
28 4.81600523 -8.87443924
29 2.39538932 0.323360980
30 192.294815 -36.7134209
31 24.6190643 21.7993126
32 -0.124062911 3.44432545
33 16.6237335 -8.54020786
34 50.0964355 -3.29175758
35 5.23409462 2.14592004
36 6.62141275 1.47515869
37 10.7572327 0.307090789
38 63.5973434 -47.7124138
39 74.9621201 2.11509633
40 4.46293068 -1.64074826
41 11.7773390 10.0654907
42 8.26941204 6.84578228
43 0.917451978 2.69560647
44 -2.21521306 15.0752039
45 8.18219483E-02 -2.06250334
46 0.279425710 -3.10328817
47 4.37736464 -1.37771702
48 -2.85058951 -1.79835165
49 5.08391476 2.68537569
50 -4.27199030 -0.642364025

Compiling your program with gfortran -Wall gives
test.f90:297:4:
function ran(idum)
1
Warning: 'ran' declared at (1) is also the name of an intrinsic.
It can only be called via an explicit interface or if declared
EXTERNAL. [-Wintrinsic-shadow]
which means that the user-defined routine ran() has the same name as intrinsic ran() in gfortran. So we need to declare it as an external routine (to tell the compiler that this is a user-defined one):
function gasdev(idum)
implicit none
integer :: idum, iset
real :: gasdev,fac, gset, rsq, v1, v2, ran
save :: iset, gset
data iset/0/
external ran !<--- here
...
It is necessary to include external ran in all routines that utilize the user-defined ran. (In this program, only the gasdev() routine is using it.) To avoid such interference, it is usually better to use a bit more specific name other than ran, rand, etc (for example, rand_uniform() or ranranran() might be good). It would also be very nice if the routine is included in a module to avoid such problems, so please search the net for how to use modules for more details (if necessary...)

Related

how to solve a simple mixing operation in gekko?

I am trying to solve a simple mixing operation in gekko. The mixer mx takes two inlet streams Feed1 and Feed2. The expected result is that mass flow of outlet stream mx.outlet should be the summation of mass flows of the inlet streams.
Here is what I have tried.
from gekko import GEKKO, chemical
m = GEKKO(remote=False)
f = chemical.Flowsheet(m)
P = chemical.Properties(m)
c1 = P.compound('Butane')
c2 = P.compound('Propane')
feed1 = f.stream()
m_feed1 = f.massflows(sn= feed1.name)
m_feed1.mdot = 200
m_feed1.mdoti = [50,150]
feed2= f.stream()
m_feed2 = f.massflows(sn= feed2.name)
m_feed2.mdot = 200
m_feed2.mdoti = [50,150]
mx = f.mixer(ni=2)
mx.inlet = [feed1.name,feed2.name]
m.options.SOLVER = 1
mf = f.massflows(sn = mx.outlet)
m.solve()
The code runs successfully. However, on mf.mdot seems to output incorrect value [-1.8220132454e-06]. The expected value is 400. Any help , what is wrong with my code?
Here is source code that works for this mixing application:
from gekko import GEKKO, chemical
import json
m = GEKKO(remote=False)
f = chemical.Flowsheet(m)
P = chemical.Properties(m)
# define compounds
c1 = P.compound('Butane')
c2 = P.compound('Propane')
# create feed streams
feed1 = f.stream(fixed=False)
feed2 = f.stream(fixed=False)
# create massflows objects
m_feed1 = f.massflows(sn=feed1.name)
m_feed2 = f.massflows(sn=feed2.name)
# create mixer
mx = f.mixer(ni=2)
# connect feed streams to massflows objects
f.connect(feed1,mx.inlet[0])
f.connect(feed2,mx.inlet[1])
m.options.SOLVER = 1
mf = f.massflows(sn = mx.outlet)
# specify mass inlet flows
mi = [50,150]
for i in range(2):
m.fix(m_feed1.mdoti[i],val=mi[i])
m.fix(m_feed2.mdoti[i],val=mi[i])
# fix pressure and temperature
m.fix(feed1.P,val=101325)
m.fix(feed2.P,val=101325)
m.fix(feed1.T,val=300)
m.fix(feed2.T,val=305)
m.solve(disp=True)
# print results
print(f'The total massflow out is {mf.mdot.value}')
print('')
# get additional solution information
with open(m.path+'//results.json') as f:
r = json.load(f)
for name, val in r.items():
print(f'{name}={val[0]}')
Below is the solver output. This will only work with APM 0.9.1 and Gekko v0.2.3 (release coming Aug 2019). The thermo and flowsheet object libraries were released with v0.2.2 and there are several features that are still under development. The next release should resolve many of them.
----------------------------------------------------------------
APMonitor, Version 0.9.1
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 6
Constants : 0
Variables : 19
Intermediates: 0
Connections : 44
Equations : 2
Residuals : 2
Number of state variables: 14
Number of total equations: - 14
Number of slack variables: - 0
---------------------------------------
Degrees of freedom : 0
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter Objective Convergence
0 3.86642E-16 1.99000E+02
1 4.39087E-18 1.11937E+01
2 8.33448E-19 6.05819E-01
3 1.84640E-19 1.62783E-01
4 2.91981E-20 7.21250E-02
5 1.55439E-21 2.28110E-02
6 5.51232E-24 1.21437E-03
7 7.03139E-29 4.30650E-06
8 7.03139E-29 4.30650E-06
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 0.0469 sec
Objective : 0.
Successful solution
---------------------------------------------------
v1 not found in results file
The total massflow out is [400.0]
time=0.0
feed1.h=44154989.486
feed1.x[2]=0.79815448476
feed1.vdot=104.9180373
feed1.dens=0.040621756423
feed1.c[1]=0.0081993193551
feed1.c[2]=0.032422437068
feed1.mdot=200.0
feed1.y[1]=0.25
feed1.y[2]=0.75
feed1.sfrc=0.0
feed1.lfrc=0.0
feed1.vfrc=1.0
feed2.h=44552246.421
feed2.x[2]=0.79815448476
feed2.vdot=106.66667125
feed2.dens=0.03995582599
feed2.c[1]=0.0080649042837
feed2.c[2]=0.031890921707
feed2.mdot=200.0
feed2.y[1]=0.25
feed2.y[2]=0.75
feed2.sfrc=0.0
feed2.lfrc=0.0
feed2.vfrc=1.0
mixer5.outlet.t=381.10062836
mixer5.outlet.h=44353617.96
mixer5.outlet.ndot=8.5239099109
mixer5.outlet.x[1]=0.20184551524
mixer5.outlet.x[2]=0.79815448476
mixer5.outlet.vdot=1.5797241143
mixer5.outlet.dens=5.5635215396
mixer5.outlet.c[1]=1.0891224437
mixer5.outlet.c[2]=4.3066994177
mixer5.outlet.mdot=400.0
mixer5.outlet.y[1]=0.25
mixer5.outlet.y[2]=0.75
mixer5.outlet.sfrc=0.0
mixer5.outlet.lfrc=1.0
mixer5.outlet.vfrc=0.0
v2=300.0
v3=4.2619549555
v4=0.20184551524
v5=0.79815448476
v6=101325.0
v7=305.0
v8=4.2619549555
v9=0.20184551524
v10=0.79815448476
v11=200.0
v12=50.0
v13=150.0
v14=200.0
v15=50.0
v16=150.0
v17=400.0
v18=100.0
v19=300.0

Visual Studio 2013 SerialPort not receiving all data

I am currently working on a project that requires serial communication between PIC 24FV16KA302 and a PC software.
I have searched the Internet for the past 3 days and i cant find an answer for my problem so i decided to ask here . This is my first visual studio program so i dont have any experience with the software.
The PIC has few variables and two 8x16 tables that i need to view and modify on the PC side . The problem comes when i send the tables , all other information is received without a problem . I am using serial connection ( 38400/8-N-1 ) via uart to usb converter
FT232
When the PC send "AT+RTPM" to the PIC .
Button7.Click
SerialPort1.ReceivedBytesThreshold = 128
MachineState = MS.Receive_table
SerialPort1.Write("AT+RTPM")
End Sub
The PIC sends back 128 Bytes( the values in the table )
case read_table_pwm : // send pwm table
for (yy = 0 ; yy < 8 ; yy ++) {
for (xx = 0 ; xx < 16 ; xx++ ) {
uart_send_char(controll_by_pmw_map_lb[yy][xx]) ;
}
}
at_command = receive_state_idle ;
break ;
Which the software is suppose to get and display in a DataGrid.
Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
If MachineState = MS.Receive_table Then
SerialPort1.Read(Buffer_array_received_data, 0, 128)
cellpos = 0
For grid_y As Int16 = 0 To 7 Step 1
For grid_x As Int16 = 0 To 15 Step 1
DataGridView1.Rows(grid_y).Cells(grid_x).Value = Buffer_array_received_data(cellpos)
cellpos += 1
Next
Next
End Sub
The problem is that most of the time ( 99 % ) it displays only part of the dataset and zeros to the end , and when i try to do it again it display the other part and it starts from the beginning .
First request
Second request
If i try the same thing with another program i always get the full dataset
Realterm
Termite
I have tried doing it cell by cell , but it only works if i request them one very second , other wise i get the same problem .
After that i need to use a timer ( 100 ms ) to request live data from the PIC .
this work better but still some of the time i get some random data. I haven't focused on that for the moment because without the dataset everything else is useless .
Am i missing something or has anyone encountered the same problem ?
I managed to solve the problem by replacing
SerialPort1.Read(Buffer_array_received_data, 0, 128)
with
For byte_pos As Int16 = 0 To 127 Step 1
Buffer_array_received_data(byte_pos) = SerialPort1.ReadByte()
Next
But aren't they supposed to be the same ?

Working with very big data faster in Matlab?

I have to deal with very big data (Point clouds generally more than 30 000 000 points) using Matlab. I can read ascii data using textscan function. After reading, I need to detect invalid data (points with 0,0,0 coordinates) and then I need to do some mathematical operations on each point or each line in the data. In my way, first I read data with textscan and then I assign this data to a matrix. Secondly, I use for loops for detecting invalid points and doing some mathematical operations on each point or line in the data. A sample of my code is shown as below. According to profile tool of Matlab textscan takes 37% and line
transformed_list((i:i),(1:4)) = coordinate_list((i:i),(1:4))*t_matrix;
takes 35% of all computation time.
I tried it with another point cloud (stores around 5 500 000) and profile tool reported same results. Is there a way of avoiding for loops, or is there another way of speeding up this computation?
fileID = fopen('C:\Users\Mustafa\Desktop\ptx_all_data\dede5.ptx');
original_data = textscan(fileID,'%f %f %f %f %f %f %f', 'delimiter',' ');
fclose(fileID);
column = original_data{1}(1);
row = original_data{1}(2);
t_matrix = [original_data{1}(7) original_data{2}(7) original_data{3}(7) original_data{4}(7)
original_data{1}(8) original_data{2}(8) original_data{3}(8) original_data{4}(8)
original_data{1}(9) original_data{2}(9) original_data{3}(9) original_data{4}(9)
original_data{1}(10) original_data{2}(10) original_data{3}(10) original_data{4}(10)];
coordinate_list(:,1) = original_data{1}(11:length(original_data{1}));
coordinate_list(:,2) = original_data{2}(11:length(original_data{2}));
coordinate_list(:,3) = original_data{3}(11:length(original_data{3}));
coordinate_list(:,4) = 0;
coordinate_list(:,5) = original_data{4}(11:length(original_data{4}));
transformed_list = zeros(length(coordinate_list),5);
for i = 1:length(coordinate_list)
if coordinate_list(i,1) == 0 && coordinate_list(i,2) == 0 && coordinate_list(i,3) == 0
transformed_list(i,:) = NaN;
else
%transformed_list(i,:) = coordinate_list(i,:)*t_matrix;
transformed_list((i:i),(1:4)) = coordinate_list((i:i),(1:4))*t_matrix;
transformed_list(i,5) = coordinate_list(i,5);
end
%i
end
Thanks in advance
for loops with conditional statements like those will take ages to run. But what Matlab lacks in loop speed it makes up with vectorization and indexing.
Let's try some logical indexing like this to solve the first step:
coordinate_list(coordinate_list(:,1) == 0 .* ...
coordinate_list(:,2) == 0 .* ...
coordinate_list(:,3) == 0)=nan;
And then vectorize the second statement:
transformed_list(:,(1:4)) = coordinate_list(:,(1:4))*t_matrix;
As EBH mentioned above this might be a bit heavy on your RAM. If it's more than your computer can handle asks yourself if the coordinates really have to be doubles, maybe single precision will do. If that still doesn't do, try slicing the vector and performing the operation in parts.
Small example to give you an idea because I had a 2million element point cloud around here:
In R2015a
transformed_list = zeros(length(coordinate_list),5);
tic
for i = 1:length(coordinate_list)
if coordinate_list(i,1) == 0 && coordinate_list(i,2) == 0 && coordinate_list(i,3) == 0
transformed_list(i,:) = NaN;
else
%transformed_list(i,:) = coordinate_list(i,:)*t_matrix;
transformed_list((i:i),(1:3)) = coordinate_list((i:i),(1:3))*t_matrix;
transformed_list(i,5) = 1;
end
%i
end
toc
Returns Elapsed time is 10.928142 seconds.
transformed_list=coordinate_list;
tic
coordinate_list(coordinate_list(:,1) == 0 .* ...
coordinate_list(:,2) == 0 .* ...
coordinate_list(:,3) == 0)=nan;
transformed_list(:,(1:3)) = coordinate_list(:,(1:3))*t_matrix;
toc
Returns Elapsed time is 0.101696 seconds.
Rather than read the whole file, you'd be better off using a loop with
fscanf(fileID, '%f', 7)
and processing input as you read it.

Garry's mod Expression 2 for loop

In a game called Garry's mod there an add on called wiremod. Inside wiremod there is expression 2 which is a conmand line basiced coding so execute commands. The command For loop I'm having issues with because I can't do variable inside a variable. In CMD I could setlocal EnableLocalExtension and do !Var%Var%!
The code simpler is: (cap sensitive)
#inputs [GoCard1,GoCart2,GoCart3,GoCart4]:entity
X = 64
Y = 24
-N is the variable
-10 is max number
-1 is how much it increments by
for(N,10,1)
{
Menu:egpText(1,toString(GoCart1),vec2(X,Y)
Y+=24 -increase by 24
I++ -increase by 1
}
My problem is I can't change GoCart1 to GoCart2, GoCart3... Ect
Tried GoCartN but gives me an error GoCartN does not exists
Anyone have any ideas?
Short answer I'm going to separate the code
#inputs [GoCard1,GoCart2,GoCart3,GoCart4]:entity
X = 64
Y = 24
T = table(GoCart1,GoCart2,GoCart3,GoCart4)
for(N,10,1)
{
Menu:egpText(1,"1. "+toString(T[N,entity]),vec2(X,Y)
Y+=24 -increase by 24
I++ -increase by 1
}
If anyone know how to put GoCart in the loop please let me know

How to speed up MATLAB integration?

I have the following code:
function [] = Solver( t )
%pre-declaration
foo=[1,1,1];
fooCell = num2cell(foo);
[q, val(q), star]=fooCell{:};
%functions used in prosomoiwsh
syms q val(q) star;
qd1=symfun(90*pi/180+30*pi/180*cos(q),q);
qd2=symfun(90*pi/180+30*pi/180*sin(q),q);
p1=symfun(79*pi/180*exp(-1.25*q)+pi/180,q);
p2=symfun(79*pi/180*exp(-1.25*q)+pi/180,q);
e1=symfun(val-qd1,q);
e2=symfun(val-qd2,q);
T1=symfun(log(-(1+star)/star),star);
T2=symfun(log(star/(1-star)),star);
%anonymous function handles
lambda=[0.75;10.494441313222076];
calcEVR_handles={#(t,x)[double(subs(diff(subs(T1,star,e1/p1),q)+subs(lambda(1)*T1,star,e1/p1),{diff(val,q);val;q},{x(2);x(1);t})),double(subs(diff(subs(T1,star,e1/p1),q)+subs(lambda(1)*T1,star,e1/p1),{diff(val,q);val;q},{0;x(1);t})),double(subs(double(subs(subs(diff(T1,star),star,e1/p1),{val;q},{x(1);t}))/p1,q,t))];#(t,x)[double(subs(diff(subs(T2,star,e2/p2),q)+subs(lambda(2)*T2,star,e2/p2),{diff(val,q);val;q},{x(4);x(3);t})),double(subs(diff(subs(T2,star,e2/p2),q)+subs(lambda(2)*T2,star,e2/p2),{diff(val,q);val;q},{0;x(3);t})),double(subs(double(subs(subs(diff(T2,star),star,e2/p2),{val;q},{x(3);t}))/p2,q,t))]};
options = odeset('AbsTol',1e-1,'RelTol',1e-1);
[T,x_r] = ode23(#prosomoiwsh,[0 t],[80*pi/180;0;130*pi/180;0;2.4943180186983711;11.216948999754299],options);
save newresult T x_r
function dx_th = prosomoiwsh(t,x_th)
%declarations
k=0.80773938740480955;
nf=6.2860930902603602;
hGa=0.16727117784664769;
hGb=0.010886618389781832;
dD=0.14062935253218495;
s=0.64963817519705203;
IwF={[4.5453398382686956 5.2541234145178066 -6.5853972592002235 7.695225990702979];[-4.4358339284697337 -8.1138542053372298 -8.2698210582548395 3.9739729629084071]};
IwG={[5.7098975358444752 4.2470526600975802 -0.83412489434697168 0.53829395964565041] [1.8689492167233894 -0.0015017513794517434 8.8666804106266461 -1.0775021663921467];[6.9513235639494155 -0.8133752392893685 7.4032432556804162 3.1496138243338709] [5.8037182454981568 2.0933267947187457 4.852362963697928 -0.10745559204132382]};
IbF={-1.2165533594615545;7.9215291787744917};
IbG={2.8425752327892844 2.5931576770598168;9.4789237295474873 7.9378928037841252};
p=2;
m=2;
signG=1;
n_vals=[2;2];
nFixedStates=4;
gamma_nn=[0.31559428834175318;9.2037894041383641];
th_star_guess=[2.4943180186983711;11.216948999754299];
%solution
x = x_th(1:nFixedStates);
th = x_th(nFixedStates+1:nFixedStates+p);
f = zeros(m,1);
G = zeros(m,m);
ZF = zeros(p,m);
ZG = zeros(p,m,m);
for i=1:m
[f(i), ZF(:,i)] = calculate_neural_output(x, IwF{i}, IbF{i}, th);
for j=1:m
[G(i,j), ZG(:,i,j)] = calculate_neural_output(x, IwG{i,j}, IbG{i,j}, th);
end
end
detG = det(G);
if m == 1
adjG = 1;
else
adjG = detG*G^-1;
end
E = zeros(m,1);
V = zeros(m,1);
R = zeros(m,m);
for i=1:m
EVR=calcEVR_handles{i}(t,x);
E(i)=EVR(1);
V(i)=EVR(2);
R(i,i)=EVR(3);
end
Rinv = R^-1;
prod_R_E = R*E;
ub = f + Rinv * (V + k*E) + nf*prod_R_E;
ua = - detG / (detG^2+dD) * (adjG * ub) ;
u = ua - signG * (hGa*(ua'*ua) + hGb*(ub'*ub)) * prod_R_E;
dx_th = zeros(nFixedStates+p, 1); %preallocation
%System in form (1) of the IEEE paper
[vec_sys_f, vec_sys_G] = sys_f_G(x);
dx_nm = vec_sys_f + vec_sys_G*u;
%Calculation of dx
index_start = 1;
index_end = -1;
for i=1:m
index_end = index_end + n_vals(i);
for j=index_start:index_end
dx_th(j) = x(j+1);
end
dx_th(index_end+1) = dx_nm(i);
index_start = index_end + 2;
end
%Calculation of dth
AFvalueT = zeros(p,m);
for i=1:m
AFvalueT(:,i) = 0;
for j=1:m
AFvalueT(:,i) = AFvalueT(:,i)+ZG(:,i,j)*ua(j);
end
end
dx_th(nFixedStates+1:nFixedStates+p) = diag(gamma_nn)*( (ZF+AFvalueT)*prod_R_E -s*(th-th_star_guess) );
display(t)
end
function [y, Z] = calculate_neural_output(input, Iw, Ib, state)
Z = [tanh(Iw*input+Ib);1];
y = state' * Z;
end
function [ f,g ] = sys_f_G( x )
Iz1=0.96;
Iz2=0.81;
m1=3.2;
m2=2.0;
l1=0.5;
l2=0.4;
g=9.81;
q1=x(1);
q2=x(3);
q1dot=x(2);
q2dot=x(4);
M=[Iz1+Iz2+m1*l1^2/4+m2*(l1^2+l2^2/4+l1*l2*cos(q2)),Iz2+m2*(l2^2/4+l1*l2*cos(q2)/2);Iz2+m2*(l2^2/4+l1*l2*cos(q2)/2),Iz2+m2*l2^2/4];
c=0.5*m2*l1*l2*sin(q2);
C=[-c*q2dot,-c*(q1dot+q2dot);c*q1dot,0];
G=[0.5*m1*g*l1*cos(q1)+m2*g*(l1*cos(q1)+0.5*l2*cos(q1+q2));0.5*m2*g*l2*cos(q1+q2)];
f=-M\(C*[q1dot;q2dot]+G);
g=inv(M);
end
end
Its target is to simulate the control of a 2-DOF robotic arm using a certain control law. The results I get after running the simulation are correct(I have a graph of the output I should expect), but it takes ages to finish!
Is there anything I could do to speed up the process?
In order to improve the computational speed of any integration in Matlab, a few options are available to you:
Reduce the required accuracy (which you already have done)
Use an adapted integrator. As mentioned by #sanchises, sometimes ode23 can be longer than another ode solver in Matlab (if your equation is stiff for instance). You could try to determine which solver is most adapted from the documentation... Or simply try them all!
The best solution, but by far the most time consuming, would be to use a compiled language, such as C or Fortran. If the integration is but a part of your Matlab program, you could use Mex files, and translate only the integration to a compiled language. You could also create dynamic libraries in your compiled language and load them in Matlab using loadlibrary. I use loadlibrary and an integration routine written in Fortran for the integration of orbits and trajectories, and I get over 100 times speedup with Fortran vs. Matlab! Of course, technically, the integration is not in Matlab anymore... But the library or Mex files trick allows you to only convert the integration part of your program to a different language! A number of open source integrators are available, such as ODEPACK or RKSUITE in Fortran. Then, you only need to create a wrapper and your dynamics function in the correct language.
So to put it in a nutshell, if you're going to use this integration a lot, I would advise using a compiled language. If not, you should make do with Matlab, and be patient!

Resources