Friday 22 October 2010

Metaprogramming Ruby

I explained here (in greek) a bit about metaprogramming and here about an application in a python/c++ finite element code but this book is everything you need about metaprogramming in Ruby. Written by Paolo Perrotta in a way that two colleagues discuss and solve every day problems. The book is full of example code and assumes some basic knowledge of the Ruby language. The first part of the book deals with general metaprogramming concepts in Ruby from objects and methods to blocks and classes to finally code that writes code. The second part dives in the metaprogramming techniques of Rails, with ActiveRecods being the most important part. The last chapter deals how to use metaprogramming safely. As Yukihiro "matz" Matsumoto says in the foreword "It's a bit like magic", but be ware because there is white magic and black magic.

Sunday 17 October 2010

TeX code generation with perl

A couple of months ago I started to learn some basic perl, because perl is one of the programming languages that one should know. It can be used from text processing and web applications to system administration and many other applications. So I came with the idea to write something simple in order to automate some TeX code generation done by some other part of my program. So I wrote some basic perl functions that can be found here:

#!/usr/bin/env perl

sub initialize{
$text = '\documentclass{article}'."\n";
$text .= '% Created by '."\n";
$text .= '\begin{document}'."\n";
}

sub usepackage{
$text = '\usepackage{'.$_[0]."}\n";
}

sub finalize{
$text = '\end{document}'."\n";
}

sub itemize{
$text = '\begin{itemize}'."\n";
$text .= "\t\item"."\n";
$text .= '\end{itemize}'."\n";
}

sub enumerate{
$text = '\begin{enumerate}'."\n";
$text .= "\t\item"."\n";
$text .= '\end{enumerate}'."\n";
}

sub figure{
$text = '\begin{figure}'."\n";
$text .= "\t".'\includegraphics{}'."\n";
$text .= '\end{figure}'."\n";
}

sub table{
$text = '\begin{tabular}'."\n";
#$text = 'table';
$text .= '\end{tabular}'."\n";
}

sub tabbing{
$a = '\begin{tabbing}'."\n";
$a .= '\end{tabbing}'."\n";
}

sub section{
$a = '\section{'.$_[0]."}\n";
}

sub subsection{
$a = '\subsection{'.$_[0]."}\n";
}

sub equation{
$a = '\begin{equation}'."\n";
$a .= $_[0];
$a .= '\end{equation}'."\n";
}

And a simple example is given below:

print &initialize;
print &usepackage("graphicx");

print &itemize;
print &enumerate;
print &figure;
print &table;

print &finalize;

Friday 8 October 2010

Book Review: Foundations of GTK+ Development

Foundations of GTK+ Development is a book about the development of GUI applications using the open source library GTK+. It assumes knowledge of the C language and explains first the structure of the GTK+ library, i.e. GLib, GObject, GDK, GdkPixbuf, Pango, and ATK. By building a simple "Hello World" application explains the basics of GTK+, the main loop function, signals and callbacks, events, and widgets. Then the author explains with many examples and code listings the use of container widgets, basic widgets, and dialogs.

The use of the GLib library, the text view widget, the tree view widget, and of course the generation of menus and toolbars are then described in detail again with examples and source code. Finally more advanced topics like dynamic user interfaces, creating custom widgets, and some additional widgets, like canvas, cairo, calendar among others, are discussed. The book concludes with a chapter with five full working applications and a lot of appendices.

Wednesday 6 October 2010

xcf2pdf

xcf2pdf is a simple python script, that uses convert from ImageMagick to batch convert every xcf image inside a folder to a pdf


#!/usr/bin/python


from subprocess import *

import re


def convert(file):

p = Popen(['convert', file, file[:-3]+'pdf'], stdout=PIPE, stderr=PIPE)


p = Popen('ls', stdout=PIPE, stderr=PIPE)

ls, error = p.communicate()

files = re.split('\n', ls)

for i in files:

if i[-3:]=='xcf':

convert(i)


There are a few features missing and known bugs, e.g. it will convert every layer of the xcf file to a different page in the same pdf. (Beware of the python code formatting)

GGGears

In many engineering applications engineers have to calculate how gears are working under stress. GGGears is a free software that does exactly this by means of the finite element package GETFEM++ and the mesh generation package GMSH, both also free software. It supports both 2D and 3D mesh generation and solver. The user does not need to know anything about finite elements or mesh generation; he/she just need to insert the geometrical data of the gear systmem.

The installation of the software can be easily done in Ubuntu by first adding the repository

sudo add-apt-repository ppa:gggears/main
sudo apt-get update

and then installing gggears and its dependencies by
sudo apt-get install gggears

Metaprogramming and finite elements

The idea of metaprogramming is great; you write code that writes code that writes code... If I am not wrong the first language that introduced this concept was LISP (at the moment I am learning the Scheme dialect) So, the idea behind metaprogramming is to write a program in a high level language which will then create source code in a lower language (or even the same) in order to automate some procedure. From Lisp many languages have been influenced, such as Ruby and python, that also incorporate metaprogramming aspects.

One day someone said why don't we apply this concept to finite element code as well. Finite element code can be really annoying (as every code is) so by writing some code that writes finite element code would make our calculations easier. The result of this idea is the FEnics project. Thank you for the great job guys!!! Finite elements are usually used to solve partial differential equations. So find a PDE you like and just solve it.

The program can be easily installed in Ubuntu by writing
sudo apt-get install fenics
Of course there are many other packages for many distributions. Have fun!