Saturday, September 19, 2015

Configure PETSc and Eclipse Parallel on OSX / Linux [C/C++/Fortran]

Nowadays I'm working on my master project which requires use of PETSc framework in c++ language.
Those who develop Finite Element codes know how essential it is to use Sparse matrices and fast solvers to solve FEM's stiff matrix. PETSc does this, it makes it extremely easy to define and use sparse matrices and provides many direct/iterative modern solvers.

The steps below are checked on both Mac OSX Yosemitte and Linux Ubuntu 14.04 LTS. Installation on other linux distributions is the same, just the package names differ (openmpi-devel on redhat vs openmpi-dev on debian).
1- Install openmpi, blas and lapack using your OS package manager. You need to install their devel pacakges if you're using linux.

2- Download latest version of PETSc from their website. I recommend the lite version.

3- Extract the package and enter it.

4- Set these two environment variables in your bash_rc:
PETSC_DIR= /path/to/your/petsc/directory
PETSC_ARCH=debug


5- run ./configure

PETSc will automatically detect your mpi compilers and gives you the next command to compile the whole package.


6- Follow the commands that PETSc gives you until the compilation / testing finish.

7- you compiled the debug version of PETSc. now again, set the PETSC_ARCH environment variable value to:
PETSC_ARCH=release
and run ./configure with no debugging:

./configure --with-debugging=0 COPTFLAGS='-O3' CXXOPTFLAGS='-O3' FOPTFLAGS='-O3'
and again follow PETSc commands to finish compilation.

8- It's now the time to choose an IDE. I myself recommend Eclipse. It's free and lightweight and supports C, C++ and Fortran.
Download the "Eclipse for Parallel Application Developers" from their website.
Eclipse requires Java which can easily get installed from OS package manager.
If you want to program Fortran in Eclipse, you need to download and install the Photran plugin on Eclipse.

9- Openmpi compilers are installed in /usr/local/bin on Mac OSx. You might get the following errors in case the /usr/local/bin path isn't loaded by default in Eclipse:
"/bin/sh: mpicc: command not found"
"/bin/sh: mpic++: command not found"
"/bin/sh: mpif90: command not found"

The fix is simple:
cd /usr/bin
sudo ln -s /usr/local/bin/mpicc
sudo ln -s /usr/local/bin/mpicxx
sudo ln -s /usr/local/bin/mpic++
sudo ln -s /usr/local/bin/mpif90

10- Open Eclipse and create a new MPI C/C++/Fortran project. Check if you can run hello world without any problem.
Now it's time to set eclipse to include PETSc library. (Read PETSc Manual -> OTHER PETSC FEATURES -> Eclipse Users)

Right click on your project -> Properties.
10-1- C/C++ Build -> Environment
Add PETSC_DIR with path to your directory. Then add PETSC_ARCH with value of "debug" for the "Debug" configuration and "release" for the "Release" configuration of Eclipse.
10-2- C/C++ Build -> Settings -> C++ Linker -> Libraries ("Fortran Build -> Settings -> Fortran Linker -> Libraries" in case of Fortran compiler)
Add
petsc
to list of Libraries and
${PETSC_DIR}/${PETSC_ARCH}/lib
to "Library Search Path":

10-3- C/C++ Build -> Settings -> C++ Compiler -> Includes ("Fortran Build -> Settings -> Fortran Compiler -> Directories" in case of Fortran compiler)
Add the following values to Include Paths :
${PETSC_DIR}/include
${PETSC_DIR}/${PETSC_ARCH}/include

10-4 C/C++ Build -> Settings -> C++ Linker ->Miscellaneous ("Fortran Build -> Settings -> Fortran Linker -> Miscellaneous" in case of Fortran compiler)
Add
-Wl,-rpath,"${PETSC_DIR}/${PETSC_ARCH}/lib"
to Linker flags:

10-5- That's all, check if you can run PETSc hello world:

#include <iostream>
#include "petsc.h"
using namespace std;

int main(int argc, char *argv[]) {
    PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);
    PetscPrintf(PETSC_COMM_WORLD,"Hello World\n");
    PetscFinalize();

    return 0;
}

On Mac OSX:


On Ubuntu 14.04 LTS:


10-6- Fortran compiler needs the following extra setting:

10-6-1 Fortran Build -> Settings -> Fortran Compiler -> Miscellaneous
Add "-cpp -dM" flag.



That's all!

Wednesday, October 22, 2014

Amazon Glacier Backup tools on your server | cPanel, Virtualmin, Directadmin, ...

Amazon Glacier is a cost-effective way to store your backup data.
Recently I set up a cPanel server for weekly database backups on glacier. Here are the steps:

First, install glacier-cmd:

yum install git python
cd /usr/src
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
# For cpanel:
ln -s /usr/local/cpanel/3rdparty/bin/git /usr/bin/git
#
git clone https://github.com/uskudnik/amazon-glacier-cmd-interface
cd amazon-glacier-cmd-interface
python setup.py install

 
Open ~/.glacier-cmd and enter your credential details:

[aws]
access_key=YOUR_ACCESS_KEY
secret_key=YOUR_SECRET_KEY

[glacier]
region=us-west-1
bookkeeping=false
bookkeeping-domain-name=backups
logfile=~/.glacier-cmd.log
loglevel=INFO
output=print





Now you can use the following simple script and cronjob to set a weekly backup:
/script/backup:

#!/bin/sh
NOW=$(date +"%d-%m-%Y")
rm -fr /home/backup-tmp
mkdir /home/backup-tmp
mysqldump -A > /home/backup-tmp/backup-all-db-$NOW.sql
gzip /home/backup-tmp/backup-all-db-$NOW.sql
glacier-cmd upload --description "$NOW Backup" VAULT_NAME /home/backup-tmp/backup-all-db-$NOW.sql.gz



Cronjob:

5 8 * * 6       /script/backup







Monday, May 26, 2014

Fortran Tips

Here I've listed some common fortran tips:

-  The form of the information contained in a variable can also be declared implicitly, e.g. IMPLICIT DOUBLE PRECISION (A-H, O-Z) . [Fortran fundamentals] (Read more)
 -  A program may contain only one blank COMMON block but any number of named COMMON blocks. (Read more)

Thursday, April 17, 2014

CFD: Non-uniform grid mesh for 2D Rectangular - Fortran code

Result:

Here's part of the code I used to generate a non-uniform grid using fortran. To understand meaning of exp_x & exp_y powers, you need a piece of paper to sketch and calculate it by yourself.



exp_x=1.05 !x-axis expansion factor
exp_y=1.10 !y-axis expansion factor

a=0.
b=1.
c=0.
d=1.

!***********************************
! Constructing non-uniform grid !
!***********************************
! del_x & del_y : Uniform step size in X & Y direction
! eta_x & eta_y : First non-uniform step size in X & Y direction
! i_th step size is : eta_x*expansion_factor^step
del_x=(b-a)/n_x
del_y=(d-c)/n_y
eta_x=(b-a)*(1-exp_x)/(2*(1-exp_x**(n_x/2)))
eta_y=(d-c)*(1-exp_y)/(2*(1-exp_y**(n_y/2)))

PRINT *, "Uniform X-Step:", del_x
PRINT *, "Uniform Y-Step:", del_y
PRINT *, "First Non-uniform X-Step:", eta_x
PRINT *, "First Non-uniform Y-Step:", eta_y

x(1)=a
y(1)=c
! X_START to Middle_X
DO i=2,n_x/2+1
       x(i)=x(i-1)+eta_x*exp_x**(i-2)
END DO
! Middle_X to End
DO i=n_x/2+2,n_x+1
       x(i)=x(i-1)+eta_x*exp_x**(n_x+1-i)
END DO
! Y_START to Middle_Y
DO j=2,n_y/2+1
       y(j)=y(j-1)+eta_y*exp_y**(j-2)
END DO
! Middle_Y to Y_END
DO j=n_y/2+2,n_y+1
       y(j)=y(j-1)+eta_y*exp_y**(n_y+1-j)
END DO
!***********************************
! non-uniform grid done !
!***********************************
 

Sunday, January 5, 2014

Deactive "sort by subject" of roundcube

In roundcube webmail client when you click on "subject" column, it will sort all your mails by subject! I don't know if it's useful or not but many times occured that I uncounsioucly clicked on this and it took long time to sort all messages by subject! Here's how to deactivate column sorting by subject :
- Open file : program/steps/mail/func.inc
- Search for "./#sort", for me it's around line 443. Then replace :
'onclick' => 'return '.JS_OBJECT_NAME.".command('sort','".$col."',this)
with :
'onclick' => 'return true'

That's it!

Tuesday, October 8, 2013

How to log Script Filename of Emails Sent by PHP Mail() Function to Detect Spam

Today I faced a server that seemed to was abused by some spammers.
I spent 6 hours to find a solution about how to Log mails and mail sender scripts in php...
Finally found that the PHP > 5.3 has the feature itself!!

Just enable "mail.log" in your php configuration and It will log each mail and the script which sends it...

It's also recommended to enable "mail.add_x_header", It'll make it easier to address spam complaints sent  from your servers...

Thursday, August 29, 2013

FSI error with Ansys Fluent or CFX

If you're getting one of the following errors while performing a FSI ( Fluid structure interaction ) simulation using ansys
Fluent coupling error :
 +====================================================================+
 |                      System Coupling Exception                     |
 +====================================================================+
 | Origin            : Transient Structural (Solution)                |
 | Error Code        : 2                                              |
 | Error Description :                                                |
 |    One or more elements have become highly distorted.  Excessive   |
 |   distortion of elements is usually a symptom indicating the need  |
 |   for corrective action elsewhere.  Try ramping the load up        |
 |   instead of step applying the load (KBC,1).                       |
 +====================================================================+

CFX coupling error :
 +--------------------------------------------------------------------+
 | ERROR #001100279 has occurred in subroutine ErrAction.             |
 | Message:                                                           |
 | CFX encountered the error: Read.  Fatal error occurred when reque- |
 | sting Total Mesh Displacement for FSI.                             |
 |                                                                    |
 |                                                                    |
 |                                                                    |
 |                                                                    |
 +--------------------------------------------------------------------+

This may occur if your solid is largely deforming at the very first step under your fluid pressure.
Remember that fluent has an operation pressure, cfx has a reference pressure too. All pressures you define are relative to this reference pressure. So it may cause a pressure increase in your model and cause a huge displacement resulting the above error.
Another reason might be if you're defining velocity inlet in wrong pressure. For example consider a pipe with X axis. Now if you wrongly define the velocity inlet toward Y or Z instead of X axis, the reduction in velocity at wall locations will cause a high pressure increase and same story.

Mos

How to Stream RTSP / Webcam / IP Camera Over the Web

I had a Hikvision IPC-B120 that provides a simple RTSP stream, which I could view in VLC. I also wanted to see my Logitech BRIO’s feed in a ...