\documentclass[a4wide,11pt]{article}
\usepackage{verbatim}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{epic}
\usepackage{eepic}
\usepackage{a4wide}
\usepackage{color}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[dvips]{epsfig}
\usepackage{psfig}
\usepackage[T1]{fontenc}
\usepackage{cite} % [2,3,4] --> [2--4]
\usepackage{shadow}
\usepackage{hyperref}

\lstset{language=c++}
\lstset{alsolanguage=[90]Fortran}
\lstset{basicstyle=\small}
\lstset{backgroundcolor=\color{white}}
\lstset{frame=single}
\lstset{stringstyle=\ttfamily}
\lstset{keywordstyle=\color{red}\bfseries}
\lstset{commentstyle=\itshape\color{blue}}
\lstset{showspaces=false}
\lstset{showstringspaces=false}
\lstset{showtabs=false}
\lstset{breaklines}

\begin{document}
\section*{Introduction to numerical projects}

Here follows a brief recipe and recommendation on how to write a report for each
project.
\begin{itemize}
\item Give a short description of the nature of the problem and the eventual 
numerical methods you have used.
\item Describe the algorithm you have used and/or developed. Here you may find it convenient
to use pseudocoding. In many cases you can describe the algorithm
in the program itself.

\item Include the source code of your program. Comment your program properly.
\item If possible, try to find analytic solutions, or known limits
in order to test your program when developing the code.
\item Include your results either in figure form or in a table. Remember to
       label your results. All tables and figures should have relevant captions
       and labels on the axes.
\item Try to evaluate the reliabilty and numerical stability/precision
of your results. If possible, include a qualitative and/or quantitative
discussion of the numerical stability, eventual loss of precision etc. 

\item Try to give an interpretation of you results in your answers to 
the problems.
\item Critique: if possible include your comments and reflections about the 
exercise, whether you felt you learnt something, ideas for improvements and 
other thoughts you've made when solving the exercise.
We wish to keep this course at the interactive level and your comments can help
us improve it.
\item Try to establish a practice where you log your work at the 
computerlab. You may find such a logbook very handy at later stages
in your work, especially when you don't properly remember 
what a previous test version 
of your program did. Here you could also record 
the time spent on solving the exercise, various algorithms you may have tested
or other topics which you feel worthy of mentioning.
\end{itemize}



\section*{Format for electronic delivery of report and programs}
%
The preferred format for the report is a PDF file. You can also
use DOC or postscript formats. 
As programming language we prefer that you choose between C/C++ and Fortran90/95.
You could also use Java or Python as programming languages. 
Matlab/Maple/Mathematica/IDL are not allowed as programming
languages for the handins, but you can use them to check your results where possible.
The following prescription should be followed when preparing the report:
\begin{itemize}
\item Use Classfronter to hand in your projects, log in  at 
blyant.uio.no and choose 'fellesrom fys3150 og fys4150'.
Thereafter you will see an icon to the left with 'hand in' or 'innlevering'.
Click on that icon and go to the given project. 
There you can load up the files within the deadline.
\item Upload {\bf only} the report file and the source code file(s) you have developed.
The report file should include all of your discussions and a list of the codes you have developed. 
Do not include library files which are available at the course homepage, unless you have
made specific changes to them.
\item Comments  from us on your projects, approval or not, corrections to be made 
etc can be found under
your Classfronter domain and are only visible to you and the teachers of the course.
 
\end{itemize}
Finally, 
we do prefer that you work two and two together. Optimal working groups consist of 
2-3 students. You can then hand in a common report. 

\section*{Project 3, Simulation of Metropolis algorithm, deadline  23 october  12am (midnight)}
In this project the aim is to show that the Metropolis algorithm
generates the Boltzmann distribution
\[
   P(\beta)=\frac{e^{-\beta E}}{Z},
\]
with $\beta=1/kT$ being the inverse temperature, $E$ is the energy of
the system and
$Z$ is the partition function. The only functions you will need are those
to generate random numbers.

We are going to study one single particle in equilibrium with 
its surroundings, the latter modelled via a large heat bath
with temperature $T$.

The model used to describe this particle is that of an ideal gas
in {\bf one} dimension and with velocity $-v$ or $v$. 
We are interested in finding  $P(v)dv$, which expresses the probability
for finding the system with a given velocity $v\in [v,v+dv]$.
The energy for this one-dimensional system is
\[
  E=\frac{1}{2}kT=\frac{1}{2}v^2,
\]
with mass $m=1$.
In order to simulate the Boltzmann distribution, your program
should contain the following ingredients:  
\begin{itemize}
\item Reads in the temperature $T$, the number of Monte Carlo cycles, 
and the initial velocity. You should also read in 
the change in velocity $\delta v$ used in every Monte Carlo step. 
Let the temperature have dimension energy.
\item Thereafter you choose a maximum velocity given by for example  
$v_{max}\sim 10\sqrt{T}$. Then you construct a velocity interval 
defined by  $v_{max}$ and divide it in small intervals through 
$v_{max}/N$,
with $N\sim 100-1000$. 
For each of these intervals your task is to find out how many times
a given velocity during the Monte Carlo sampling appears 
in each specific interval.
\item The number of times a given velocity appears in a specific
interval is used to construct a histogram representing 
$P(v)dv$. To achieve this you should construct a vector 
$P[N]$ which contains the number of times a given velocity 
appears in the subinterval 
$v,v+dv$. 
\end{itemize}

In order to find the number of velocities appearing in each interval
we will employ the Metropolis algorithm. A pseudocode for this is
\begin{lstlisting}
   for( montecarlo_cycles=1; Max_cycles; montecarlo_cycles++) {
      ...
      // change speed as function of delta v
      v_change = (2*ran1(&idum) -1 )* delta_v;
      v_new = v_old+v_change;
      // energy change
      delta_E = 0.5*(v_new*v_new - v_old*v_old) ;
      ......
      // Metropolis algorithm begins here
        if ( ran1(&idum) <= exp(-beta*delta_E)  ) {
            accept_step = accept_step + 1 ;      
            v_old = v_new ;
            .....
        }
      // thereafter we must fill in  P[N] as a function of
      // the new speed
        P[?] = ...

      // upgrade mean velocity, energy and variance
         ...
      }
\end{lstlisting}


\begin{enumerate}
\item[a)] Make your own algorithm which sets up the histogram
$P(v)dv$, find mean velocity, energy, energy variance and the number of
accepted steps for a given temperature. Study the change of the number of
accepted moves as a function of $\delta v$.
Compare the final energy with the analytic result
$E=kT/2$ for one dimension.
Use $T=4$ and set the intial velocity to zero, i.e., $v_0=0$. 
Try different values of $\delta v$.
Check the final result for the energy as a function 
of the number of Monte Carlo cycles.
\item[b)] Make thereafter a plot of  $ln(P(v))$ as function of $E$
and see if you get a straight line. Comment the result.

\end{enumerate}


\end{document}









