Wednesday, February 17, 2016

Free Open Source Software for Mechanical Engineering Students, Researchers & Professionals

Looking at the AICTE's list of Open Source alternatives to commercial s/w for Engineering students, and also at the chaos going on in this respect in several engineering campuses, I present here, based on my own experiences, a list of open source solutions for such learning and research situations, in particular, and also for industries.

As I have understood it, the basic set of ideas leading myself and several of my colleagues (for example, at IIT-B, University of Alaska Fairbanks, MIT, 2nd one from MIT, Virginia Tech, Harvard, London School of Economics, Sports Biomechanics Lab at UCDavis, San Francisco State University, The University of Utah, The California Institute of Technology (Caltech)) to the use of such software, since past about 20 years, are as follows:

1. Since there is perpetual shortage of funds, these free alternatives become the natural choice and a natural refuge for students, researchers as well as for professionals. In the Indian education scenario, it was surprising a few years back and is painful, even now, to see students taking bank loans to fund their education. Really tragic and painful stories are being now found in the media.

2. From the social perspective of a researcher and a s/w writer, I find it difficult to accept that such s/w should remain captive under the contemporary fund-owners. Each of such s/w is fundamentally a magnificent storehouse of knowledge and experiences of countless man/woman-hours of countless dedicated researchers, professionals etc.

3. In this discussion, the English word "generic" is very important. As per dictionaries, the word-meaning of "generic" is:

a: characteristic of or relating to a class or group of things; not specific:

b: of or relating to a whole group or class, not sold or made under a particular brand name

Obviously, the second word-meaning above is simply amazing! There is a concept of generic software, very much like generic medicine.  Thus, for example, for the generic 3D mechanical modeling s/w like Creo, Solidworks, Catia etc., there is the powerful open source alternative in the form of FreeCAD. It was really wonderful to find that the steps (like 2d drawing, pocketing, extruding etc.) encountered while undergoing a training on a commercial 3D modeling s/w, were found almost the same  in the online documentation/tutorial of FreeCAD.

Moreover, for a particular generic category of s/w, one may find different industrial entities using different commercial s/w, not much unlike different doctors prescribing from different brands for the same "generic medicine".

4. Particularly in the education institutes and universities, the objective is to impart the student the basic understanding of the s/w in a particular "generic" category. For this objective, the open source alternatives should be the natural choice, keeping in mind a comprehensive view of the situation. Also, it may again be emphasized that almost all of these s/w are widely used in research as well as industrial situations.


 The list (not complete, indeed):

1. Operating System: Linux, in the form of its several distributions. The most commonly used distributions being - Debian, Ubuntu and Fedora. For further motivation, see: 1 & 2.

2.  Office s/w: LibreOffice. No probs at all!!

3. C/C++: Most of the engineering students begin their coding experience with learning C/C++ on the blue TurboC/C++ ide which itself is known to be freely downloadable. However, the open source alternative gcc (used with vim and gnu make) provides a universe of advantages in terms of industry-readiness, comprehensive documentation etc. Still, if one wants to use the the blue TurboC/C++ ide, it is easily installable on Linux.

4. Painting, simple line-diagrams and 2D drafting: xfig.

5. AutoCAD: AutoCAD itself has been freely downloadable and installable via AICTE support since about 5 years. Moreover, there is DraftSight from Dassault systems.

6.3D modeling: As already discussed above, in the context of mechanical engineering, this is the most important case of free and open source alternative in the form of FreeCAD (ref.: University of Alaska Fairbanks, MIT, 2nd one from MIT, Virginia Tech, Harvard, Sports Biomechanics Lab at UCDavis, San Francisco State University, The University of Utah, The California Institute of Technology (Caltech)) which must be used in place of commercial alternatives.

7. Matlab: Octave is the Linux-community  solution I have myself been using since past several years. Scilab is another alternative, very strongly being advocated, for example, by academia from IIT Bombay.

8. CAM: "LinuxCNC controls CNC machines. It can drive milling machines, lathes, 3d printers, laser cutters, plasma cutters, robot arms, hexapods, and more."

In conclusion, with increasing laptop population (each laptop costing less than 10% of the expenditure on a typical engineering/technology UG degree (BTech/BE)), the best alternative to mechanical CAD and/or simulation labs would be to encourage students to bring to class their own laptops loaded with s/w solutions listed above, and/or, may be, the student versions of the commercial software.

The shortcomings found in these open source s/w, if any, should be removed by collaborative contributions from academia, governments etc. Indeed, this has already been happening since past several years.

Please do contact/comment for your suggestions, clarifications etc.

Thursday, February 4, 2016

A simple finite differencing example using Matlab/Octave

Listening to the queries of some students and researchers, a need was felt for writing a small and simple tutorial blog post giving a simple finite differencing example using Matlab/Octave.

Taking 'x' as the independent variable and 'f(x) = sin(x)' as the dependent variable, the attached code demonstrates the calculation of first two derivatives.

In the code, taking xVec (= x0, x2, x3 .... xn) to represent the nodal values of 'x',
and,

y1 = sin(xVec);


and,

y3 = cos(xVec);

the derivatives of f(x) at the nodal points are calculated as:

1st derivative:y2 = Dmatx*y1;% dy/dx = Dmatx * y1

2nd derivative:
y4 = Dmatx*Dmatx*y1;% d2y/dx2 = Dmatx * Dmatx * y1

It must be obvious that (y1, y2, y3, y4) are ((n + 11) by 1) vectors and Dmatx is (n + 1) dimension square matrix.

The resulting plots:


For 1at derivative (with n = 10):

For 2nd derivative (with n = 10):



For 1st derivative (with n = 20):
For 2nd derivative (with n = 20):


Please do contact/comment for your suggestions, clarifications etc.

Downloads.

Happy Matlabbing/Octaving!

Monday, December 21, 2015

A Python/openpyxl based timetable tool for automatically updating class, room/lab, teacher and centralized timetables taking an ERP-like input from an excel spreadsheet

A Python/openpyxl based timetable tool

1. Each row (starting from second) in the file “TT_ERP_Input1.xlsx” in “inputs” directory gives the per-event (lecture, tutorial or lab) input for preparing all the above mentioned four timetables using the four Python script files (*.py).

2. An empty row (actually, just the first cell in that row) is sensed to stop further processing of the above stated input-file.

3. The folder “templates” has templates used to prepare class, teacher, room/lab and centralized timetables.

4. The four python scripts for preparing these four timetables are in the base directory.

5. I have used Python 2.7.9 and openpyxl 2.3.2 versions. Using (Open Source) Linux Mint 17.2. However, it is well known that all these tools work just as fine on any other operating system or distribution. LibreOffice 4.3.3.2 was used as the spreadsheet s/w.

6. Each python-script may be run at command prompt as:

$ python makeclasses.py

or, inside the ipython shell (recommended) as:

In [1]: execfile('makeclasses.py')

Based on the contents of file (inputs/TT_ERP_Input1.xlsx), the exact number of "Class_*.xlsx" files are created in the current folder. Same for the other three “*.py” script-files (Of course, the script "makecentralized.py" creates only one "Centralized.xlsx"). A clash in each of these four types of timetables is highlighted by the corresponding cell filled red.

7. The zip-file “openpyxlTT.zip” has all the four script files, templates etc. with proper directory structure.

Feel free to communicate suggestions, comment,s etc. and to make your own modifications etc.

Happy timetabling !!!!!

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!