Fortran Runtime Error during read from file - io

I'm trying to read in a .gro file which looks like this:
1coh C 1 0.206 -0.049 0.429
1coh O1 2 0.295 0.048 0.501
1coh H3 3 0.252 -0.137 0.424
1coh H4 4 0.186 -0.014 0.337
I need to store the 0.206, -0.049, 0.429 into arrays.
I use this to recognise the coh in the lines:
open(unit=20,file=grofile2)
do i=1,3
read(20,'(A)')line
lname2=line(6:8)
enddo
close(unit=20)
Then, read in the specific lines using:
open(unit=20,file=grofile2)
150 read(20,'(A)',end=151)line
if(line(6:8).eq.lname2)then
i=i+1
lig2(i)=line
write(*,*)lig2(i)
endif
goto 150
151 continue
close(unit=20)
natoms2=i
The write here does output the lines to the terminal correctly.
I then follow this with:
do j=1,natoms2
write(*,*) j
write(*,*) lig2(j)
read(lig2(j)(23:28),*)lig2x(j)
read(lig2(j)(31:36),*)lig2y(j)
read(lig2(j)(39:44),*)lig2z(j)
write(*,*) lig2x(j),lig2y(j),lig2z(j)
enddo
to read the individual values to these arrays.
At this point I get the error:
At line 145 of file samgen.f
Fortran runtime error: Bad real number in item 1 of list input
Line 145 is:read(lig2(j)(23:28),*)lig2x(j)
This syntax has worked for this file in a different program, but is not working here. I've tried using F6.3 instead of * in the read line. I've checked for variable declarations and types. What am I doing wrong? Any ideas? Or possibly a better way to read this in?

I take 'test.gro' file as an example.
1coh C 1 0.206 -0.049 0.429
1coh O1 2 0.295 0.048 0.501
1coh H3 3 0.252 -0.137 0.424
1coh H4 4 0.186 -0.014 0.337
"I need to store the 0.206, -0.049, 0.429 into arrays." I did not understand what follows in your question. Does this quite simple code do what you need?
program read
character :: char1*5, char2*3
integer :: i1
real :: r1, r2, r3
open (30, FILE='test.gro', STATUS='OLD')
do i=1,4
! I would read without format specification: it is much easier
! though type of variable must be correct!
read(30,*) char1, char2, i1, r1, r2,r3
!test what I have read:
print*, char1, char2, i1, r1, r2, r3
end do
close (30)
end program

Related

Illegal input detected on my modeling on Scilab

I've been trying to run this on Scilab, but I can't get it to work.
global ke Yxs Va Kg A D cx cxa cxamax fbc Kp Yps Ypx umax rho V mg mga mbc nxa
ke=0.003/(10^9);
Yxs=45*10^9;
Va=0.005;
Kg=0.04;
A=0.004657;
D=4.5*10^(-10);
cx=10^9;
cxa=10^9;
cxamax=1163*10^9;
fbc=0.01;
Kp=0.01;
Yps=0.9;
Ypx=0.01125;
umax=1.6;
rho=1030;
V=0.2;
function ydot=g(t,y)
ydot=zeros(y);
ydot(1)=((umax*((y(3)/Va)/((y(3)/Va)+Kg)))-ke*Yxs)*y(1);
ydot(2)=-(D*A*(((y(2)/V)-(y(3)/Va))/((y(4)/(rho*fbc))/A)));
ydot(3)=(D*A*(((y(2)/V)-(y(3)/Va))/((y(4)/(rho*fbc))/A)))-((umax*((y(3)/Va)/((y(3)/Va)+Kg)))/Yxs+(Ypx*(umax*((y(3)/Va)/((y(3)/Va)+Kg)))*(y(3)/Va)/(cga+Kp))/Yps)*y(1);
ydot(4)=(Ypx*(umax*((y(3)/Va)/((y(3)/Va)+Kg)))*(y(3)/Va)/(cga+Kp))*y(1);
endfunction
t0=0;
t=0:1:50;
y0=ones(0,4,4,0);
y=ode(y0,t0,t,g);
This is what the console returns when I run it.
lsoda-- neq (=i1) .lt. 1
where i1 is : 0
Illegal input detected (see printed message).
at line 52 of executed file C:\Users\pandrade\Documents\Pessoal\Ufla\modelagem artigo.sce
ode: lsoda exit with state -3.
Line 52 is the last line of the code, where the ode command is. Does anyone know what's wrong with it?
Your intialization of y0 is incorrect. Maybe you meant:
y0=[0;4;4;0]
or
y0=ones(4,1)
In any case y0 should be a column vector of 4 elements. Moreover you probably misspelled cga instead of cxa in the code of g(t,y).
Remember to always test that your right hand side function (here g) works outside the context of the ode call, by typing something like
g(0,y0)
on the Scilab command line.

Reading a text file linewise

I have a text file which looks like this:
0.031 0.031 0.031 1.4998 0.9976 0.5668 0.9659
0.062 0.031 0.031 0.9620 0.7479 0.3674 0.4806
0.094 0.031 0.031 0.3549 0.0738 0.0054 0.3471
0.125 0.031 0.031 0.4270 0.3422 0.2180 0.1332
0.156 0.031 0.031 1.0766 0.9005 0.3868 0.4455
0.188 0.031 0.031 0.9285 0.6619 0.0161 0.6509
0.219 0.031 0.031 1.1200 0.6464 0.3230 0.8557
and so on...(32768 lines because it's a 32^3 grid.)
The first three columns represent x,y and z coordinates , the 4th column being the norm of the coordinates and the last three columns represent the components of a 3D vector. I need to calculate curl and divergence of the vector field( 5,6,7) using central difference method at each point. Since there are a lot of lines, I want to parallelize the input using MPI. But for this to happen,I need to have access to three lines at once as central difference method states (for eg)
My fortran code for reading in data from the file is:
open(unit = 1,file = '32data.txt') !
do i = 1,32767
read(1,*) x(i),y(i),z(i),norm(i),xv(i),yv(i),zv(i)
end do
do i = 2,32767
dx = x(i) - x(i-1)
dy = y(i) - y(i-1)
dz = z(i) - z(i-1)
Fx(i) = (xv(i+1) - xv(i-1))/(2.0)*dx
Fy(i) = (yv(i+1) - yv(i-1))/(2.0)*dy
Fz(i) = (zv(i+1) - zv(i-1))/(2.0)*dz
div(i) = Fx(i)+Fy(i)+Fz(i)
end do
So I need to send chunk of lines to different processes. BUT there's a problem here. Let's say process 2 takes 4 lines and process 3 takes the next 6. To calculate the gradient for the first line in process 3, I need data from the previous line in process 2. So I need to send that again to process 3. So is it possible to parallelize this in MPI? Or should I follow something else?

Displaying a rounded matrix

I want to display a vector with a predefined precision. For instance, let us consider the following vector,
v = [1.2346 2.0012 0.1230 0.0001 1.0000]
If I call,
mat2str(v, 1);
the output should be,
1.2 2.0 0.1 0.0 1.0
If I call,
mat2str(v, 2)
the output should be,
1.24 2.00 0.12 0.00 1.00
and so on.
I tried this code, but it resulted in an empty matrix:
function s = mat2str(mat, precision)
s = sprintf('%.%df ', precision, round(mat, precision));
end
mat2str(similarity, 3)
ans =
Empty string: 1-by-0
How can I display a vector with a predefined number of decimal places?
The format specifier for sprintf already provides an easy way to do this by using * for the precision field and passing that value as an argument to sprintf. Your function (which I renamed to mat2prec) can therefore be written as follows:
function s = mat2prec(mat, precision)
s = sprintf('%.*f', precision, mat);
end
This one works on my Matlab 2014b:
function s = mat2str(mat, precision)
printstring=strcat('%',num2str(precision),'.',num2str(precision),'f','\t');
s = sprintf(printstring, round(mat, precision));
end
function roundedmat2str(X,N)
NN = num2str(N); % Make a number of the precision
for ii = size(X,1):-1:1
out(ii,:) = sprintf(['%.' NN 'f \t'],X(ii,:)); % create string
end
disp(out)
end
X=magic(3)+rand(3);N=2;
MyRounding(X,N)
8.69 1.03 6.77
3.32 5.44 7.80
4.95 9.38 2.19
X = X(:).';
MyRounding(X,N)
8.69 3.32 4.95 1.03 5.44 9.38 6.77 7.80 2.19
Note that sprintf and fprintf already do implicit rounding when setting the number of decimals.
Also: please don't use existing function names for your own functions or variables. Never call a sum sum, a mean mean, or a mat2str mat2str. Do things like total, average and roundedmat2str. This makes your code portable and also makes sure you don't error out when you're using your own function but expect the default and vice-versa.
I think this is what you wanted to do in the first place:
s = sprintf(sprintf('%%.%df ', precision), mat)
EDIT
In case you want to extend your question to matrices, you could use this slightly more complicated one-liner:
s = sprintf([repmat(sprintf('%%.%df ', precision), 1, size(mat, 2)) '\n'], mat')
One noticeable difference with the previous one-liner is that it ends with a carriage return.

Statsmodels GLM and OLS with formulas missing paramters

I am trying to run a general linear model using formulas on a data set that contains categorical variables. The results summary table appears to be leaving out one of the variables when I list the parameters?
I haven't been able to find doc's specific to the glm showing the output with categorical variables but I have for the OLS and it looks like it should list each categorical variable seperately. When it do it (with GLM or OLS) it leaves out one of the values for each category. For example:
import statsmodels.formula.api as smf
import pandas as pd
Data = pd.read_csv(root+'/Illisarvik/TestData.csv')
formula = 'Response~Day+Class+Var'
gm = sm.GLM.from_formula(formula=formula, data=Data,
family=sm.families.Gaussian()).fit()
ls = smf.ols(formula=formula,data=Data).fit()
print (Data)
print(gm.params)
print(ls.params)
Day Class Var Response
0 D A 0.533088 0.582931
1 D B 0.839837 0.075011
2 D C 1.454716 0.505442
3 D A 1.455503 0.188945
4 D B 1.163155 0.144176
5 N A 1.072238 0.918962
6 N B 0.815384 0.249160
7 N C 1.182626 0.520460
8 N A 1.448843 0.870644
9 N B 0.653531 0.460177
Intercept 0.625111
Day[T.N] 0.298084
Class[T.B] -0.439025
Class[T.C] -0.104725
Var -0.118662
dtype: float64
Intercept 0.625111
Day[T.N] 0.298084
Class[T.B] -0.439025
Class[T.C] -0.104725
Var -0.118662
dtype: float64
C:/Users/wesle/Dropbox/PhD_Work/Figures/SkeeterEtAlAnalysis.py:55: FutureWarning: sort is deprecated, use sort_values(inplace=True) for INPLACE sorting
P.sort()
Is there something wrong with my model? The same issue presents its self when I print the full summary table:
print(gm.summary())
print(ls.summary())
Generalized Linear Model Regression Results
==============================================================================
Dep. Variable: Response No. Observations: 10
Model: GLM Df Residuals: 5
Model Family: Gaussian Df Model: 4
Link Function: identity Scale: 0.0360609978309
Method: IRLS Log-Likelihood: 5.8891
Date: Sun, 05 Mar 2017 Deviance: 0.18030
Time: 23:26:48 Pearson chi2: 0.180
No. Iterations: 2
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
Intercept 0.6251 0.280 2.236 0.025 0.077 1.173
Day[T.N] 0.2981 0.121 2.469 0.014 0.061 0.535
Class[T.B] -0.4390 0.146 -3.005 0.003 -0.725 -0.153
Class[T.C] -0.1047 0.170 -0.617 0.537 -0.438 0.228
Var -0.1187 0.222 -0.535 0.593 -0.553 0.316
==============================================================================
OLS Regression Results
==============================================================================
Dep. Variable: Response R-squared: 0.764
Model: OLS Adj. R-squared: 0.576
Method: Least Squares F-statistic: 4.055
Date: Sun, 05 Mar 2017 Prob (F-statistic): 0.0784
Time: 23:26:48 Log-Likelihood: 5.8891
No. Observations: 10 AIC: -1.778
Df Residuals: 5 BIC: -0.2652
Df Model: 4
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
Intercept 0.6251 0.280 2.236 0.076 -0.094 1.344
Day[T.N] 0.2981 0.121 2.469 0.057 -0.012 0.608
Class[T.B] -0.4390 0.146 -3.005 0.030 -0.815 -0.064
Class[T.C] -0.1047 0.170 -0.617 0.564 -0.541 0.332
Var -0.1187 0.222 -0.535 0.615 -0.689 0.451
==============================================================================
Omnibus: 1.493 Durbin-Watson: 2.699
Prob(Omnibus): 0.474 Jarque-Bera (JB): 1.068
Skew: -0.674 Prob(JB): 0.586
Kurtosis: 2.136 Cond. No. 9.75
==============================================================================
This is a consequence of the way the linear model works.
For instance, where you have the categorical variable Day as far as the linear model is concerned this can be represented as just a single 'dummy' variable which is set to 0 (zero) for the value you mention first, namely D and one for the second value, namely N. Statistically speaking, you can recover only the difference between the effects of the two levels of this categorical variable.
If you now consider Class, which has two levels, you have two dummy variables which represent two differences between the levels of the available three levels of this categorical variable.
As a matter of fact, it's perfectly possible to expand on this idea using orthogonal polynomials on the treatment means but that's something for another day.
The short answer is that there's nothing wrong, at least on this account, with your model.

Is there an fread analog for reading from stdin?

I'd like to use fread in a (R)script that would get input data via the linux pipe mechanism. Is there an fread analog for the following?
read.csv(file = 'stdin', ...)
I'll also settle for reading stdin some other way and then using fread to parse it, as I mainly want this for fread's superior separator and header logic.
Turns out it's as simple as:
fread('file:///dev/stdin')
This works, because fread actually creates a temporary file when the first 7 characters are "file://" or "http://" and uses download.file to copy the data there and then fread that.
Update: As of version 1.8.11 one can use shell commands in fread, making another solution possible:
fread('cat /dev/stdin')
All of the read.* functions use 'scan' under their hoods. scan is fairly low level but does have the capacity for parsing lines of data into different classes.
> mat <- matrix(scan(), 4,4) # will paste in block of data
1: 0.5 0.1428571 0.25
4: 0.5 0.1428571 0.25
7: 0.5 0.1428571 0.25
10: 0.5 0.1428571 0.25
13: 0.5 0.1428571 0.25
16: 0.5
17: # Terminate with two <cr>'s
Read 16 items
> mat
[,1] [,2] [,3] [,4]
[1,] 0.5000000 0.1428571 0.2500000 0.5000000
[2,] 0.1428571 0.2500000 0.5000000 0.1428571
[3,] 0.2500000 0.5000000 0.1428571 0.2500000
[4,] 0.5000000 0.1428571 0.2500000 0.5000000
> lst <- scan(what=list(double(0), "a"))
1: 4 t
2: 6 h
3: 8 l
4: 8 8
5:
Read 4 records
> lst
[[1]]
[1] 4 6 8 8
[[2]]
[1] "t" "h" "l" "8"
You should also look at the ?connections page.

Resources