Unix C-Shell Programming Notes: Part II

This is the second part of this series of notes on the advanced use of the Unix shell. The first part can be accessed by following the link. In this second part, I will show some advanced examples I wrote for my research activity.

1. CHANGE FILE NAMES SCRIPT

I wrote this script to change the default names of the files generated during a molecular dynamics simulation by the program mdrun in the early version of the GROMACS package for MD simulation. GROMACS is a widely used software package for simulating the behavior of molecules and molecular systems. Although it is now possible to define the root name of the files upon running the simulation program, this script can still be helpful. The script is written in the C Shell (csh) .

 #! /bin/csh -ef
#
# Change the names of the default  output Files
# generated by the program GROMACS mdrun program
#
# (c) Danilo Roccatano
#######
#
#Define the radix of the files name
#
setenv RADIX  'EH_min'

if (-e md.log) then
   if (  -e $RADIX'.log')  then 
     echo Warning $RADIX'.log' exist
   else
     mv md.log            $RADIX'.log'
   endif
else
   echo The md.log file does not exist!
endif

if (-e ener.ene) then
  if (  -e 'e'$RADIX'.ene') then 
   echo Warning 'e'$RADIX'.ene' exist
  else
    mv ener.ene       'e'$RADIX'.ene'
  endif
else
  echo The energy file does not exist
endif

if (-e ctraj.xtc ) then
  if ( -e 'r'$RADIX'.xtc') then 
    echo Warning 'r'$RADIX'.xtc' exist
  else
    mv ctraj.xtc      'r'$RADIX'.xtc'
  endif
else
  echo The compressed trajectory file does not exist
endif

if (-e confout.gro) then
  if ( -e 'x'$RADIX'.gro') then 
    echo Warning 'x'$RADIX'.gro' exist
  else
    mv confout.gro    'x'$RADIX'.gro'
  endif
else
  echo The final configuration file does not exit.
  echo Probably your simulation crashed before the end 
endif
 
exit
Continue reading

Introducing the Mini LED Table: Compact, Affordable, and Enhanced With Computer Vision

How many times have you found yourself looking at your cake decorations, wondering how many pearls are left in the box? Have you ever tried to count how many mustard, basil or pepper plants you can grow with the seeds you have in stock? And what about the urge to know the exact number of millimeter stainless steel balls you recently purchased at your favorite DIY store?
For this purpose, some scrap cardboard, a strip of LEDs, and a Raspberry Pi Zero with its tiny chamber might help satisfy your mathematical desire to enumerate these tiny objects.
In a recent article on Instructables, we detailed the eye-opening experience with all the construction details of a compact, battery-powered, easy-to-use, and affordable mini LED table (MTL). In addition to the hardware, an open source software based on OpenCv is also strengthened for the acquisition and simple processing of the images of the objects on the MTL for possible applications.

Continue reading

Introduzione alla programmazione in Python (III)

Puntate precedenti:

  1. Introduzione alla programmazione in Python (I)
  2. Introduzione alla programmazione in Python (II)

Siamo giunti alla terza parte di questa serie dove vedremo come usare le istruzion condizionali per il controllo del flusso del programma.

DIAGRAMMI DI FLUSSO

Per una descrizione più semplice e intuitiva dei commandi per il controllo del programma, verranno utilizzati diagrammi di flusso. Un diagramma di flusso è un tipo di diagramma (grafico o simbolico) che rappresenta un algoritmo o un processo. Ogni passo nel processo è rappresentato da un simbolo diverso e contiene una breve descrizione del passo del processo. I simboli del diagramma di flusso sono collegati tra loro con frecce che mostrano la direzione del flusso del processo. Un diagramma di flusso mostra tipicamente il flusso dei dati in un processo, dettagliando le operazioni/passi in un formato pittorico che è più facile da capire rispetto alla lettura di un formato testuale.

Continue reading

Retro programming Nostalgia IV: Acid/Base Equilibrium and Titration (Part I)

The motivation for this article stems from my interest in retro-computing connected, on the one hand, to the re-evaluation of my youthful explorations of scientific computing in the BASIC language and, on the other, to the popularity they are having in the amateur sector in recent years and of teaching, single-board microcomputers (single-board computers, such as, for example, the Raspberry Pi). These small computers have considerably more power at a much lower cost than the microcomputers of the 80s. This made it possible to emulate the operating systems of the legendary Commodore home computer models and the MSX extension on these computers.

Therefore, a renewed interest in the BASIC programming language is also occurring. This interest in retro-computing reflects the nostalgia for the great emotions that the development of consumer information technology brought to my generation in the 1970s and 1980s. I remember being so struck by the creativity in using and programming these microcomputers that it redirected my scientific interests and academic career. I have written in other articles about my first programming adventures with Commodore home computers and MSX systems in the late 80s and early 90s and my rediscoveries in computer archaeology. Among the exhibits, I found a small program I used to study acid/base titrations developed in MSX BASIC. Therefore, I took the opportunity to write notes on acid-base balance and titration and thus provide a restored and improved version of my program to students passionate about programming and grappling with this important concept of analytical chemistry.

Continue reading

Easter 2023: Superquadric and Squared-Eggs

First there was the Great Cosmic Egg.

Huai-nan Tzu, China 100 BCE 

Another Easter is arriving, and here I am for a new article on old friends: the Eggs. In my blog for Easter 2021, I mentioned squared eggs paraphrasing a sentence attributed to the extraordinary jeweler Peter Carl Fabergé:” This year, your Highness, we will be featuring square eggs.” A hen is unlikely to make a cubic-shaped egg, but we can still transform a cooked egg to get the cuboid shape. If you look for squared eggs on the internet, you will find a tool called the egg cuber that does the magic. Unfortunately, I could not yet try one, but it seems to be doing an excellent job from the reviews. The shape of the egg is not a perfect cube but a cuboid, namely a cube with rounded corners. In my previous article, I anticipated that this shape could be obtained using the superquadric function. Curiouser and curiouser! In this article, I will give a bit of mathematical background and even a source code to play with such a function as my Easter Bunny gift.

Continue reading

FORTRAN Programming (PART III): NUMERICAL DIFFERENTIATION

In the fall of 1972 President Nixon announced that the rate of increase of inflation was decreasing. This was the first time a sitting president used the third derivative to advance his case for reelection.

Hugo Rossi. Mathematics Is an Edifice, Not a Toolbox, Notices of the AMS, v. 43, no. 10, October 1996.

Welcome to the third part is the third part of the essential introduction to the FORTRAN programming series. In this part, we will introduce the use of functions and subroutines to structure the programs. Then we use them to make a program to calculate numerical derivatives of mathematical functions.

Continue reading

Numeric Solutions of the Schrödinger equation

For Schrödinger, the atom was an oscillating system — like the string of a musical instrument — for which there exist a number of modes of oscillation (fundamentals and overtones) which are interpreted as the atom’s energy states.

Armin Hermann. Werner Heisenberg 1901-1976.

This set of slides is from one of my courses in Computational Chemistry. It covers the one-dimensional solution of the Schrödinger equation using Numerov’s method. The material for this article was mainly taken from the excellent Levin textbook in the reference.
If you are interested in more details, please like it and subscribe to my website. This way, I will be motivated to add more of these contents.

Continue reading

FORTRAN Programming: A Basic Introduction (PART II)

In college, before video games, we would amuse our- selves by posing programming exercises. One of the favorites was to write the shortest self-reproducing program. Since this is an exercise divorced from reality, the usual vehicle was FORTRAN. Actually, FORTRAN was the language of choice for the same reason that three-legged races are popular.

Ken Thompson, Communications of the ACM. 27 (8), 761–763, 1984.

This is the second part of the basic introductin to FORTRAN programming series.

Control Statements 

Relational operators

Relational operators are used for comparison between variables

< or .lt. : less than 

<= or .le. : less than or equal to 

> or .gt. : greater than 

>= or .ge. :greater than or equal to

== or .eq. : equal to  

/= or .ne. : not equal to

Logical operators

Logical operator are used to perfrom

.and. : Logical multiplication ( and) 

.or. : Logical addition (or) 

.not. : Logical negation (not)

Continue reading

FORTRAN Programming: A Basic Introduction (PART I)

In college, before video games, we would amuse our- selves by posing programming exercises. One of the favorites was to write the shortest self-reproducing pro- gram. Since this is an exercise divorced from reality, the usual vehicle was FORTRAN. Actually, FORTRAN was the language of choice for the same reason that three-legged races are popular.

Ken Thompson, Communications of the ACM. 27 (8), 761–763, 1984.

In December of last year, I celebrated the 30th anniversary of my Laurea in Chemistry dissertation. The starting of my thesis dissertation also signed my acquaintance to one of the grannies of the scientific programming languages, FORTRAN. Since then, I have used and (continue to) this language for my research activity by writing several thousands of lines of code. Therefore, I want to share some of my modest programming achievements using this language.

I will concisely introduce this captivating programming language in a series of articles. This is a primer on a programming language with much more to offer, especially in the new versions starting from the FORTRAN 90. Readers interested in deepening their knowledge in FORTRAN can find online many excellent tutorials and discussion groups, as well as plenty of excellent textbooks that have been written.

The FORTRAN language 

Fortran (FORmula TRANslation) language was introduced in 1957 and remains the language of choice for most scientific programming. The language was constantly restyled and updated (e.g. Fortran IV, Fortran 77). Recent improvements, recently introduced with the Fortran 90 and 95/2003, include several extensions in more modern languages (e.g. in the C language). Some of the most important features of Fortran 90/95 include recursive subroutines, dynamic storage allocation and pointers, user-defined data structures, modules, and the ability to manipulate entire arrays. Fortran 90 is compatible with Fortran 77 but not the other way around. However, the new Fortran language has evolved in a modern computer language by incorporating constructs from other languages.  

Continue reading

A Practical Introduction to the C Language for Computational Chemistry. Part 4

Controlling complexity is the essence of computer programming.

Brian W. Kernighan. in Software Tools (with PJ Plauger), 1976.

THE FUNCTIONS

A C program is a collection of functions. A C function is equivalent to the subroutine in FORTRAN or BASIC and procedures in PASCAL, PERL, or PYTHON programming languages. A portion of the program cannot be executed independently but only as part of another program. The function contains a specific algorithm or a stand-alone procedure. You have already used several library functions in your previous programs. Output commands for printing or reading files (such as printf(), openf()), mathematical functions (sqrt(), cos() are a library or intrinsic functions as well. Other library functions, we can classify as follows

  • Input/output functions. Input/output on the computer devices (e.g. output to the terminal, printer, hard disk, input from keyboard). It is usually used with #include <stdio.h>;
  • String manipulation functions. This library contains common operations on strings (e.g., concatenation, length, search, and extraction of substrings). It is usually used with #include <string.h>;
  • Mathematical functions. Mathematical calculations (e.g. trigonometrics functions, exponentiation, square root extraction). It is usually used with #include <math.h>;
  • Graphical functions. Function for graphics operations (open a graphical window and canvas) and drawing graphical primitives (e.g. points, line, curves).
  • Operative system control functions. Operation requiriing allocation of the computer resources or devices (e.g. date and time, allocation of memory). It is usually used with, for example, #include <time.h>;
  • Data conversion functions. Operation for data conversion (e.g. change characters type, ascii to integer). It is usually used with #include <ctype.h>;

To use these function, you need to use the precompiler instruction #include at the beginning of the program. The compiler use by deafult the standard library #include <stdlib.h>;

You can write your functions, and it is called user-defined functions. The use of function allows the program to structure and makes its organization and reading easier. C language is structured around the use of functions. The main() function is a function that contains calls to other functions, both intrinsic and user-defined functions.

Continue reading