Converting Fortran array to numpy array - numpy

I am in the process of converting a Fortran 90 code to python. The Fortran code includes multidimensional array like:
integer fvpair(3,12,2)
integer jpair(3,2)
real*8 aread(3,12)
I wonder if the following numpy array are correct equivalent, assuming zero initialization:
fvpair = np.array([[np.zeros(3)],[np.zeros(12)],[np.zeros(2)]])
jpair = np.array([[np.zeros(3)],[np.zeros(2)]])
aread = np.array([[np.zeros(3)],[np.zeros(12)]])

If you want to preserve the original Fortran array storage order (column-major), do not forget to pass the order='F' flag!
fvpair = np.zeros((3,12,2), dtype=int, order='F')
jpair = np.zeros((3,2), dtype=int, order='F')
aread = np.zeros((3,2), dtype=float64, order='F')

Related

Whether numpy array is immutable?

Arr = np.array([1,2,3,4,5])
Arr + 10 returns a new np array with 10 added to each cell
Original array isn't affected
Whereas Arr+=100 does the same but mutates the original array.
Does this mean numpy array is not immutable? Or is it shallow/deep copy?
I'm confused because operations like Arr[0]=100 mutate the original array?
numpy arrays are mutable.
__iadd__() modifies the array inplace.
try:
Arr += 10
__add__() will create a new array.

Python - Slicing an Array of float

I have two 1-D of array of float ('Xdata' and 'tdata'). I want to make a new variable named 'ratedata'. The problem is when I run the code, the console showed "IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices". How to encounter this problem? thank you.
the code:
dxdt_a = np.array(pd.read_excel('T50-katalis1-m14.xlsx',index_col=0,header=5))
Xdata = dxdt_a[:,1]
tdata = dxdt_a[:,0]
ratedata = np.zeros(len(Xdata))
for i in ratedata:
ratedata[i] = (Xdata[i+1]-Xdata[i])/(tdata[1]-tdata[0])

Creating 2d array and filling first columns of each row in numpy

I have written the following code for creating a 2D array and filing the first element of each row. I am new to numpy. Is there a better way to do this?
y=np.zeros(N*T1).reshape(N,T1)
x = np.linspace(0,L,num = N)
for k in range(0,N):
y[k][0] = np.sin(PI*x[k]/L)
Simply do this:
y[:, 0] = np.sin(PI*x/L)

calling a fortran dll from python using cffi with multidimensional arrays

I use a dll that contains differential equation solvers among other useful mathematical tools. Unfortunately, this dll is written in Fortran. My program is written in python 3.7 and I use spyder as an IDE.
I successfully called easy functions from the dll. However, I can't seem to get functions to work that require multidimensional arrays.
This is the online documentation to the function I am trying to call:
https://www.nag.co.uk/numeric/fl/nagdoc_fl26/html/f01/f01adf.html
The kernel dies without an error message if I execute the following code:
import numpy as np
import cffi as cf
ffi=cf.FFI()
lib=ffi.dlopen("C:\Windows\SysWOW64\DLL20DDS")
ffi.cdef("""void F01ADF (const int *n, double** a, const int *lda, int *ifail);""")
#Integer
nx = 4
n = ffi.new('const int*', nx)
lda = nx + 1
lda = ffi.new('const int*', lda)
ifail = 0
ifail = ffi.new('int*', ifail)
#matrix to be inversed
ax1 = np.array([5,7,6,5],dtype = float, order = 'F')
ax2 = np.array([7,10,8,7],dtype = float, order = 'F')
ax3 = np.array([6,8,10,9],dtype = float, order = 'F')
ax4 = np.array([5,7,9,10], dtype = float, order = 'F')
ax5 = np.array([0,0,0,0], dtype = float, order = 'F')
ax = (ax1,ax2,ax3,ax4,ax5)
#Array
zx = np.zeros(nx, dtype = float, order = 'F')
a = ffi.cast("double** ", zx.__array_interface__['data'][0])
for i in range(lda[0]):
a[i] = ffi.cast("double* ", ax[i].__array_interface__['data'][0])
lib.F01ADF(n, a, lda, ifail)
Since function with 1D arrays work I assume that the multidimensional array is the issues.
Any kind of help is greatly appreciated,
Thilo
Not having access to the dll you refer to complicates giving a definitive answer, however, the documentation of the dll and the provided Python script may be enough to diagnose the problem. There are at least two issues in your example:
The C header interface:
Your documentation link clearly states what the function's C header interface should look like. I'm not very well versed in C, Python's cffi or cdef, but the parameter declaration for a in your function interface seems wrong. The double** a (pointer to pointer to double) in your function interface should most likely be double a[] or double* a (pointer to double) as stated in the documentation.
Defining a 2d Numpy array with Fortran ordering:
Note that your Numpy arrays ax1..5 are one dimensional arrays, since the arrays only have one dimension order='F' and order='C' are equivalent in terms of memory layout and access. Thus, specifying order='F' here, probably does not have the intended effect (Fortran using column-major ordering for multi-dimensional arrays).
The variable ax is a tuple of Numpy arrays, not a 2d Numpy array, and will therefore have a very different representation in memory (which is of utmost importance when passing data to the Fortran dll) than a 2d array.
Towards a solution
My first step would be to correct the C header interface. Next, I would declare ax as a proper Numpy array with two dimensions, using Fortran ordering, and then cast it to the appropriate data type, as in this example:
#file: test.py
import numpy as np
import cffi as cf
ffi=cf.FFI()
lib=ffi.dlopen("./f01adf.dll")
ffi.cdef("""void f01adf_ (const int *n, double a[], const int *lda, int *ifail);""")
# integers
nx = 4
n = ffi.new('const int*', nx)
lda = nx + 1
lda = ffi.new('const int*', lda)
ifail = 0
ifail = ffi.new('int*', ifail)
# matrix to be inversed
ax = np.array([[5, 7, 6, 5],
[7, 10, 8, 7],
[6, 8, 10, 9],
[5, 7, 9, 10],
[0, 0, 0, 0]], dtype=float, order='F')
# operation on matrix using dll
print("BEFORE:")
print(ax.astype(int))
a = ffi.cast("double* ", ax.__array_interface__['data'][0])
lib.f01adf_(n, a, lda, ifail)
print("\nAFTER:")
print(ax.astype(int))
For testing purposes, consider the following Fortran subroutine that has the same interface as your actual dll as a substitute for your dll. It will simply add 10**(i-1) to the i'th column of input array a. This will allow checking that the interface between Python and Fortran works as intended, and that the intended elements of array a are operated on:
!file: f01adf.f90
Subroutine f01adf(n, a, lda, ifail)
Integer, Intent (In) :: n, lda
Integer, Intent (Inout) :: ifail
Real(Kind(1.d0)), Intent (Inout) :: a(lda,*)
Integer :: i
print *, "Fortran DLL says: Hello world!"
If ((n < 1) .or. (lda < n+1)) Then
! Input variables not conforming to requirements
ifail = 2
Else
! Input variables acceptable
ifail = 0
! add 10**(i-1) to the i'th column of 2d array 'a'
Do i = 1, n
a(:, i) = a(:, i) + 10**(i-1)
End Do
End If
End Subroutine
Compiling the Fortran code, and then running the suggested Python script, gives me the following output:
> gfortran -O3 -shared -fPIC -fcheck=all -Wall -Wextra -std=f2008 -o f01adf.dll f01adf.f90
> python test.py
BEFORE:
[[ 5 7 6 5]
[ 7 10 8 7]
[ 6 8 10 9]
[ 5 7 9 10]
[ 0 0 0 0]]
Fortran DLL says: Hello world!
AFTER:
[[ 6 17 106 1005]
[ 8 20 108 1007]
[ 7 18 110 1009]
[ 6 17 109 1010]
[ 1 10 100 1000]]

Initializing variables in gekko using the array model function

Defining an array of Gekko variables does not allow any arguments to initialize the variables. For example, I am unable to make an array of integer variables using the m.Array function.
I can make an array of variables using this syntax: m.Array(m.Var, (42, 42)). However, I don't know how to make this array an array of integer variables because the m.Var passed in to the m.Array function does not take any arguments.
I have a single variable as an integer variable:
my_var_is_an_integer_var = m.Var(0, lb=0, ub=1, integer=True)
I have an array of variables that are not integer variables:
my_array_vars_are_not_integer_vars = m.Array(m.Var, (42, 42))
I want an array of integer variables: my_array_vars_are_integer_vars = m.Array(m.Var(0, lb=0, ub=1, integer=True), (42,42)) (Throws error)
HOW DO I INITIALIZE THE VARIABLES IN THE ARRAY TO BE INTEGER VARIABLES???
Error when trying to initialize array as integer variables:
Traceback (most recent call last):
File "integer_array.py", line 7, in <module>
my_array_vars_are_not_integer_vars = m.Array(m.Var(0, lb=0, ub=1,
integer=True), (42,42))
File "C:\Users\wills\Anaconda3\lib\site-packages\gekko\gekko.py", line
1831, in Array
i[...] = f(**args)
TypeError: 'GKVariable' object is not callable
If you need to pass additional arguments when creating a variable array, you can use one of the following options. Option 1 creates a Numpy array while Options 2 and 3 create a Python list.
Option 1 (Preferred)
Create a numpy array with the m.Array function with additional argument integer=True:
y = m.Array(m.Var,(42,42),lb=0,ub=1,integer=True)
Option 2
Create a 2D list of variables with a list comprehension:
y = [[m.Var(lb=0,ub=1,integer=True) for i in range(42)] for j in range(42)]
Option 3
Alternatively, you can create an empty list (y) and append binary values to that list.
y = [[None]*42]*42
for i in range(42):
for j in range(42):
y[i][j] = m.Var(lb=0,ub=1,integer=True)
The UPPER and LOWER bounds can be changed after the variable creation but the integer option is only available at initialization. Don't forget to switch to the APOPT MINLP solver for integer variable solutions with m.options.SOLVER = 1. Below is a complete example that uses all three options but with a 3x4 array for x, y, and z.
from gekko import GEKKO
import numpy as np
m = GEKKO()
# option 1
x = m.Array(m.Var,(3,4),lb=0,ub=1,integer=True)
# option 2
y = [[m.Var(lb=0,ub=1,integer=True) for i in range(4)] for j in range(3)]
# option 3
z = [[None]*4]*3
for i in range(3):
for j in range(4):
z[i][j] = m.Var(lb=0,ub=1,integer=True)
# switch to APOPT
m.options.SOLVER = 1
# define objective function
m.Minimize(m.sum(m.sum(x)))
m.Minimize(m.sum(m.sum(np.array(y))))
m.Minimize(m.sum(m.sum(np.array(z))))
# define equation
m.Equation(x[1,2]==0)
m.Equation(m.sum(x[:,0])==2)
m.Equation(m.sum(x[:,1])==3)
m.Equation(m.sum(x[2,:])==1)
m.solve(disp=True)
print(x)
The objective is to minimize sum of all the elements in x, y, and z but there are certain constraints on an element, row, and columns of x. The solution is:
[[[1.0] [1.0] [0.0] [0.0]]
[[1.0] [1.0] [0.0] [0.0]]
[[0.0] [1.0] [0.0] [0.0]]]