并行MPI程序(C语言和Fortran的一些区别)

最近尝试回顾一下C语言,但是由于习惯了Fortran,导致了一些错误。

以下列举一些(也许这些错误很简单,不过也费了我不少时间找):

1.用C语言调用MPI函数的时候必须区分大小写。比如MPI_Reduce如果写成mpi_reduce或者MPI_REDUCE的话,即使编译会通过,运行时候也会出现Segmentation Fault。这对初学者来说是很大一个陷阱。

2.用C语言的时候,若在main函数中初始化了MPI,然后在自定义的一个函数中使用变量“MPI_COMM_WORLD”的话,会导致错误。必须将MPI_COMM_WORLD当成一个参数传给自定义的函数,否则自定义函数不知道MPI_COMM_WORLD是什么。

3.用MPI_Bcast传输一个2D数组。注意Fortran语言中的2D数组是存在于一个连续内存区域,但是C语言的2D数组不是。见这个帖子。

初次使用Git 笔记

我参考了以下几个网站的内容:

1. Pro Git book (http://git-scm.com/book/zh/v1), 这本书写得深入浅出,翻译得很好,看完前两章基本上就入门了;

2.http://lugir.com/git-basic.html, 我参考了博主写的链接github repository的方法,很简洁.

3.https://help.github.com/articles/dealing-with-non-fast-forward-errors/ (在使用CCR来push本地文件到remote目录时遇到相关问题,这个网页帮助很大)。

假设已经装好了git的程序。在debian环境下直接用“ sudo apt-get install git “来安装。

现在写一下git和github基本的使用流程(默认在LINUX或MAC环境下):

1.首先,进入你的项目目录,或者随便创建一个新目录叫 “./project”。

mkdir project
cd project
git init #创建一个新的git目录,用以存储相关信息

2.现在需要设置一下git的属性,如用户名和email:

git config --global user.name "Cooper"
git config --global user.email "cooper@gmail.com"

3.现在可以在当前目录添加一些你想要的文件,并把当前文件夹加入到当前的git仓库中:

touch README
touch first.cpp
touch second.cpp
git add . #将当前文件夹所有文件添加到git的跟踪状态,被跟踪(track)的文件如果有任何变动,git程序都会知道
git commit -m "First commit!" #提交所有文件

注:如果你想忽略某些特定类型的文件,比如以 .o, .mod和 .h为后缀名的,那么你可以在当前目录创建一个”.gitignore”文档来忽略这些文件。
4.将当前git档案保存的文件上传到github的服务器中:

git remote add origin git@github.com:cooper/repository.git #添加远程服务器上的git目录,如果无法通过ssh添加的话,可以采用https添加。使用https的缺点是需要每次输入密码。
git pull origin master #将github的目录和当前git目录合并
git push origin master #将本地的文件(README,first.cpp,second.cpp)传到远程git目录

注:
1.当你想看git的状态时,可以使用命令: git status
2.如果想看git的一些环境变量,可以用: git config -l 或 git config --list
 

awk使用小记

awk命令使用基本格式: awk ‘条件1{操作1}条件2{操作2}…’ [文件名]。
1.基本变量:
NF:每一行($0)的总的字段数
NR:当前行数
FS:当前分隔符,默认是空格
BEGIN:开始
END:结束
注意,如果要使用shell的变量,用:”‘$variable'”。

附处理sigma.log*的脚本:

for OUTPUT in $(ls sigma.log*)
do
if [ $OUTPUT != "sigma.log" ]; then
awk 'BEGIN{ORS="";print "'$OUTPUT'","\t"}
NR==64{print 56,$10," "}
NR==65{print 57,$10,"\n"}' $OUTPUT |
sed 's/sigma.log/ /g'
fi
done |
sort -k1 -n

2.如果要在AWK中使用shell里的变量,可以用:”‘$变量名'”。

比如从一些输出文件”test-run-nprocs-proc.out”提取数据的脚本,在awk中用”‘$nprocs'”使用for循环的变量nprocs。


for nprocs in 4 6 8 12 16
do
filename="test-run-"
filename+=$nprocs
filename+="proc.out"
# echo $filename
echo " "
grep 'timing' $filename | awk 'NR%4==2 { serialtime = $6 }
NR%4==3 { size = $10; paratime = $6}
NR%4==0 { precondtime = $9;
awknproc="'$nprocs'";
printf "%s %10d %12.7f %12.7f %12.7f \n",awknproc,size,serialtime,paratime,precondtime }'
done

sed命令笔记

直接修改文件:-i

1. 修改所有行:

sed ‘s/oldstring/newstring/g’ [filename]

2.修改第4~10行:

sed ‘4,10s/oldstring/newstring/g’ [filename]

3.删除:

sed ‘1,3d’ [filename] (删除1到3行)

sed ‘$d’ [filename] (删除尾行)

sed ‘/^th/d’ [filename] (删除开头为th的行)

sed ‘/^$/d’ [filename] (删除空行)

4.多次编辑:

sed -e [action1] -e [action2] -e [action3] … [filename]

Some notes on dielectric functions 1

1. Why does metal reflect light ?

A simple reason is that there are lots of free electrons in metal. The light incident on the metal is electric field which drive the free electrons to oscillate in the same frequency with the light.

We start from drude model.

According to the simplest Drude model, the motion of the electron is governed by the external electric field E, and the probability of electron scattering within unit time, denoted as \gamma, which is usually within infrared frequency range.

Then the equation of motion of electron is then written in semi classical way:

m_e \frac{ d^2 x }{d t^2} = -e E - m_e \gamma \frac{d x}{d t}, where x is the position of electron. Consider electric field changing in certain frequency \omega: E = E_0 e^{i \omega t}, then we can use ansatz: x = x_0 (\omega) e^{i \omega t }. Insert the ansatz into equation: x_0 = \frac{eE_0}{\omega m_e (-i\gamma+\omega)}.

Remember the polarization density P is given by: P(\omega) = n e x(\omega), where n is the number density of free electrons in material, so

P(\omega) = n \cdot (-e) \cdot x_0 e^{i \omega t} = \frac{ne^2}{\omega m_e (\omega-i\gamma)} E(\omega)

and :

D(\omega) = E(\omega) + 4\pi P(\omega) = (1- \frac{4\pi ne^2}{\omega m_e (\omega-i\gamma)}) E(\omega) (Pay attention that E and P has the same unit!!).

Define the plasma frequency: \omega_P^2 = \frac{4\pi ne^2}{\omega m_e}. Then:

D(\omega) = (1- \frac{\omega_P^2}{\omega(\omega-i\gamma)}) E(\omega).

Now, we have the expression for the dielectric function for free electrons:

\epsilon(\omega) = 1 - \frac{\omega_P^2}{\omega(\omega-i\gamma)}

(NOTE: THIS EXPRESSION IS ONLY FOR FREE ELECTRONS, THE SCATTERING PROCESSES ARE DESCRIBED BY \gamma.)

Here are some particular magnitude of n for different types of materials:

Metal: 10^22 ~ 10^23 cm^-3 (Potassium, Sodium, Copper) –> \omega_P is on the order of 10^16 Hz, which is within ultraviolet range

Semimetals: 10^17~10^20 cm^-3 ( Arsenic, Antimony, Bismuth, Graphite) –> \omega_P is around 10^14 Hz ( between infrared and Vis.)

Semiconductor: 10^13~10^15 cm^-3.

The dielectric function can rewritten as:

\epsilon(\omega) = 1 - \frac{\omega_P^2}{\omega^2+\gamma^2} - i \frac{\omega_P^2 \gamma}{\omega(\omega^2+\gamma^2)} .

(NOTE: if I assume the time dependence is e^{-i\omega t}, then it becomes: \epsilon(\omega) = 1 - \frac{\omega_P^2}{\omega^2+\gamma^2} + i \frac{\omega_P^2 \gamma}{\omega(\omega^2+\gamma^2)} . )

Now let’s consider visible light incident on metals.

For visible light, \omega > 10 \gamma. In this situation, we can neglect the imaginary part of \epsilon(\omega), and the real part \epsilon_r(\omega) = 1- \frac{\omega_P^2}{\omega^2+\gamma^2} is negative, because the plasma frequency is in the ultraviolet range, which has higher frequency than \omega.

Remember \epsilon(\omega) = ( \hat{n} )^2, where \hat{n} = N+iK is refractive index. When \epsilon is almost negative real number, we have N \sim 0.

In this case, the reflectance: \mathscr{R} = |(\hat{n}-1)/(\hat{n}+1|^2 = \frac{(N-1)^2+K^2}{(N+1)^2+K^2} \sim 1, which means the light is reflected back completely.