How to bypass isFromMockProvider() in Smali - android-gps

I tried to edit source code of an app which is used to locate servants
It has a function or parameter "isFromMockProvider()"
I guess this one is the culprit
I want to bypass it
So that App could not know that I am using Mock location
I am a python dev not know enough about Smali
Please Help..
Thank You..
.line 106
invoke-virtual {p0}, Landroid/location/Location;->isFromMockProvider()Z
move-result p0
const-string v1, "mocked"
invoke-interface {v0, v1, p0}, Lcom/facebook/react/bridge/WritableMap;->putBoolean(Ljava/lang/String;Z)V
:cond_61
return-object v0
.end method

Try replacing your lines with this
line 106
invoke-virtual {p0}, Landroid/location/Location;->isFromMockProvider()Z
move-result p0
const-string v1, "mocked"
const/4 v2, 0x0
invoke-interface {v0, v1, v2}, Lcom/facebook/react/bridge/WritableMap;->putBoolean(Ljava/lang/String;Z)V
:cond_61
return-object v0
Here I initialised another boolean (Z) variable v2 with false (0x0) and put it instead of p0 in putBoolean function call.
But I don't know if your original function has space for another register (v2). So check if .locals or .registers is more then 3.
Usually .locals N is first line in your function. Where N means that you can use N-1 registers name.
.method public static contains(Ljava/lang/String;)Z
.locals 1
const/4 v0, 0x0 # - GOOD
const/4 v1, 0x0 # - BAD. WE NEED TO WRITE .locals 2 to use v1 register
const/4 v2, 0x0 # - BAD. WE NEED TO WRITE .locals 3 to use v1 register

You should delete some lines related to isFromMockProvider()Z, just like this:
.line 106
sget-object p0, Ljava/lang/Boolean;->FALSE:Ljava/lang/Boolean;
invoke-interface {v0, v1, p0}, Lcom/fa. . . . . . .
:cond_61 . . . . . . . .

Related

Debugging <<loop>> error message in haskell

Hello i am encountering this error message in a Haskell program and i do not know where is the loop coming from.There are almost no IO methods so that i can hook myself to them and print the partial result in the terminal.
I start with a file , i read it and then there are only pure methods.How can i debug this ?
Is there a way to attach to methods or create a helper that can do the following:
Having a method method::a->b how can i somehow wrap it in a iomethod::(a->b)->IO (a->b) to be able to test in in GHCI (i want to insert some putStrLn-s etc ?
P.S My data suffer transformations IO a(->b->c->d->......)->IO x and i do not know how to debug the part that is in the parathesis (that is the code that contains the pure methods)
Types and typeclass definitions and implementations
data TCPFile=Rfile (Maybe Readme) | Dfile Samples | Empty
data Header=Header { ftype::Char}
newtype Samples=Samples{values::[Maybe Double]}deriving(Show)
data Readme=Readme{ maxClients::Int, minClients::Int,stepClients::Int,maxDelay::Int,minDelay::Int,stepDelay::Int}deriving(Show)
data FileData=FileData{ header::Header,rawContent::Text}
(>>?)::Maybe a->(a->Maybe b)->Maybe b
(Just t) >>? f=f t
Nothing >>? _=Nothing
class TextEncode a where
fromText::Text-> a
getHeader::TCPFile->Header
getHeader (Rfile _ ) = Header { ftype='r'}
getHeader (Dfile _ )= Header{ftype='d'}
getHeader _ = Header {ftype='e'}
instance Show TCPFile where
show (Rfile t)="Rfile " ++"{"++content++"}" where
content=case t of
Nothing->""
Just c -> show c
show (Dfile c)="Dfile " ++"{"++show c ++ "}"
instance TextEncode Samples where
fromText text=Samples (map (readMaybe.unpack) cols) where
cols=splitOn (pack ",") text
instance TextEncode Readme where
fromText txt =let len= length dat
dat= case len of
6 ->Prelude.take 6 .readData $ txt
_ ->[0,0,0,0,0,0] in
Readme{maxClients=Prelude.head dat,minClients=dat!!1,stepClients=dat!!2,maxDelay=dat!!3,minDelay=dat!!4,stepDelay=dat!!5} where
instance TextEncode TCPFile where
fromText = textToFile
Main
module Main where
import Data.Text(Text,pack,unpack)
import Data.Text.IO(readFile,writeFile)
import TCPFile(TCPFile)
main::IO()
main=do
dat<-readTcpFile "test.txt"
print dat
readTcpFile::FilePath->IO TCPFile
readTcpFile path =fromText <$> Data.Text.IO.readFile path
textToFile::Text->TCPFile
textToFile input=case readHeader input >>? (\h -> Just (FileData h input)) >>? makeFile of
Just r -> r
Nothing ->Empty
readHeader::Text->Maybe Header
readHeader txt=case Data.Text.head txt of
'r' ->Just (Header{ ftype='r'})
'd' ->Just (Header {ftype ='d'})
_ -> Nothing
makeFile::FileData->Maybe TCPFile
makeFile fd= case ftype.header $ fd of
'r'->Just (Rfile (Just (fromText . rawContent $ fd)))
'd'->Just (Dfile (fromText . rawContent $ fd))
_ ->Nothing
readData::Text->[Int]
readData =catMaybes . maybeValues where
maybeValues=mvalues.split.filterText "{}"
#all the methods under this line are used in the above method
mvalues::[Text]->[Maybe Int]
mvalues arr=map (\x->(readMaybe::String->Maybe Int).unpack $ x) arr
split::Text->[Text]
split =splitOn (pack ",")
filterText::[Char]->Text->Text
filterText chars tx=Data.Text.filter (\x -> not (x `elem` chars)) tx
I want first to clean the Text from given characters , in our case }{ then split it by ,.After the text is split by commas i want to parse them, and create either a Rfile which contains 6 integers , either a Dfile (datafile) which contains any given number of integers.
Input
I have a file with the following content: r,1.22,3.45,6.66,5.55,6.33,2.32} and i am running runghc main 2>err.hs
Expected Output : Rfile (Just (Readme 1.22 3.45 6.66 5.55 6.33 2.32))
In the TextEncode Readme instance, len and dat depend on each other:
instance TextEncode Readme where
fromText txt =let len= length dat
dat= case len of
To debug this kind of thing, other than staring at the code, one thing you can do is compile with -prof -fprof-auto -rtsopts, and run your program with the cmd line options +RTS -xc. This should print a trace when the <<loop>> exception is raised (or if the program loops instead, when you kill it (Ctrl+C)). See the GHC manual https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/runtime_control.html#rts-flag--xc
As Li-yao Xia said part of the problem is the infinite recursion, but if you tried the following code, then the problem still remains.
instance TextEncode Readme where
fromText txt =let len= length [1,2,3,4,5,6] --dat
dat= case len of
The second issue is that the file contains decimal numbers but all the conversion function are expecting Maybe Int, changing the definitions of the following functions should give the expected results, on the other hand probably the correct fix is that the file should have integers and not decimal numbers.
readData::Text->[Double]
--readData xs = [1,2,3,4,5,6,6]
readData =catMaybes . maybeValues where
maybeValues = mvalues . split . filterText "{}"
--all the methods under this line are used in the above method
mvalues::[Text]->[Maybe Double]
mvalues arr=map (\x->(readMaybe::String->Maybe Double).unpack $ x) arr
data Readme=Readme{ maxClients::Double, minClients::Double,stepClients::Double,maxDelay::Double,minDelay::Double,stepDelay::Double}deriving(Show)

EBNF: prefix and suffix-like operator in assembly code production

I'm trying to write down 6809 assembly in EBNF to write a tree-sitter parser.
I'm stuck on one certain production. In 6809 assembly, you can use a register as an operand and additionally de- or increment it:
LDA 0,X+ ; loads A from X then bumps X by 1
LDD ,Y++ ; loads D from Y then bumps Y by 2
LDA 0,-U ; decrements U by 1 then loads A from address in U
LDU ,--S ; decrements S by 2 then loads U from address in S
Mind the "missing" first operand in the second line of code. Here are the productions I wrote:
instruction = opcode, [operand], ["," , register_exp];
...
register_exp = [{operator}], register | register, [{operator}];
register = "X" | "Y" | "U" | etc. ;
operator = "+" | "-";
The problem is register_exp = .... I feel like there could be a more elegant way to define this production. Also, what happens if only a register is given to register_exp?
You probably need
register_exp = [{operator}], register | register, [{operator}] | register;
to allow register names without operators. Why do you find it not so elegant? Quite descriptive.

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

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...)

How to overcome simulation of ode issue in R?

I#m trying to solve and then optimise the parameter and initial condition values in the system of differential equations. However, I can't run the code due to error messages (this code was working fine when the FS was given as an equation). Due to the limited data points, I want to be able to optimise against E values the whole system (calculated using equation F). I've made the code work till the LLode function, so optimisation doesn't take place at all.
I've managed to solve the error thanks to forum replies (Lyngbakr):
initSim<-ode(initCond, tspan, hormonesode, params, method="ode45",atol = 1e-10, rtol = 1e-10,hmax=NULL,maxsteps=100000000000000000)
Warning message:
In rk(y, times, func, parms, method = "ode45", ...) :
Number of time steps 1 exceeded maxsteps at t = 0
I will appreciate any hint.
Malgosia
My code looks as follow:
tspan<-c(0,1,2,3,4,5)#,6,7,8,9,16,17,18,19)
E<-c(0.303,0.205,0.205,0.381,0.272,0.188)#,0.317,0.274,0.106,0.161,0.947,2.722,4.701,0.24)
names(df)=c("tspan","E")
require(deSolve)
#initial parameter values
#k1<-1.062169#0.370 7.754
k2<-1.908891#-0.00727284 0.022
#k3<-0.321
k4<-2.14
k5<-10.7
#k6<-1.07
A0<-1.38 #15.47
B0<-0.61 #0.298
C0<-0.28
#F0<-0.303#3.28803757 3.434
#define a weight vector outside LLODE function
wts<-sqrt(sqrt(E))
#combine parameters into a vector
params<-c(k2,k4,k5,A0,B0,C0)
names(params)<-c("k2","k4","k5","A0","B0","C0")
#ode function
hormonesode<-function(t,x,params){
A<-x[1]
B<-x[2]
C<-x[3]
D<-x[4]
E<-x[5]
F<-x[6]
# k1<-params[1]
k2<-params[1]
#k3<-params[3]
k4<-params[2]
k5<-params[3]
A0<-params[4]
B0<-params[5]
C0<-params[6]
P<-3.02796-3.1867*cos(0.314159*t)-0.55332*cos(2*0.314159*t)+0.362678*cos(3*0.314159*t)+0.486708*cos(4*0.314159*t)-0.10133*cos(5*0.314159*t)-0.21977*cos(6*0.314159*t)-0.08926*cos(7*0.314159*t)+0.222292*cos(8*0.314159*t)-1.05119*sin(0.314159*t)+0.855633*sin(2*0.314159*t)+0.176677*sin(3*0.314159*t)-0.05658*sin(4*0.314159*t)-0.34108*sin(5*0.314159*t)-0.15718*sin(6*0.314159*t)+0.397642*sin(7*0.314159*t)-0.0986*sin(8*0.314159*t)
FS<-0.1944+0.002017*cos(0.261799*t)+0.009603*cos(2*0.261799*t)+0.01754*cos(3*0.261799*t)+0.106208*cos(4*0.261799*t)+0.020423*cos(5*0.261799*t)+0.015417*cos(6*0.261799*t)+0.01079*cos(7*0.261799*t)+0.115042*cos(8*0.261799*t)+0.008853*sin(0.261799*t)+0.013523*sin(2*0.261799*t)+0.012254*sin(3*0.261799*t)+0.026053*sin(4*0.261799*t)+0.000957*sin(5*0.261799*t)-0.001*sin(6*0.261799*t)+0.002374*sin(7*0.261799*t)+0.026775*sin(8*0.261799*t)
dA<-1.0621*1/(1+(1/5*P)^5)*(1/3*(F^10)/(1+(F)*1/3)^10)-2.14*A;
dB<-75*(((A/5)^10)/(1+(A/5)^10))-8.56*B;
dC<-1.909*FS+0.321*FS*C- 0.749*C;
dD<-0.749*C- 0.749*D+0.214*FS*D^2;
dE<-0.749*D-0.749*E+0.214*B*E^2;
dF<-k2 + k4*D + k5*E-1.07*F;
output<-c(dA, dB, dC, dD, dE, dF)
list(output)
}
#Initial conditions
A0<-2500#1.0038#2.794
B0<-105#25.0061#6.13
C0<-0.018#0.02#0.06126
D0<-0
E0<-0
F0<-0.303#3.28803757 3.434
initCond<-c(A0, B0, C0, D0, E0, F0)
initCond
#run ode with initial guesses
initSim<-ode(initCond, tspan, hormonesode, params, method="ode45",atol = 1e-1, rtol = 1e-1,hmax=NULL, maxsteps=100000)
plot(tspan,initSim[,7], type="l", lty="dashed")
points(tspan,E)
initSim
LLode<-function(params){
A0<-params[4]
B0<-params[5]
C0<-params[6]
D0<-0
E0<-0
F0<-0.303
initCond<-c(A0, B0, C0, D0, E0, F0)
#Run the ODE
odeOut<-ode(initCond,tspan,hormonesode,params,method="ode45",atol = 1e-1, rtol = 1e-1,hmax=NULL, maxsteps=100000)
if(attr(odeOut,"istate")[1]!=0){
#Check if satisfactory 2 indicates perceived success of method 'lsoda', 0 for
#'ode45'. Other integrators may have different indicator codes
cat("Integration possibly failed\n")
LL<-.Machine$double.xmax*1e-06#indicate failure
}else{
y<-odeOut[,7] #measurement variable
wtDiff1<-(y-E)*wts#weighted difference
LL<-as.numeric(crossprod(wtDiff1))#Sum of squares
}
LL
}
# optimize uzing optim()
MLoptres<-optim(params,LLode,method="Nelder-Mead",
control=list(trace=0,maxit=500))
MLoptres
require(optimx)
optxres<-optimx(params,LLode,lower=rep(0,6),upper=rep(200,6),
method=c("nmkb","bobyqa"),
control=list(usenumDeriv=TRUE, maxit=500,trace=0))
summary(optxres,order=value)
bestpar<-coef(summary(optxres,order=value)[1,])
cat("best parameters:")
print(bestpar)
#dput(optxres,file='includes/C20bestpar.dput')
bpSim<-ode(initCond,tspan,hormonesode,bestpar,method="ode45")
X11()
plot(tspan,initSim[,7],type="l",lty="dashed")
points(tspan,E)
#points(tspan,MLoptres[,]/k,type="l",lty="twodash")
points(tspan,bpSim[,7],type="l")
title(main="Improved fit using optimx")

SPSS Syntax- new variable based on 3 variables

I need to create a new variable based on 3 variables.
If someone is coded as 1 against any 1 of 3 variables, they are coded as 1 in the new variable
If they don’t code 1 on any variable, but code as 2 against any 1 of 3 variables they are coded as 2 in the new variable
Everything else is coded as 99
In syntax, I’ve written this as:
IF (Keep_Any=1 OR Find_Any=1 OR Improve_Any=1) Keep_Find_Improve=1.
IF ((Keep_Find_Improve~= 1) & (Keep_Any=2 | Find_Any=2 | Improve_Any=2)) Keep_Find_Improve=2.
IF (Keep_Find_Improve~=1 & Keep_Find_Improve~=2) Keep_Find_Improve=99.
EXECUTE.
However, the first part correctly identifies cases coded as 1, but the rest of the syntax doesn't work. This is in despite of using some syntax that uses the exact same logic on other variables:
COMPUTE Keep_Any= Q9a_recoded = 1 | Q9b_recoded = 1 | Q9c_recoded = 1.
EXECUTE.
IF (Q9A_recoded=1 OR Q9B_recoded=1 OR Q9C_recoded=1) Keep_Any=1.
IF ((Keep_Any~=1) & (Q9A_recoded= 2 OR Q9B_recoded=2 OR Q9C_recoded=2)) Keep_Any=2.
IF (Keep_Any~=1 & Keep_Any~=2) Keep_Any=99.
EXECUTE.
Does anyone have any ideas of why this is happening and how to fix it?
Try:
COMPUTE Target1=99.
COMPUTE Target1=ANY(2, V1, V2, V3).
COMPUTE Target1=ANY(1, V1, V2, V3).
Or better still (more efficient, even if more lines of code)
DO IF ANY(1, V1, V2, V3)=1.
COMPUTE Target2= 1.
ELSE IF ANY(2, V1, V2, V3)=1.
COMPUTE Target2 = 2.
ELSE.
COMPUTE Target2=99.
END IF.
The reason your syntax isn't working is the second condition you are using:
IF ((Keep_Find_Improve~= 1) & ...
When the first condition wasn't met, Keep_Find_Improve is still missing, and therefore doesn't meet the condition of ~=1.
The same thing goes later for IF (Keep_Any~=1 & Keep_Any~=2).
So you shouldn't be comparing Keep_Find_Improve to 1 or 2, you should be checking if it has received a value or if it's still missing:
IF (Keep_Any=1 OR Find_Any=1 OR Improve_Any=1) Keep_Find_Improve=1.
IF (missing(Keep_Find_Improve) & (Keep_Any=2 | Find_Any=2 | Improve_Any=2)) Keep_Find_Improve=2.
IF missing(Keep_Find_Improve) Keep_Find_Improve=99.
* alternatively: recode Keep_Find_Improve(miss=99).
EXECUTE.
All this being said, I recommend you use the more advanced code suggested by #JigneshSutar.

Resources