fixed all the bugs, the result is the same with Nikolay's.
fixed all the bugs in self-consistent loop;
generate the same result with Nikolay's code