AnthonyKalaitzis / NumFort

Numerical library for fortran with matlab/python like bindings

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

numFort

Description

numFort is a numerical library for FORTRAN that includes quadpack, lapack, PLplot and Matplotlib source code. It enables the use of handy mathematical macros such as linspace. I have wasted so much of my life on this nonsense.

Installation - Linux/Unix

You will have to at least install PLplot or Pyplot in order to use one or the other for plotting algorithms or both. You may then comment out the other where specified below at run time.

ifort

  • Before using numFort, make sure to download the ifort compiler and the necessary math kernel libraries for the use of LAPACK. These may be downloaded from the following link (free for students) Parallel studio XE, click the C++ link unless on mac
  • Simply download the custom install, extract to a location and run under sudo privileges like so
# If you wish to install it globally use the below sudo prefix (not necessary)
sudo ./install_GUI.sh
  • You may wish to install it locally instead though (hit option 3 i think after typing ./install_GUI.sh)
  • Make sure to check the math kernel for install
  • After installation you may need to export the path to the ifort compiler. Add code similar to below in your ~/.bashrc. Your exact path for the above libraries might be different (probably just different numbers for 2018.3.222)
source /path/to/intel/bin/compilervars.sh intel64
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/intel/compilers_and_libraries_2018.3.222/linux/mkl/lib/intel64_lin/
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/intel/compilers_and_libraries_2018.3.222/linux/compiler/lib/intel64_lin/
  • Close and open the terminal to refresh
  • Check that it’s working by typing
ifort --version
  • Your ifort version should display

Python

Conda

Download anaconda from here download link. navigate in the terminal to where the final was downloaded and type the following. For us the title of the file was “Anaconda-latest-Linux-x86_64.sh”

bash Anaconda-latest-Linux-x86_64.sh

follow the prompts and it should install. Say yes to append the path to your .bashrc. If it didn’t add the necessary path in your .bashrc then simply type the following in your .bashrc.

export PATH="/path/to/anaconda3/bin:$PATH"

Try typing

python3.7 --version

If nothing pops up type

conda install python=3.7

Matplotlib & Numpy

This should come with matplotlib, numpy and math. To test this open a python shell with python3.7 and type the following

import matplotlib.pyplot as pypl
import numpy as np
import math as m

as long as no errors pop up you should be fine. Write the following lines in the terminal

cd
mkdir bin
ln -s /path/to/anaconda3/bin/python3.7 ~/bin/python3.7

This will create a symbolic link for python3.7. Add the following to your .bashrc

export PATH=$PATH:~/bin

NumFort

  • Simply run the install file with ./install.sh or bash install.sh (same for the uninstall file). Follow the prompts and you’re good. When using you may use any of the modules listed in the documentation. Copy the makefile template found after installation into your directory with you .f90 file.
  • The install will create an example makefile. Simply copy this makefile to your .f90 file and run said commands depending on your plotting needs
  • You may also choose to make a bashfile that runs these commands for you to save time e.g.
#!/bin/bash

make all
./program
# Run your perferred plotting
python pyplot.py
# or
python customPlot.py

# simply run this file by typing "chmod +x bashFile.sh" and then "./bashFile.sh"

Documentation

The four modules you may include are kinds, lapack95 and numFort. Simply include in any .f90 file via a simple use statement as per usual.

program progName
      use kinds
      use lapack95
      use numFort

      implicit none
      ...

end progName

Below we will give short descriptions of the module files and the subroutines and functions contained inside.

Listed variables (some may be optional) and examples of how to call:

Kinds

Precision parameter file. Main uses are for constants like pi and making variables double precision.

lapack95

see online lapack documentation for an extensive list on possible linear algebra computations online documentation.

NumFort

Factorial

Calculates the factorial of n

VariableDescription
ninteger
factorialoutputted factorial
m = factorial(n)

bessel

Calculate the value of the 0<n<5 order bessel fucntion at x

VariableDescription
ninteger, order of bessel
xreal double precision
besselvalue of the bessel function
value = bessel(x,n)

Deriv

Numerically calculates the derivative via a centred finite difference method.

VariableDescription
fInput function
x0value to calculate
derivvalue of numerical derivative
function f(x)
value = deriv(f,x0)

Trace

Calculate the trace of a matrix

VariableDescription
M(N,N)Matrix, SP or DP for real or complex matrices
TraceSame type as input matrix
value = trace(M)

Inv

Calculates the inverse of a matrix

VariableDescription
M(N,N)Matrix, SP or DP for real or complex matrices
MinvInverse of M
Minv = inv(M)

Meshgrid

Creates a unique lattice of points for two given vectors x and y. Usually used for making a 3D grid for 3 dimensional plots.

VariableDescription
x(N)double precision vector
y(M)double precision vector
XX(M,N)double precision matrix
YY(M,N)double precision matrix
call meshgrid(x,y,XX,YY)

FFT

Calculates the fast Fourier transform

VariableDescription
x(N)double precision vector
call FFT(x)

Splinefit

Fits a cubic spline to inputted data. This function can return the coefficients or just a list of desired points to be interpolated at.

VariableDescription
x(N)double precision vector
y(N)double precision vector
xj(N)this is the vector x for calculation use
c(N)coefficients for spline fit
xpoint to evaluate fit at
splinevaloutput values for fit
c = splinefit(x,y,c)
! should be called after splinefit
value = splineval(c,xj,x)

PolyFit

Exactly the same as SplineFit but for a Nth order polynomial.

VariableDescription
Ninteger, order of polynomial
x(N)double precision vector
y(N)double precision vector
c(N+1)coefficient of fit
c = polyfit(x,y,N)
! Should be called after polyfit
value = polyCal(c,x)

GuessZero

Given a set of values or a function with boundaries, returns the approximate value of where the function changes sign. An index is returned for inputted values method and the x value exactly is returned for the function method.

VariableDescription
finput function
fvalslist of y values for a function
a,brange for zero guess
GuessZerointeger index of zero location
value = guesszero(fvals)

function f(x)
value = guesszero(f,a,b)

writeData

Writes variables to a data file which can then be used with customPlot.py to use python to plot (requires numpy and matplotlib)

VariableDescription
xreal double precision
yreal double precision
zreal double precision
wreal double precision
titlename of the file to load
! title variable is optional
call writeData(x,y,title)
call writeData(x,y,z,w,title)

Newton1D

Performs a 1 dimensional newtons method to find the zero of a function.

VariableDescription
fnInput function
guessinitial guess of zero of the function
newton1Dzero of function guess location
function fn(x)
value = newton1D(fn,x)

EulerM

Performs Eulers method to solve a single or N coupled DE’s, same call notation as rk4

VariableDescription
t0initial value to start stepping at
y0initial y value(s)
finput function(s)
hstep size
nEqnumber of coupled equations
rk4output (yn+1)
function f(t,y)
value = eulerM(f,h,t0,y0)

! In the N DE case, y = y(N),f = f(N), values = values(N)
! i.e. N initial conditions and equations
function f(t,y,nEq)
values = eulerM(f,h,t0,y0)

rk4

Performs a 4th order Runge Kutta solving algorithm for a given DE. Algorithms giving for a single DE or N coupled DE.

VariableDescription
t0initial value to start stepping at
y0initial y value(s)
finput function(s)
hstep size
nEqnumber of coupled equations
rk4output (yn+1)
function f(t,y)
value = rk4(f,h,t0,y0)

! In the N DE case, y = y(N),f = f(N), values = values(N)
! i.e. N initial conditions and equations
function f(t,y,nEq)
values = rk4(f,h,t0,y0)

integral

Numerically calculates an integral given a function and bounds. Using Gaussian quadrature.

VariableDescription
fInput function
aleft bound
bright bound
absErrabsolute error
relErrrelative error
integralnumerical value of integral
function f(x)
value = integral(f,a,b,absErr,relErr)

integralPV

Numerically calculates a Cauchy-Principle value integral using Gaussian quadrature. For a given f(x), evaluates the integral of f(x)/(x-c).

VariableDescription
fInput Function
cPole
aLeft bound
bRight bound
absErrabsolute error
relErrrelative error
integralPVnumerical value of integral
function f(x)
value = integralPV(f,c,a,b,absErr,relErr)

Linspace

Creates a linear space of points between a and b with N points.

VariableDescription
startleft bound
finishright bound
Nnumber of points, integer
linspacevector of points between a and b
vector = linspace(a,b,N)

PrintTime

Prints the computation time after calls for cpu_time

VariableDescription
timeIInitial time, real(DP)
timeFFinal time, real(DP)
call PrintTime(timeI,timeF)

pythonPlot

python plotting wrappers, simply copy the makefile and bashFortran.sh files from the InsertCode directory into the necessary directory and then on execution of make all, the necessary plotting files will be created. Use the following commands inside your fortran files (see the bashFortran file to decide whether you wish to use custom plotting or automatic plotting)

VariableDescription
x(N,M)multi-dimensional array
x(N)x values
y(N)y values
xaxisx axis title (optional)
yaxisy axis title (optional)
legendlegend (optional)
titletitle (optional)
call pyplot(x,title,xaxis,yaxis,legend)
call pyplot(x,y,title,xaxis,yaxis)

PLplots

Call PLplot by using the subroutine plot() for example, this will call various wrappers to plplot which can be found within numFort. Below is a list of said wrappers and their arguments. Axes labels and title may be omitted in all below routines if one wishes.

plot

standard x vs y plot or even x1,x2,… vs y1,y2,…

VariableDescription
x(N)x values
y(N)y values
data(N,M)multi-dimensional data
xlabelx axis title (optional)
ylabely axis title (optional)
titletitle (optional)
call plot(x,y,xlabel,ylabel,title)
call plot(data,xlabel,ylabel,title)

scatterplot

standard scatter plot

VariableDescription
x(N)x values
y(N)y values
stylepoints style e.g. “+”
xlabelx axis title (optional)
ylabely axis title (optional)
titletitle (optional)
call scatterplot(x,y,style,xaxis,yaxis,title)

surf

3D surface plot (goes well with meshgrid)

VariableDescription
x(N)x values
y(N)y values
z(N,N)z values
xlabelx axis title (optional)
ylabely axis title (optional)
zlabelz axis title (optional)
titletitle (optional)
call surf(X,Y,Z,xlabel,ylabel,zlabel,title)

scatter3D

3D scatter plot.

VariableDescription
x(N)x values
y(N)y values
z(N)z values
xlabelx axis title (optional)
ylabely axis title (optional)
zlabelz axis title (optional)
titletitle (optional)
call scatter3D(X,Y,Z,xlabel,ylabel,zlabel,title)

About

Numerical library for fortran with matlab/python like bindings


Languages

Language:Fortran 99.7%Language:Shell 0.2%Language:Python 0.1%