22.00j Tutorial: Introduction to Athena, MATLAB, and C/C++.


Overview of required skills

Many of the problem sets in this class involve reading and modifying codes in both C/C++ and FORTRAN, but some only involve using codes which are given to you. The semester projects often involve a moderate amount of programming. However, note that the programs are numerical codes and involve very simple data structures. With the following skills in C/C++, FORTRAN, and MATLAB, you should be able to tackle the problem sets. We will teach you most of these constructs in the next few tutorials.

Useful skills
Note that if you are going to use MATLAB, you should be very comfortable using vectors and vector functions. If you are not, your scripts will probably be too slow to be very useful.

Introduction to Athena

Before you can program, you need to know how to edit files, compile, and organize your files. We covered the following topics in the tutorial:

Basic Athena Topics

Directory navigation and file movement

These commands are covered in great detail on Working on Athena. Make sure at the very least to read about the following commands: Note that you may learn about many of these commands by typing man <command name> at the command prompt. The redirection operators: <, > are discussed in the man page for tcsh or bash, depending on your shell (probably tcsh). Here is a sample session using most of the above commands:
no-knife% mkdir some_directory
no-knife% cd some_directory
no-knife% ls
no-knife% mkdir second
no-knife% cd second
no-knife% ls
no-knife% ls -a
./   ../
no-knife% cd ..
no-knife% ls
second/
no-knife% echo hello > text.txt
no-knife% more text.txt
hello
no-knife% cp text.txt text2.txt
no-knife% cat text2.txt
hello
no-knife% cd second/
no-knife% pwd
/afs/athena.mit.edu/user/d/i/dion/some_directory/second
no-knife% cd ..
no-knife% pwd
/afs/athena.mit.edu/user/d/i/dion/some_directory
no-knife% mv text.txt new_name.txt
no-knife% ls
new_name.txt  second/       text2.txt     
no-knife% mv text2.txt second
no-knife% cd second
no-knife% ls
text2.txt  
no-knife% ls ..
..:
new_name.txt  second/       
no-knife% rm text2.txt 
no-knife% cd ..
no-knife% rmdir second
no-knife% ls
new_name.txt  

Creating and editing a text file

This is covered in somewhat more detail in Emacs on Athena. To create or open a text file type emacs and then the file you want to create or open. Editing is fairly straight forward. Some basic commands are listed below.

Getting Emacs to color-code program files.

During the tutorial, my emacs colored some files. To get a similar result for your files, make a directory called el in your home directory and put the following files there:

Then add the following lines to a file called .emacs in your home directory:

; add custom directory for new el files

(setq load-path (cons "~/el" load-path))

;; Matlab options

(autoload 'matlab-mode "matlab" "Enter Matlab mode." t)
(setq auto-mode-alist (cons '("\\.m\\'" . matlab-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("pico\\.[0-9]*\\'" . text-mode) 
			    auto-mode-alist))
(autoload 'matlab-shell "matlab" "Interactive Matlab mode." t)

; add font-lock to hooks (coloring)

(add-hook 'c-mode-hook 'turn-on-font-lock)
(add-hook 'makefile-mode-hook 'turn-on-font-lock)
(add-hook 'c++-mode-hook 'turn-on-font-lock)
(add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
(add-hook 'matlab-mode-hook 'turn-on-font-lock)
(add-hook 'java-mode-hook 'turn-on-font-lock)
(add-hook 'sgml-mode-hook 'turn-on-font-lock)

; custom functions
(global-set-key "\C-cg" 'goto-line)
The last two lines let you type Ctrl-c g to go to a line in a file. I find this useful when programming.

Compiling a simple (one file) C or FORTRAN program.

There is extensive documentation for the gnu compilers if you use the info documentation system. To do so, type the following commands at the athena prompt:

athena% add gnu; add emacs
Now if you want the C/C++ compiler documentation, type info gcc. If you want the FORTRAN compiler documentation, type info g77. For help using the info documentation system, type info info.

To compile a single file C file (.c suffix), type

athena% gcc -o <application name> <file name>
If you use math functions (rounding, sin, cosine, etc.), you must append -lm to link your program with the math library.

To compile a C++ file (.cc or .C suffix) or a C file type

athena% g++ -o <application name> <file name>
Note that you may include -O3 before the -o in order to have the compiler aggressively optimize your program. It will take longer to compile, but your program will sometimes run much faster.

Introduction to MATLAB

We played around a little with MATLAB much in the style of a demonstration. To use MATLAB on Athena, type add matlab then matlab &. This will boot up the program. It may take awhile!

For some useful MATLAB demos, you can type demo in the MATLAB window. For help, type helpdesk. There are many books on MATLAB, and some introductory primers on the web. For example: MATLAB Primer. This is also published as a hard-copy book, which may be found on Amazon.

Although I will not include a full transcript of our demonstration here, I will provide much of what we went over. I assume the session is started in a directory called "some_directory" with the following files:

The :-operator

We started the session discussing the :-operator. Here is the sample code:

>> a_vector = 1:5

a_vector =

     1     2     3     4     5

>> a_vector = 1.5:5.5

a_vector =

    1.5000    2.5000    3.5000    4.5000    5.5000

>> a_vector = 1:5:20

a_vector =

     1     6    11    16

>> a_vector = -1:-5:-18

a_vector =

    -1    -6   -11   -16

>> a_vector = -1:-5:10 

a_vector =

   Empty matrix: 1-by-0

As you can see, a statement of the form <start>:<increment>:<end> produces a row vector with numbers starting at <start> incrementing with steps of size <increment>, and stopping just before going past <end> if the increment sign goes towards from the start to end. Otherwise the expression produces an empty matrix. If <increment> is omitted, it is taken as 1.

Plotting and ;

If you end a statement with ";", matlab won't print anything out. This is useful when working with very large vectors and matrices. The following code produces two plots:

>> x=0:0.01:1;
>> sin([1 2 3])

ans =

    0.8415    0.9093    0.1411

>> plot(x,sin(x))
>> plot(x*pi,sin(x*pi))       
Here are the plots:

Matrix multiplication and running scripts

>> y=x';
>> size(y)

ans =

   101     1

>> size(x)

ans =

     1   101

>> x*y

ans =

   33.8350

>> pwd

ans =

/afs/athena.mit.edu/user/d/i/dion/some_directory

>> test1
>> a

a =

     1     2     3
     4     5     6

>> h

h =

     1     1
     2     4
     3     9
     4    16
     5    25
     6    36
     7    49
     8    64
     9    81
    10   100
Finally, "plot(h(:,1),h(:,2))" plots the first column of h vs the second column. This is using the colon operator for selecting the entire range of the an index of a matrix.

Loading raw data

This is straight forward. To load a text file containing columns of numbers separated by spaces, just type "load <filename>". This loads the data into a variable with the same name as the first letters of the file up to the first period. You can also save the data into a variable of your own choosing (see below).

>> load('data.txt')
>> data

data =

     1     2     3
     4     5     6
     7     8     9

>> my_data = load('data.txt')

my_data =

     1     2     3
     4     5     6
     7     8     9

Logic and loops in MATLAB

See the MATLAB Primer for reference.

Introduction to C

We looked at making a simple C program and some basic constructs. Most of these may be learned from a careful perusal of either the file we produced or monty_hall.c. Additional information may be found in an introductory textbook on C or C++. I don't know much about such things, but one can find such books on Amazon. Here is one example, and may lead you to others. I thought Quantum had some good intro books on C, but apparently this is not the case. Note that intro books on C++ such as this may also be useful, but they teach you a lot of tools which are not so useful for our class: classes, inheritance, and polymorphism. However, C++ has some slight syntactic differences which make it easier (for me) to program.