Thursday, November 20, 2014

Preparing a LaTeX article for Elsevier - example paper "Solving a heat equation in Octave/Matlab"

Below are the details of one of my recent presentation to postgraduate students.

Presentation Title: Preparing a LaTeX article for Elsevier

Example paper: "Solving a heat equation in Octave/Matlab"

The Octave script: myoctave1.m

The LaTeX input file: elsarticle-template-num.tex

The zip-file containing the five image files( one xfig file and its eps copy, four eps files having screen-shots of windows having resulting from the octave script given below) : allfigures.zip

The commands (on Ubuntu 14.04 with texlive etc.):

For getting the plots from Octave:

in konsole or any other terminal:

user@domain:~$ octave

octave> myoctave1

This pops out four windows having a plot each - the screen-shots given in the file allfigures.zip mentioned above.

For preparing the paper

latex elsarticle-template-num.tex
dvipdf elsarticle-template-num.dvi


The otput:
elsarticle-template-num.dvi

and 

elsarticle-template-num.pdf


Thanks for visiting!

Thursday, February 6, 2014

A tutorial on C++ based GSL code for solving partial differential equations (PDE) with finite differencing

Presented below is a C++ based code example of using GSL for solving partial differential equations with finite differencing.

The example problem is the free vibration case of an Euler Bernoulli beam.

Field equation:

EI (d4w/dx4) + (rho*A) (d2w/dt2) = 0

Boundary conditions:

Clamped:

w = 0, dw/dx = 0

Hinged:

w = 0, d2w/dx2 = 0

Free:

d3w/dx3 = 0, d2w/dx2 = 0

Notations are the usual ones.


The code begins

#include < iostream >
#include < fstream >
#include < math.h >

//REF.: http://www.gnu.org/software/gsl/manual/html_node/Linear-Algebra-Examples.html#Linear-Algebra-Examples
#include < stdio.h >
#include < gsl/gsl_linalg.h >
//REF.: http://www.gnu.org/software/gsl/manual/html_node/Linear-Algebra-Examples.html#Linear-Algebra-Examples

//REF.: https://www.gnu.org/software/gsl/manual/html_node/BLAS-Examples.html#BLAS-Examples
#include < gsl/gsl_eigen.h >
#include < gsl/gsl_blas.h >
//REF.: https://www.gnu.org/software/gsl/manual/html_node/BLAS-Examples.html#BLAS-Examples

using namespace std;

int main (){

 cout<<"Hello, i am eulerbeam1... \n";

 fstream ofl1;
 ofl1.open("eulerbeam1.res", ios::out | ios::ate);

 //REF.: http://www.techscience.com/doi/10.3970/sl.2011.006.115.pdf
 double E = 2.1e11, rho = 7.85e3, l = 1, b = 0.01, d = 0.005;
 double I = b*d*d*d/12, A = b*d;
 
 int Nnodes = 60;

 double x0 = 0, xn = 1;
 double deltax = (xn - x0)/(Nnodes - 1);
 double onb2deltax = 1./(2*deltax);

 gsl_vector * vector1 = gsl_vector_alloc(Nnodes);

 gsl_matrix * Cmatx_nt = gsl_matrix_calloc (Nnodes, Nnodes);//Coefficients for first derivative
 gsl_matrix * Cmatx_nt2 = gsl_matrix_calloc (Nnodes, Nnodes);//Coefficients for second derivative
 gsl_matrix * Cmatx_nt3 = gsl_matrix_calloc (Nnodes, Nnodes);//Coefficients for third derivative
 gsl_matrix * Cmatx_nt4 = gsl_matrix_calloc (Nnodes, Nnodes);//Coefficients for fourth derivative
 gsl_matrix * Cmatx_Id = gsl_matrix_calloc (Nnodes, Nnodes);//the Identitiy matrix
 gsl_matrix * Cmatx_K = gsl_matrix_calloc (Nnodes, Nnodes);//the Stiffness matrix
 gsl_matrix * Cmatx_M = gsl_matrix_calloc (Nnodes, Nnodes);//the Mass matrix
 gsl_matrix * Cmatx_invK_M = gsl_matrix_calloc (Nnodes, Nnodes);//for storingthe inv(K)*M

 //for 0th node
 gsl_matrix_set(Cmatx_nt, 0, 0, -1./deltax);
 gsl_matrix_set(Cmatx_nt, 0, 1, 1./deltax);
 //for nth node
 gsl_matrix_set(Cmatx_nt, Nnodes - 1, Nnodes - 2, -1./deltax);
 gsl_matrix_set(Cmatx_nt, Nnodes - 1, Nnodes - 1, 1./deltax);
 for(int i = 1; i < Nnodes - 1; i++){
  gsl_matrix_set(Cmatx_nt, i, i - 1, -onb2deltax);
  gsl_matrix_set(Cmatx_nt, i, i + 1, onb2deltax);
 }

 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
     1.0, Cmatx_nt, Cmatx_nt,
     0.0, Cmatx_nt2);
 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
     1.0, Cmatx_nt, Cmatx_nt2,
     0.0, Cmatx_nt3);
 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
     1.0, Cmatx_nt2, Cmatx_nt2,
     0.0, Cmatx_nt4);

 gsl_matrix_set_identity(Cmatx_Id);


 gsl_matrix_memcpy(Cmatx_K, Cmatx_nt4);

 gsl_matrix_get_row(vector1, Cmatx_Id, 0);//for (deflection, w = 0) at x = 0
 //gsl_matrix_get_row(vector1, Cmatx_nt3, 0);//for (shear force, d3w/dx3 = 0) at x = 0
 gsl_matrix_set_row(Cmatx_K, 0, vector1);

 gsl_matrix_get_row(vector1, Cmatx_Id, Nnodes - 1);//for (deflection, w = 0) at x = 1
 //gsl_matrix_get_row(vector1, Cmatx_nt3, Nnodes - 1);//for (shear force, d3w/dx3 = 0) at x = 1
 gsl_matrix_set_row(Cmatx_K, Nnodes - 1, vector1);

 //gsl_matrix_get_row(vector1, Cmatx_nt, 0);//for (slope, dw/dx = 0) at x = 0
 gsl_matrix_get_row(vector1, Cmatx_nt2, 0);//for (moment, d2w/dx2 = 0) at x = 0
 gsl_matrix_set_row(Cmatx_K, 1, vector1);

 //gsl_matrix_get_row(vector1, Cmatx_nt, Nnodes - 1);//for (slope, dw/dx = 0) at x = 1
 gsl_matrix_get_row(vector1, Cmatx_nt2, Nnodes - 1);//for (moment, d2w/dx2 = 0) at x = 1
 gsl_matrix_set_row(Cmatx_K, Nnodes - 2, vector1);

 gsl_matrix_set_identity(Cmatx_M);
 gsl_matrix_set(Cmatx_M, 0, 0, 0);
 gsl_matrix_set(Cmatx_M, 1, 1, 0);
 gsl_matrix_set(Cmatx_M, Nnodes - 2, Nnodes - 2, 0);
 gsl_matrix_set(Cmatx_M, Nnodes - 1, Nnodes - 1, 0);

//Computing inv(K)*M through LU decomposition
 int signum; 
 gsl_permutation * p_lu = gsl_permutation_alloc (Nnodes);
 gsl_linalg_LU_decomp(Cmatx_K, p_lu, &signum);
 for(int i=0; i < Nnodes; i++){
  gsl_matrix_get_col (vector1, Cmatx_M, i); 
  gsl_linalg_LU_svx(Cmatx_K, p_lu, vector1);
  gsl_matrix_set_col(Cmatx_invK_M, i, vector1);
 }


//Computing eigenvalues and eigenvectors
 gsl_vector_complex *eval = gsl_vector_complex_alloc (Nnodes);
 gsl_matrix_complex *evec = gsl_matrix_complex_alloc (Nnodes, Nnodes);
 gsl_eigen_nonsymmv_workspace * myeigen_ws =
  gsl_eigen_nonsymmv_alloc (Nnodes);
 gsl_eigen_nonsymmv (Cmatx_invK_M, eval, evec, myeigen_ws);
 gsl_eigen_nonsymmv_free (myeigen_ws);
 gsl_eigen_nonsymmv_sort (eval, evec,
   GSL_EIGEN_SORT_ABS_DESC);
  

//saving the frequencies(Hz) in ascending order
 int i, j;
 double dumi= (1./(2*M_PI*l*l))*sqrt(E*I/(rho*A));
 for (i = 0; i < Nnodes; i++)
 {
  gsl_complex eval_i
   = gsl_vector_complex_get (eval, i);
  gsl_vector_complex_view evec_i
   = gsl_matrix_complex_column (evec, i);
  ofl1 << sqrt(1./(GSL_REAL(eval_i)))*dumi << " i" << GSL_IMAG(eval_i) << endl;
 }



 ofl1.close();
 cout << "Hello, i was eulerbeam1... \n";
 return 0;
}



The code ends.
Compilation command on a Linux console/command line:

g++ -lgsl eulerbeam1.cc -o eulerbeam1

Execution:
./eulerbeam1

Results give the following fundamental frequencies (Ref.: http://www.techscience.com/doi/10.3970/sl.2011.006.115.pdf):

Clamped-Clamped: 26.5281Hz
Clamped-Free: 4.44362 Hz
Simply-Supported: 11.7253 Hz






Friday, May 10, 2013

Matlab/Octave for the UG students of the subject - Control Systems


Students of the subject “Control Systems” can use GNU Octave for practising Matlab code.
Following is an example of a simple session while using Octave version 3.6.3 under a Fedora 17 installation.
BODE PLOT
Example 1:
System: (1 + s)

octave:1> sys1 = tf([1 1],[1])

Transfer function 'sys1' from input 'u1' to output ...

y1: s + 1

Continuous-time model.
octave:2> bode(sys1)
octave:5>

The resulting plot:



Example 1:
System: 30/(1 + 0.5s)(s + 1)(s + 5)

octave:13> [numer, denom] = zp2tf([],[-2 -1 -5], 60)
numer = 60
denom =

1 8 17 10

octave:14> sys2 = tf(numer, denom)

Transfer function 'sys2' from input 'u1' to output ...

60
y1: -----------------------
s^3 + 8 s^2 + 17 s + 10

Continuous-time model.
octave:16> close all
octave:17> bode(sys2)
octave:18>
The resulting plot:
Thanks for visiting!

Wednesday, February 20, 2013

A simple Python based tool for preparing randomized multi-set multiple choice question papers

A simple Python based tool for preparing randomized multi-set multiple choice question papers
Scipy.in 2012, IIT-BPecha Kucha Presentation
December 27-29 2012
Ashish Sharma

The Problem
 A class full of students
 So much to teach
 Frequent, continuous evaluations – QUIZES
 Multiple choice or objective type questions
 Not enough invigilators
 Time constraints
 Quality of evaluations

The Solution
 Randomized multiple choice question papers – orders of questions and answers
 Python - for the randomized text manipulation
 The input is a set of three text (txt or tex) files containing – the questions and the answers,  the header and the footer
 The output is a chosen number question papers containing the same sets of questions and answer choices – both randomly differentiated in terms of order

The Code (download the zip file here: for text & for latex) – imports
import sys, random
import fileinput
from subprocess import call
........
infilename = sys.argv[1]
...........
for line in fileinput.input( infilename ):
...........
random.seed()
...........
call(['latex', outfilename + ".tex"])

A Typical Session – for text only
$ ./otqpr.py

Usage: ./otqpr.py infile.[txt/tex] N
N being the number of versions to be created.
infileH.[txt/tex] and infileF.[txt/tex] must exist to define the header and the footer

$ ls
in1F.txt  in1H.txt  in1.txt  otqpr.py

$ ./otqpr.py in1.txt 3

$ ls
in1F.txt  in1H.txt  in1_OUTV0.txt  in1_OUTV1.txt  in1_OUTV2.txt  in1.txt  otqpr.py

A Typical Session – for latex input$ ./otqpr.py

Usage: ./otqpr.py infile.[txt/tex] N
N being the number of versions to be created.
infileH.[txt/tex] and infileF.[txt/tex] must exist to define the header and the footer

$ ls
Fig2.eps in1F.tex  in1H.tex  in1.tex  otqpr.py

$ ./otqpr.py in1.tex 3

$ ls
Fig2.eps in1F.tex  in1H.tex in1_OUTV0.pdf in1_OUTV1.pdf in1_OUTV2.pdf in1.tex otqpr.py

Typical Outputs – formatted .txt files
VERSION 1
Q. 1: The reason we are not crushed under the atmospheric pressure is
(A) the pressure inside us is almost same as the atmospheric pressure (B) the pressure is too small (C) we are strong enough (D) the resulting force is too small
Q. 2: A force can be providing
(A) neither a push nor a pull (B) either a push or a pull (C) only a pull (D) only a push

VERSION 2
Q. 1: The reason we are not crushed under the atmospheric pressure is
(A) we are strong enough (B) the pressure is too small (C) the pressure inside us is almost same as the atmospheric pressure (D) the resulting force is too small
Q. 4: A force can be providing
(A) neither a push nor a pull (B) either a push or a pull (C) only a push (D) only a pull


Thanks!

Thursday, November 22, 2012

Using Octave/Matlab for plotting a histogram showing frequency of byte-values in a file


octave:1> myfile = fopen('Pachmarhi3.png', 'rb');
octave:2> mydata = fread(myfile, 'uint8');
octave:3> size(mydata)
ans =
           72181       1
octave:4> hist(mydata, (0:255)');

octave:5> axis([0 255]);

octave:6> xlabel ("Byte value")

octave:7> ylabel ("Counts")
octave:8> title ("A histogram showing frequency of byte-values in a file");



octave:9>

 The file "Pachmarhi3.png":

Saturday, March 24, 2012

Potential areas for doing PhD using open source tools

1. General modeling and simulation problems across disciplines.
2. Computational mechanics of laminated composite plates - including static, dynamic (vibration) & buckling problems, both linear as well as non-linear
3. Applications of computational tools like Genetic Algorithm, Simulated Annealing, Neural Networks, Fuzzy logic etc.
4. Computation Fluid Mechanics (CFD) applied in several areas like - flow through pipes, bearings etc.
5. Cryptography - chaos based image encryption,  watermarking etc.
6. Computational Geometry applied to various fields like Finite Element Method (FEM), biology, CAD, computer communications, wild fire propagation etc.

Sunday, January 9, 2011

Solid Mechanics

Formulae: Stress Transformation
The necessary formulae for the plane stress case are as follows(ref. Solid Mechanics, Popov).Stresses on the $\theta$-plane.\begin{displaymath} \sigma_{x'}=\frac{\sigma_x+\sigma_y}{2} + \frac{\sigma_x-\sigma_y}{2} cos(2\theta) + \tau_{xy}sin(2\theta) \end{displaymath}\begin{displaymath} \tau_{x'y'}=-\frac{\sigma_x-\sigma_y}{2} sin(2\theta) + \tau_{xy}cos(2\theta) \end{displaymath}Principal planes\begin{displaymath} tan(2\theta_{1})=\frac{2\tau_{xy}}{\sigma_x-\sigma_y} \end{displaymath}This gives principal planes at $\theta_{11}$ and $\theta_{12}(=\theta_{11}+\pi/2)$. Principal stresses $(\sigma_{1}$ and $\sigma_2)$ corresponding to these planes are found from the formula for $(\sigma_{x'})$ above.Maximum shear stress $(\sigma_1$ ~$ \sigma_2)/2$ and its planes

Planes at 
and,
 
are the planes on which maximum shear stress occurs. Sense of maximum shear stresses corresponding to these planes are found from the formula for $(\tau_{x'y'})$ above.


Principal stresses and maximum shear stress can also be found directly as,