Slowing Down is the Key…..

Slowing down is the key to increased speed.

Past couple of months I was dabbing with fortran GUI and trying pgplot graphics library. I have produced gui’s in c, vb and then integrated them with fortran, but creating GUIs from fortran was new to me.

As the exploration began I took the fire aim adjust approach!! Dived deep into the tutorials and anything that I could lay my hands on.

Quickly from tutorials I graduated to actually creating my own little programs. This went on for a couple of months.

In the beginning I was sprinting as hard as possible. Learning, doing, getting stuck, reading and then doing again. The pace was fast.

But as I become comfortable, my approach shifted. I slowed.

I wrote a program and pondered how and what am I actually doing. This slowing down and pondering doubled my learning. It felt like I was learning at greater pace with this slowdown.

So the technique I want to advocate to anyone learning a new programming language, a new analysis tool or cad software, is to sprint in the first few weeks. Race and learn as much as you are able to handle. Dive deep and continue the pace as long as you are able to.

When exhaustion, sense of acheivement begins to creep in, slow down. Become deliberate in what you do? Question why and what you are doing?

I hope applying this method will help you as much as it has helped me.

Hermite Polynomials.

Hermite Polynomials are showing up in all most all the technical papers that I am reading these days.

Their constant appearance lead me to investigate them further. After a couple of hour of digging, I have this fortran module ready to get and evaluate hermite Polynomials.

The figure shows Pgplot output of these polynomials. Click the image to enlarge.

On Wasting time and inefficiency

From the new rules blog

Wasting time and inefficiencies are the way to discovery. When Condé Nast’s editorial director Alexander Liberman was challenged on his inefficiencies in producing world-class magazines such The New Yorker, Vanity Fair, and Architectural Digest, he said it best: “I believe in waste. Waste is very important in creativity.”

Out of many things, inefficiencies have helped me learn java and pgplot at a faster rate.

Trying things, creating silly programs has produced so many wasteful programs, but each has sharpened the skills.

Animating Heart using pgplot with fortran

Here’s the code for the heart animation written in fortran, using pgplot and quickwin.

Not an optimized code but mashed quickly to get something up and running.

Find the story and code in action at the following post Did i miss the valentine’s day?

```      MODULE DATAitem

TYPE thedata
REAL x0,y0,xf,yf,dx,dy,speed
CHARACTER(len=1) schar
END TYPE

INTEGER,parameter :: isteps=15,iinum=2000
INTEGER inum
LOGICAL :: direction
END MODULE

PROGRAM heart

!
! Animating heart.
! by sukhbinder
! date: 27th Feb 2012
!

USE DATAitem
USE IFqwin

TYPE(qwinfo) :: winfo
INTEGER(4)   :: RESULT

INTEGER  :: pgopen,pgcurs
REAL xr(iinum),yr(iinum)
TYPE(thedata) alpa(iinum)
CHARACTER*1 ch

winfo%TYPE=QWIN\$MAX
RESULT= setwsizeqq(qwin\$framewindow,winfo)

RESULT=aboutboxqq("Heart animation IN PGPLOT\rSukhbinder SINgh\rVersion 1.0\r15Feb2012"C)

direction =.true.

IF(pgopen('/w9') .LE. 0) STOP

CALL pgenv(-2.0,2.0,-2.0,1.5,1,-2)

DO iii=100,2000,200
INum=iii
CALL INit
CALL mydelay(500)
CALL ani

END DO

CALL pgclos

CONTAINS

SUBROUTINE ani

DO i=1,isteps
CALL pgbbuf()

CALL pgsci(0)

CALL pgpt(inum,alpa%xf,alpa%yf,1)

CALL pgsci(2)
DO j=1,inum
IF(direction) THEN

alpa(j)%xf=alpa(j)%xf-alpa(j)%dx
alpa(j)%yf=alpa(j)%yf-alpa(j)%dy
ELSE
alpa(j)%xf=alpa(j)%xf+alpa(j)%dx
alpa(j)%yf=alpa(j)%yf+alpa(j)%dy
END IF

END DO
CALL pgpt(inum,alpa%xf,alpa%yf,1)
CALL mydelay(400)

CALL pgebuf()
END DO

IF(direction) THEN
direction = .FALSE.
ELSE
direction = .TRUE.
END IF

END SUBROUTINE ani

SUBROUTINE init

REAL xxx(3)

xr(1)=-2.0
xinc=real(4.0/inum)
yr(1)=(cos(xr(1)))**0.5 * cos(200*xr(1)) + (abs(xr(1)))**0.5 -0.7 &
* (4.0 -xr(1) * xr(1))**0.01
DO i=2,inum
xr(i)=xr(i-1)+xinc
yr(i)=(cos(xr(i)))**0.5 * cos(200*xr(i)) +  (abs(xr(i)))**0.5 -0.7 &
* (4.0 -xr(i) * xr(i))**0.01
END DO

CALL RANDOM_SEED
DO i=1,inum
alpa(i)%x0=xr(i)
alpa(i)%y0=yr(i)
CALL RANDOM_NUMBER(xxx)
xxx(1)=-20.0 + xxx(1)*40.0
xxx(2)=-20.0 + xxx(2)*40.0

xxx(3)=xxx(1)
alpa(i)%xf=xxx(1)
alpa(i)%yf=xxx(2)
alpa(i)%speed=xxx(3)
alpa(i)%dx=(alpa(i)%xf-alpa(i)%x0)/isteps
alpa(i)%dy=(alpa(i)%yf-alpa(i)%y0)/isteps
END DO

CALL pgsci(2)
CALL pgpt(inum,alpa%xf,alpa%yf,1)

END SUBROUTINE
END PROGRAM

SUBROUTINE mydelay(nnsec)
INTEGER :: COUNT, count_rate, count_max,nnsec,icount

CALL SYSTEM_CLOCK(count, count_rate, count_max)
icount = count+nnsec
DO
CALL SYSTEM_CLOCK(count, count_rate, count_max)
IF(count .GE. icount) EXIT

END DO

END SUBROUTINE```

Neutron Collisions, Stanislaw Ulam , Solitares and my Pgplot practice.

The year was 1946. Physicists at Los Alamos Scientific Lab were investigating radiation shielding and distance neutrons would travel in various materials. Despite having the data, the problem could not be solved with analytical calculations.

At the very same time, Stanislaw Ulam was convalescing from an illness and playing solitaires and thinking. His thoughts meandered around what are the chances that a Canfield solitaire laid out with 52 cards will come out successfully?

Stanislaw says “After spending a lot of time trying to estimate them by pure combinatorial calculations, I wondered whether a more practical method than “abstract thinking” might not be to lay it out say one hundred times and simply observe and count the number of successful plays. This was already possible to envisage with the beginning of the new era of fast computers, and I immediately thought of problems of neutron diffusion and other questions of mathematical physics, and more generally how to change processes described by certain differential equations into an equivalent form interpretable as a succession of random operations. Later, I described the idea to John von Neumann, and we began to plan actual calculations”

Thus the Monte Carlo Method was born.

Monte Carlo methods are a class of computational algorithms that rely on repeated random sampling to compute their results. These methods are most suited to calculation by a computer and tend to be used when it is infeasible to compute an exact result with a deterministic algorithm. Source wikipedia

If you read through the above Wikipedia article, it explains a method to estimate the value of Pi approximately using Monte Carlo Method. So here’s the Fortran program just doing that.

It uses PGPLOT as the graphics engine and you can see graphically see how the program estimates the value of Pi.

This slideshow requires JavaScript.

Really cool!! But not as cool as neutron collisions!

Heart in fortran

Well it’s not valentines day! But I just crossed my 5th wedding anniversary so it’s apt to do some heart warming geeky thing.

The above heart is rendered by pgplot from a program written in fortran.

The magic happens because of this little equation

y= sqrt(cos(x))*cos(200*x) + sqrt(abs(x)) – 0.7 * (4.0 – x*x)^0.01

-2.0>x<2.0

Here’s the complete program.

PROGRAM HEART

USE IFQWIN

TYPE(QWINFO) :: WINFO
INTEGER(4) :: RESULT
INTEGER :: PGOPEN
REAL XR(4000),YR(4000)

WINFO%TYPE=QWIN\$MAX
RESULT= SETWSIZEQQ(QWIN\$FRAMEWINDOW,WINFO)

IF(PGOPEN(‘?’) .LE. 0) STOP
XR(1)=-2.0
XINC=0.001
YR(1)=(COS(XR(1)))**0.5 * COS(200*XR(1)) + (ABS(XR(1)))**0.5 -0.7 * (4.0 -XR(1) * XR(1))**0.01
DO I=2,4000
XR(I)=XR(I-1)+XINC
YR(I)=(COS(XR(I)))**0.5 * COS(200*XR(I)) + (ABS(XR(I)))**0.5 -0.7 * (4.0 -XR(I) * XR(I))**0.01
END DO

CALL PGENV(-2.0,2.0,-2.0,1.5,1,-1)
CALL PGSCI(2)
CALL PGPT(4000,XR,YR,1)

CALL PGCLOS
END PROGRAM

Showed it to my wife. From her quizzical look, I guess a real heart shaped gift would have been more appropriate! 😉 Lesson learnt 🙂

Thanks walkingrandomly

Let It Snow in Fortran – the code

So here’s the code to do “let it snow” with Fortran.

If you are looking for developing graphics application with Intel Fortran compiler using pgplot and quickwin, then this small program can get you started.

The snow module is generic and creates the snow, while the main program deals with actually driving the snow.

You can download the program here and see the snow in action here

Have a look.

```      MODULE SNOW
!———————————————————————–
! Inspired from Google’s let it snow
! Developed by Sukhbinder Singh

! 30th December 2011
!
!———————————————————————–
INTEGER, PARAMETER :: NUM=200
INTEGER, PARAMETER :: ITIME=50
INTEGER, PARAMETER :: ISPEED=700
REAL :: Y(NUM),X(NUM),FALL(NUM)
REAL :: SFS(NUM),STEP(NUM),CURRSTEP(NUM)
REAL :: H,W,R
CONTAINS

!
!———————————————————————–
SUBROUTINE INIT()
DOUBLE PRECISION :: XX
CALL RANDOM_SEED

W=600.0
H=600.0
DO I=1,NUM
CALL RANDOM_NUMBER(XX)
SFS(I)=INT(1.0D0+XX*1.0D0)
CURRSTEP(I)=0
IF(SFS(I) .EQ. 1) THEN
CALL RANDOM_NUMBER(XX)
FALL(I)=INT(2.0D0+XX*2.0D0)
CALL RANDOM_NUMBER(XX)
STEP(I)=0.050D0+XX*0.1D0
ELSE

CALL RANDOM_NUMBER(XX)
FALL(I)=INT(3.0D0+XX*2.0D0)
CALL RANDOM_NUMBER(XX)
STEP(I)=0.050D0+XX*0.050D0
END IF
CALL RANDOM_NUMBER(XX)
XX=XX*W
X(I)= XX

CALL RANDOM_NUMBER(XX)
XX=XX*H
Y(I)= XX

!
END DO
END SUBROUTINE
!
!———————————————————————–
SUBROUTINE SNOWING
DOUBLE PRECISION :: DX,DY,XX

CALL RANDOM_SEED

DO I=1,NUM
DY=FALL(I)
DX = FALL(I)*COS(CURRSTEP(I))
Y(I)=Y(I)-DY
X(I)=X(I)-DX
!
IF((X(I) .LE. 0.0) .OR. (Y(I) .LE. 0.0)) THEN

CALL RANDOM_NUMBER(XX)
Y(I)=H*XX
CALL RANDOM_NUMBER(XX)
X(I)=XX*W
!
IF(SFS(I) .EQ. 1) THEN

CALL RANDOM_NUMBER(XX)
FALL(I)=INT(2.0D0+XX*2.0D0)
!
CALL RANDOM_NUMBER(XX)
STEP(I)=0.050D0+XX*0.1D0
ELSE
CALL RANDOM_NUMBER(XX)
FALL(I)=INT(3.0D0+XX*2.0D0)
CALL RANDOM_NUMBER(XX)
STEP(I)=0.050D0+XX*0.050D0
END IF

END IF
CURRSTEP(I)=CURRSTEP(I)+STEP(I)
END DO

END SUBROUTINE

!
!———————————————————————–
SUBROUTINE MYDELAY(NNSEC)
INTEGER :: COUNT, COUNT_RATE, COUNT_MAX,NNSEC,ICOUNT
CALL SYSTEM_CLOCK(COUNT, COUNT_RATE, COUNT_MAX)
ICOUNT = COUNT+NNSEC
DO

CALL SYSTEM_CLOCK(COUNT, COUNT_RATE, COUNT_MAX)
IF(COUNT .GE. ICOUNT) EXIT
END DO

END SUBROUTINE
END MODULE
!———————————————————————–
! Main program calling the snow module
! uses quickwin and pgplot library.
!———————————————————————–

PROGRAM SNOWFORTRAN
!
USE SNOW
USE IFQWIN
!Qwin
INTEGER(4) RESULT

INTEGER(2) numfonts, fontnum
TYPE (qwinfo) winfo
!
INTEGER N,NT
REAL PI,A,B
PARAMETER (NT = 2000)
PARAMETER (PI=3.14159265359)
PARAMETER (A = 2.0*PI/num)
PARAMETER (B = 2.0*PI/NT)

! Variables:
INTEGER I, T, L,ISYM
CHARACTER*8 STR
INTEGER PGBEG
!———————————————————————–

IF (PGBEG(0,’/w9',1,1) .NE. 1) STOP

! Maximize frame window
winfo%TYPE = QWIN\$MAX
RESULT = SETWSIZEQQ(QWIN\$FRAMEWINDOW, winfo)
! Maximize child window
RESULT = SETWSIZEQQ(0, winfo)

CALL PGQINF(‘HARDCOPY’, STR, L)
IF (STR(:L).NE.’NO’) WRITE (*,*) ‘Warning: device is not interactive’

CALL INIT
N=NUM
ISYM=39
CALL PGPAGE
CALL PGVSIZ (-1.5, 10.5, -1.5, 8.0)
CALL PGBBUF
CALL PGSCI(1)

!
CALL PGPT (N, X, Y, ISYM)
CALL PGEBUF
CALL PGSCI(3)
CALL PGSLS(5)
CALL PGSLW (25)
CALL PGSCH(5.0)
CALL PGMTXT(‘B’, -1.7, 1.4, 1.5,’\frHAPPY NEW YEAR’)
CALL PGSCH(1.0)
CALL PGSLW (1)

DO

CALL PGBBUF
CALL PGSCI(0)
CALL MYDELAY(ISPEED)
CALL PGPT (N, X, Y, ISYM)
CALL PGSCI(1)
CALL SNOWING
CALL PGPT (N, X, Y, ISYM)
CALL PGEBUF
END DO

CALL PGEND
END```