- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在编写二维 Ising 模型的模拟。除了一件事外,该模型的行为与预测的一样:临界温度大约为 3.5,而它应该接近 2/ln(2 + sqrt (2))
。
该项目是一个生成数据的C++程序,以及一个执行该程序的shell脚本。完整代码可见here .这里还有 lattice.cpp
#include <iostream>
#include "include/lattice.h"
using namespace std;
/*
Copy assignment operator, too long to include in the header.
*/
lattice &lattice::operator=(const lattice &other) {
size_ = other.size_;
spins_ = other.spins_;
J_ = other.J_;
H_ = other.H_;
delete spins_;
return *this;
}
void lattice::print() {
unsigned int area = size_ * size_;
for (unsigned int i = 0; i < area; i++) {
cout << to_symbol(spins_->at(i));
if (i % size_ == size_ - 1)
cout << endl;
}
cout << endl;
}
/*
Computes the energy associated with a spin at the given point.
It is explicitly float as that would allow the compiler to make use of multiple
registers instead of keeping track of unneeded precision. (typically J, H ~ 1).
*/
float lattice::compute_point_energy(int row, int col) {
int accumulator = get(row + 1, col) + get(row - 1, col) + get(row, col - 1) +
get(row, col + 1);
return -get(row, col) * (accumulator * J_ + H_);
}
/*
Computes total magnetisation in O(n^2). Thread safe
*/
int lattice::total_magnetisation() {
int sum = 0;
#pragma omp parallel for reduction(+ : sum)
for (unsigned int i = 0; i < size_ * size_; i++) {
sum += spins_->at(i);
}
return sum;
}
int inline to_periodic(int row, int col, int size) {
if (row < 0 || row >= size)
row = abs(size - abs(row));
if (col < 0 || col >= size)
col = abs(size - abs(col));
return row * size + col;
}
用 lattice.h
#ifndef lattice_h
#define lattice_h
#include <cmath>
#include <vector>
/* Converts spin up/down to easily printable symbols. */
char inline to_symbol(int in) { return in == -1 ? '-' : '+'; }
/* Converts given pair of indices to those with periodic boundary conditions. */
int inline to_periodic(int row, int col, int size) {
if (row < 0 || row >= size)
row = abs(size - abs(row));
if (col < 0 || col >= size)
col = abs(size - abs(col));
return row * size + col;
}
class lattice {
private:
unsigned int size_;
// vector<bool> would be more space efficient, but it would not allow
// multithreading
std::vector<short> *spins_;
float J_;
float H_;
public:
lattice() noexcept : size_(0), spins_(NULL), J_(1.0), H_(0.0) {}
lattice(int new_size, double new_J, double new_H) noexcept
: size_(new_size), spins_(new std::vector<short>(size_ * size_, 1)),
J_(new_J), H_(new_H) {}
lattice(const lattice &other) noexcept
: lattice(other.size_, other.J_, other.H_) {
#pragma omp parallel for
for (unsigned int i = 0; i < size_ * size_; i++)
spins_->at(i) = other.spins_->at(i);
}
lattice &operator=(const lattice &);
~lattice() { delete spins_; }
void print();
short get(int row, int col) {
return spins_->at(to_periodic(row, col, size_));
}
unsigned int get_size() { return size_; }
void flip(int row, int col) { spins_->at(to_periodic(row, col, size_)) *= -1; }
int total_magnetisation();
float compute_point_energy(int row, int col);
};
#endif
和simulation.cpp
#include <iostream>
#include <math.h>
#include "include/simulation.h"
using namespace std;
/*
Advances the simulation a given number of steps, and updates/prints the statistics
into the given file pointer.
Defaults to stdout.
The number of time_steps is explcitly unsigned, so that linters/IDEs remind
the end user of the file that extra care needs to be taken, as well as to allow
advancing the simulation a larger number of times.
*/
void simulation::advance(unsigned int time_steps, FILE *output) {
unsigned int area = spin_lattice_.get_size() * spin_lattice_.get_size();
for (unsigned int i = 0; i < time_steps; i++) {
// If we don't update mean_energy_ every time, we might get incorrect
// thermodynamic behaviour.
total_energy_ = compute_energy(spin_lattice_);
double temperature_delta = total_energy_/area - mean_energy_;
if (abs(temperature_delta) < 1/area){
cerr<<temperature_delta<<"! Reached equilibrium "<<endl;
}
temperature_ += temperature_delta;
mean_energy_ = total_energy_ / area;
if (time_ % print_interval_ == 0) {
total_magnetisation_ = spin_lattice_.total_magnetisation();
mean_magnetisation_ = total_magnetisation_ / area;
print_status(output);
}
advance();
}
}
/*
Advances the simulation a single step.
DOES NOT KEEP TRACK OF STATISTICS. Hence private.
*/
void simulation::advance() {
#pragma omp parallel for collapse(2)
for (unsigned int row = 0; row < spin_lattice_.get_size(); row++) {
for (unsigned int col = 0; col < spin_lattice_.get_size(); col++) {
double dE = compute_dE(row, col);
double p = r_.random_uniform();
float rnd = rand() / (RAND_MAX + 1.);
if (exp(-dE / temperature_) > rnd) {
spin_lattice_.flip(row, col);
}
}
}
time_++;
}
/*
Computes change in energy due to flipping one single spin.
The function returns a single-precision floating-point number, as data cannot under
most circumstances make use of greater precision than that (save J is set to a
non-machine-representable value).
The code modifies the spin lattice, as an alternative (copying the neighborhood
of a given point), would make the code run slower by a factor of 2.25
*/
float simulation::compute_dE(int row, int col) {
float e_0 = spin_lattice_.compute_point_energy(row, col);
return -4*e_0;
}
/*
Computes the total energy associated with spins in the spin_lattice_.
I originally used this function to test the code that tracked energy as the lattice
itself was modified, but that code turned out to be only marginally faster, and
not thread-safe. This is due to a race condition: when one thread uses a neighborhood
of a point, while another thread was computing the energy of one such point in
the neighborhood of (row, col).
*/
double simulation::compute_energy(lattice &other) {
double energy_sum = 0;
unsigned int max = other.get_size();
#pragma omp parallel for reduction(+ : energy_sum)
for (unsigned int i = 0; i < max; i++) {
for (unsigned int j = 0; j < max; j++) {
energy_sum += other.compute_point_energy(i, j);
}
}
return energy_sum;
}
void simulation::set_to_chequerboard(int step){
if (time_ !=0){
return;
}else{
for (unsigned int i=0; i< spin_lattice_.get_size(); ++i){
for (unsigned int j=0; j<spin_lattice_.get_size(); ++j){
if ((i/step)%2-(j/step)%2==0){
spin_lattice_.flip(i, j);
}
}
}
}
}
用simulation.h
#ifndef simulation_h
#define simulation_h
#include "lattice.h"
#include "rng.h"
#include <gsl/gsl_rng.h>
/*
The logic of the entire simulation of the Ising model of magnetism.
This simulation will run and print statistics at a given time interval.
A simulation can be advanced a single time step, or many at a time,
*/
class simulation {
private:
unsigned int time_ = 0; // Current time of the simulation.
rng r_ = rng();
lattice spin_lattice_;
double temperature_;
double mean_magnetisation_ = 1;
double mean_energy_;
double total_magnetisation_;
double total_energy_;
unsigned int print_interval_ = 1;
void advance();
public:
void set_print_interval(unsigned int new_print_interval) { print_interval_ = new_print_interval; }
simulation(int new_size, double new_temp, double new_J, double new_H)
: time_(0), spin_lattice_(lattice(new_size, new_J, new_H)), temperature_(new_temp),
mean_energy_(new_J * (-4)), total_magnetisation_(new_size * new_size),
total_energy_(compute_energy(spin_lattice_)) {}
void print_status(FILE *f) {
f = f==NULL? stdout : f;
fprintf(f, "%4d\t%e \t%e\t%e\n", time_, mean_magnetisation_,
mean_energy_, temperature_);
}
void advance(unsigned int time_steps, FILE *output);
double compute_energy(lattice &other);
float compute_dE(int row, int col);
void set_to_chequerboard(int step);
void print_lattice(){
spin_lattice_.print();
};
// void load_custom(const lattice& custom);
};
#endif
现在的输出看起来像这样:虽然它应该在 2.26 附近下降
最佳答案
我在您的代码中发现了一些问题:
compute_dE
方法返回错误的能量,因为 2 的因数不应该存在。伊辛系统的哈密顿量为
当你有效地使用
compute_energy
方法返回错误的能量。该方法应该只对每个自旋对迭代一次。像这样的东西应该可以解决问题:
for (unsigned int i = 0; i < max; i++) {
for (unsigned int j = i + 1; j < max; j++) {
energy_sum += other.compute_point_energy(i, j);
}
}
您使用动态更新的温度而不是使用目标温度。我不太明白这样做的目的。
关于c++ - 伊辛模型模拟偏移临界温度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49661195/
我正在尝试从第 4 到 9 页以及第 12 和 13 页上的单元格中清除所有内容(包括图像)。我有以下代码,但它正在清除第 3-9 和 12-15 页中的内容,我不知道为什么。 有什么想法吗? Sub
有没有办法增加极坐标图刻度标签(θ)的填充/偏移? import matplotlib import numpy as np from matplotlib.pyplot import figure,
我正在调用本地 API 并尝试以分页 样式进行操作。我有 n 张图片,我想将它们分成 n/4 行(每行 4 张图片)。因此,我正在调用我的 API,images/count,offset。但不知何故,
我的问题解释起来有点棘手,但无论如何我都会尝试。我有两个水平选项卡,当您单击它们时,会打开一个文本框内容。当他们被点击时,我试图“关注”他们。我在网上找到了很多资料,但除了我在下面显示的这段代码外,没
所以我有一个 float 的 div,我需要它始终向右 200 像素,并填充窗口的其余部分。有没有某种跨浏览器兼容的方法,我可以在不借助 javascript 的情况下使宽度填满页面的其余部分? 最佳
我有以下片段 $('html,body').animate({scrollTop: $('#menu').offset().top}, 'slow'); 单击链接时,我希望浏览器从#menu div
我目前正在为我的应用程序使用 JASidePanel,并且我有一个 UITableViewcontroller 和一个 UIRefreshControl 作为它的 ViewController 之一。
给出以下代码: imshow(np.arange(16*16).reshape(16,16)) cb = colorbar() cb.set_label("Foo") cb.set_ticks([0,
我是编程新手,我认为 VBA 是一个很好的起点,因为我在 Excel 中做了很多工作。 我创建了一个宏,它从输入框中获取一个整数(我一直使用 2、3 和 4 来测试),并创建该数字的一组 4 层层次结
我在 PHP 中有一个 unix 时间戳: $timestamp = 1346300336; 然后我有一个我想要应用的时区的偏移量。基本上,我想应用偏移量并返回一个新的 unix 时间戳。偏移量遵循这
演示:http://jsfiddle.net/H45uY/6/ 我在这里想做的是将 的左上角设为跟随鼠标。代码在没有段落的情况下工作正常(请参阅上面的演示),但是当您添加段落时,被向上推,鼠标位于盒
假设我们有两个由无符号长(64 位)数组表示的位图。我想使用特定的移位(偏移)合并这两个位图。例如,将位图 1(较大)合并到位图 2(较小)中,起始偏移量为 3。偏移量 3 表示位图 1 的第 3 位
通过在 pageViewController 中实现 tableView,tableView 与其显示的内容不一致。对此最好的解决办法是什么? 最佳答案 如果您的 TableView 是 View C
我设置了一个在 nib 中显示地点信息的地点配置文件。当我在标准屏幕流程中推送此 View 时,它工作正常。但是,当我从另一个选项卡推送此 View 时,UINavigationBar 似乎抵消了它,
如果我想选择 5 条记录,我会这样做: SELECT * FROM mytable LIMIT 5 如果我想添加偏移量,我会这样做: SELECT * FROM mytable OFFSET 5 LI
我有一个应用程序,其中某些 View 需要全屏,而其他 View 不需要全屏。在某些情况下,我希望背景显示在状态栏下方,所以我在 View 加载时使用它来使 Activity 全屏显示: window
在下图中,我进行绘制,结果位于 A 点,就在我手指接触的地方。 如何使图像显示在实际触摸上方约 40pt。 (二) 我正在使用经典的 coreGraphic UITouch 代码,如下所示: - (v
只要键盘处于事件状态,我就会尝试偏移 UITextField,效果很好,直到我尝试了表情符号布局。有没有办法检测键盘输入的类型,以便找出高度差?谢谢 最佳答案 不是使用 UIKeyboardDidSh
这是我的 Swift 代码 (AppDelegate.swift): var window: UIWindow? var rootViewController :UIViewController? f
我有一个 div 作为绝对定位的 body 的直接子节点,其 css 属性定义如下: div[id^="Container"] { display: block; position: a
我是一名优秀的程序员,十分优秀!