Random programming stuff

I just needed a space to put small programming tricks on a daily basis. I don't think it would ever interest anyone but maybe you may have googled certain terms discussed here.

May 18, 2016 Setting up SSH keys

Probably there's no need to explain what SSH key is, but I just forget how to do it every now and then. Here's what to do:

1. Smash ssh-keygen -t rsa

2. Copy ~/.ssh/id_rsa.pub to ~/.ssh/authorized_keys on your server

That's it.

May 1, 2016 Automatic Werewolf distributor

If you have played Werewolf, you have certainly experienced a situation, where people (intentionally or not intentionally) sees your card, or the card itself isn't in good condition and others can easily see what you are. For these reasons, I decided to write a program that determines the personalities automatically and sends them by SMS. The entire code was written in AppleScript. You are very welcome to copy it.

 #!/usr/bin/osascript 
 set Yasin to "+33XXXXXXXXX" 
 set Mina to "+33XXXXXXXXX" 
 set Cintia to "+33XXXXXXXXX" 
 set Elena to "+33XXXXXXXXX" 
 set Adrian to "+33XXXXXXXXX" 
 set Mkrtych to "+33XXXXXXXXX" 
 set Daniele to "+33XXXXXXXXX" 
 set Nosley to "+33XXXXXXXXX" 
 set Veronique to "+33XXXXXXXXX" 
 set Nathalie to "+33XXXXXXXXX" 
 set Andrea to "+33XXXXXXXXX" 
 set Brian to "+33XXXXXXXXX" 
  
 set Wolf to "Wolf: when called by the moderator in the night, you choose a person to kill together with other wolves" 
 set Villager to "Villager" 
 set Fortuneteller to "Fortune teller: when called by the moderator, you can indicate one person. The moderator tells you whether he/she is a wolf or an innocent" 
 set Witch to "Witch: you have one portion of life and one portion of death. Each night, the moderator wakes you up and indicates the person killed by wolves. You may save this person with your portion of life (may also be yourself). You can also use your portion of death to kill another person." 
 set Saver to "Saver: each night, you choose a person. This person shall not be killed during the night. However, you cannot choose the same person in a row." 
 set Idiot to "Idiot: If you're killed during the day, you will still continue the game with others, though you are not allowed to vote anymore." 
 set Cupid to "Cupid: At the beginning of the game, you choose two lovers. If one of the lovers die, the other one shall die, too." 
  
 set Playerlist to {Yasin, Elena, Mina, Andrea, Veronique, Brian, Adrian, Cintia, Mkrtych, Nathalie} 
 set Keyword to "First: " 
 set Rolelist to {Wolf, Wolf, Witch, Saver, Cupid, Fortuneteller, Idiot} 
 set num_player to count of Playerlist 
  
 set Player_num to random number from 1 to num_player 
 set Role_num to random number from 1 to num_player 
 set Player_incr to some item of {"13", "19", "29", "37", "43", "53", "61", "71", "79", "89", "101", "107", "113", "131", "139", "151", "163", "173", "181", "193", "199", "223", "229", "239", "251", "263", "271", "281", "293", "311", "317", "337", "349", "359", "373", "383", "397", "409", "421", "433", "443", "457", "463", "479", "491", "503", "521", "541", "557", "569", "577", "593", "601", "613", "619", "641", "647", "659", "673", "683", "701", "719", "733", "743", "757", "769", "787", "809", "821", "827", "839", "857", "863", "881", "887", "911", "929", "941", "953", "971", "983", "997"} 
 set Role_incr to some item of {"11", "17", "23", "31", "41", "47", "59", "67", "73", "83", "97", "103", "109", "127", "137", "149", "157", "167", "179", "191", "197", "211", "227", "233", "241", "257", "269", "277", "283", "307", "313", "331", "347", "353", "367", "379", "389", "401", "419", "431", "439", "449", "461", "467", "487", "499", "509", "523", "547", "563", "571", "587", "599", "607", "617", "631", "643", "653", "661", "677", "691", "709", "727", "739", "751", "761", "773", "797", "811", "823", "829", "853", "859", "877", "883", "907", "919", "937", "947", "967", "977", "991"} 
  
 repeat num_player times 
 	set Player_num to ((Player_num-1+Player_incr) mod num_player) + 1 
 	set Role_num to ((Role_num-1+Role_incr) mod num_player) + 1 
 	set Player to item Player_num of Playerlist 
 	if (Role_num > count of Rolelist) then 
 		set Role to Villager 
 	else 
 		set Role to item Role_num of Rolelist 
 	end if 
 	log Player & ": " & Role 
 	tell application "Messages" 
 		send Keyword & Role to (buddy 1 whose handle is Player) 
 	end tell 
 end repeat 

September 8, 2015 Calculating Euler Angles

I had to determine the crystal orientation of a fcc metal using the Euler angles. Here's how it works (I'm going to use v,w instead of X,Y just to make it easier to understand (with w=(wx,wy,wz) and v=(vx,vy,vz))

1. Determine the vector giving the maximum value in the direction of x (definition of v).

2. Find the vector w perpendicular to v with a positive value in the direction of z

3. Calculate N=(-wy,wx,0)

4. Calculate the angles: $$\cos(\alpha)=\frac{w_y}{\sqrt{w_x^2+w_y^2}}$$$$\cos(\beta)=\frac{w_z}{\sqrt{w_x^2+w_y^2+w_z^2}}$$$$\cos(\gamma)=\frac{v_xw_y-v_yw_x}{\sqrt{v_x^2+v_y^2+v_z^2}\sqrt{w_x^2+w_y^2}}$$

August 27, 2015 Installing LAMMPS as library

Today, I installed LAMMPS on Blue Gene (and was incredibly easy!)

1. Download LAMMPS (and untar)

2. Tap module load xl/mpi

3. Go to src and tap make mpi (If you have to install special libraries, like REPLICA in my case, you must do it before make mpi)

4. If no error (which should be the case) tap make clean-all (maybe not needed) and then make mpi mode=lib. This will create a static library liblammps_mpi.a (check if it was correctly done by for example ls -lt | head. It should be the first item).

5. Let's call the C++ code main.cpp and the path to LAMMPS folder PATH_TO_LAMMPS. Then you just have to go to the folder containing main.cpp and compile it by mpic++ -I PATH_TO_LAMMPS/src -L PATH_TO_LAMMPS/src main.cpp -l lammps

Easy, isn't it?

July 11, 2015 Object oriented Javascript

I created the figure for MPI_Allgatherv with Pages, but I wanted to know whether it's possible to make it in a simple way with object oriented javascript.

Your browser does not support the HTML5 canvas tag.

Okay, here are two big problems: I don't know how to use italic and above all, the text is quite blurry (the shadow behind each component is intentional). However, neat that it's possible to draw everything within javascript. :)

July 10, 2015 MPI_Allgatherv

I don't know how often I wondered how to use MPI_Allgather with variable spaces. Following code will work ONLY with 4 threads. I'm sure I'm not the only one person who doesn't understand its functionality and had to look for a solution in the internet for quite a while...

#include<iostream>
#include<mpi.h>

using namespace std;

int main(int arg, char** name){
	MPI_Init(&arg, &name);
	int me,nproc;
	MPI_Comm_size(MPI_COMM_WORLD, &nproc);
	MPI_Comm_rank(MPI_COMM_WORLD, &me);
	int * x = new int [10];
	for(int i=0; i<10; i++)
		x[i]=me+1;
	int * d = new int [nproc];
	int * c = new int [nproc];
	for(int i=0; i<nproc; i++)
		c[i] = i+1;
	d[0]=0;
	for(int i=1; i<nproc; i++)
		d[i] = d[i-1]+c[i-1];
	MPI_Allgatherv(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, &(x[0]), c, d, MPI_INT, MPI_COMM_WORLD);
	if(me==0)
		for(int i=0; i<10; i++)
			cout<<i<<" "<<x[i]<<endl;
	MPI_Finalize();
}

You must compile & execute it by

mpic++ main.cpp
mpirun -np 4 a.out

The output will be:

0 1
1 2
2 2
3 3
4 3
5 3
6 4
7 4
8 4
9 4

... and of course you can do the same thing with MPI_Gatherv.