- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试模拟一维伊辛模型。该模型包含自旋链(100 次自旋),并使用 Mont Carlo - Metropolis 在系统能量(单一)下降或小于随机数时接受自旋翻转。在正确的程序中,能量和磁化强度都变为零,我们得到的结果是高斯分布的(能量或磁化强度的图形,由蒙特卡洛步骤数决定)。
我已经做了一些工作,但我认为我的随机生成器对此不正确,而且我不知道如何/在何处实现边界条件:链的最后一个旋转是第一个。我需要帮助才能完成它。欢迎任何帮助。谢谢。我正在粘贴我的 C 程序:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h> //necessary for function time()
#define LENGTH 100 //size of the chain of spins
#define TEMP 2 // Temperature in units of J
#define WARM 200 // Termalização
#define MCS 20000 //Monte Carlo Steps
void start( int spin[])
{
/* starts with all the spins 1 */
int i;
for (i = 0 ; i < 100; i++)
{
spin[i] = 1;
}
}
double energy( int spin[]) //of the change function J=1
{
int i;
double energyX=0;// because the begining Energy = -J*sum (until 100) =-100,
for (i = 0;i<100;i++)
energyX=energyX-spin[i]*spin[i+1];
return(energyX);
}
int randnum(){
int num;
srand(time(NULL));
/* srand(time(NULL)) objectives to initiate the random number generator
with the value of the function time(NULL). This is calculated as being the
total of seconds passed since january first of 1970 until the present date.
So, this way, for each execution the value of the "seed" will be different.
*/
srand(time(NULL));
//picking one spin randomly zero to 100
num=rand() % 100;
printf("num = %d ", num);
return num;
}
void montcarlo( int spin[])
{
int i,j,num;
double prob;
double energyA, energyB; // A -> old energy and B -> the new energy
int rnum1,rnum2;
prob=exp(-(energyB-energyA)/TEMP);
energyA = 0;
energyB = 0;
for (i = 0;i<100;i++)
{
for (j = 0;j<100;j++)
{
energyA=energy(spin);
rnum1=randnum();
rnum2=randnum(); // i think they will give me different numbers
spin[rnum1] = -spin[rnum1]; //flip of the randomly selected spin
energyB = energyB-spin[j]*spin[j+1];
if ((energyB-energyA<0)||((energyB-energyA>0)&&(rnum2>prob))){ // using rnum2 not to be correlated if i used rnum1
spin[rnum1]=spin[rnum1];} // keep the flip
else if((energyB-energyA>0)&&(rnum2<prob))
spin[rnum1]=-spin[rnum1]; // unflip
}
}
}
int Mag_Moment( int spin[] ) // isso é momento magnetico
{
int i;
int mag;
for (i = 0 ; i < 100; i++)
{
mag = mag + spin[i];
}
return(mag);
}
int main()
{
// starting the spin's chain
int spin[100];//the vector goes til LENGHT=100
int i,num,j;
int itime;
double mag_moment;
start(spin);
double energy_chain=0;
energy_chain=energy(spin); // that will give me -100 in the begining
printf("energy_chain starts with %f", energy_chain);// initially it gives -100
/*Warming it makes the spins not so ordered*/
for (i = 1 ; i <= WARM; i++)
{
itime = i;
montcarlo(spin);
}
printf("Configurtion after warming %d \n", itime);
for (j = 0 ; j < LENGTH; j++)
{
printf("%d",spin[j]);
}
printf("\n");
energy_chain=energy(spin); // new energy after the warming
/*openning a file to save the values of energy and magnet moment of the chain*/
FILE *fp; // declaring the file for the energy
FILE *fp2;// declaring the file for the mag moment
fp=fopen("energy_chain.txt","w");
fp2=fopen("mag_moment.txt","w");
int pures;// net value of i
int a;
/* using Monte Carlo metropolis for the whole chain */
for (i = (WARM + 1) ; i <= MCS; i++)
{
itime=i;//saving the i step for the final printf.
pures = i-(WARM+1);
montcarlo(spin);
energy_chain = energy_chain + energy(spin);// the spin chain is moodified by void montcarlo
mag_moment = mag_moment + Mag_Moment(spin);
a=pures%10000;// here i select a value to save in a txt file for 10000 steps to produce graphs
if (a==0){
fprintf(fp,"%.12f\n",energy_chain); // %.12f just to give a great precision
fprintf(fp2,"%.12f\n",mag_moment);
}
}
fclose(fp); // closing the files
fclose(fp2);
/* Finishing -- Printing */
printf("energy_chain = %.12f\n", energy_chain);
printf("mag_moment = %.12f \n", mag_moment);
printf("Temperature = %d,\n Size of the system = 100 \n", TEMP);
printf("Warm steps = %d, Montcarlo steps = %d \n", WARM , MCS);
printf("Configuration in time %d \n", itime);
for (j = 0 ; j < 100; j++)
{
printf("%d",spin[j]);
}
printf("\n");
return 0;
}
最佳答案
您应该在您的程序中只调用一次 srand(time(NULL));
。每次你在同一秒内调用它,你都会得到相同的随机数序列。所以很可能两次调用 randnum
都会给你相同的数字。
只需在 main
的开头添加 srand(time(NULL));
并将其从其他地方删除。
关于c - Ising 1-Dimensional C - 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28094157/
我正在尝试使用预先训练的模型。这就是问题发生的地方 模型不是应该接受简单的彩色图像吗?为什么它需要 4 维输入? RuntimeError T
我正在尝试实现自定义用户控件。 让我们考虑 ViewModels: public class FileViewModel { public string Name { get; set; }
我承认,我脑子有问题。我已经走得够远了,几乎可以看到隧道尽头的曙光,但我不确定要采取的下一步。 我创建了一个 SQLfiddle example here 这是 SQL 数据透视表: SET @sql
我收到 SSAS 错误“函数中指定的两个集合具有不同的维度”。 我实际上使用相同的维度,相同的层次结构(隐式),在一组中我使用“全部”成员,而在另一组中我使用叶成员。如果我更改成员标识符以显式引用(单
我有以下值,每个值都是 double 类型的标量:a1, a2, a3, a4, a5 . 我尝试使用 Numpy 连接它们,如下所示: f = np.concatenate((a1,a2,a3,a4
我有一个功能 findMaxEval我以以下方式调用:eMax0,var0=findMaxEval(np.diag(eVal0),q,bWidth=.01)哪里np.diag(eVal0)是一个形状为
我复制了spline example具有以下点(而不是随机点): var points = [[75, 15], [104, 80], [275, 80], [300, 15]]; 它工作完美。现在我
我正在尝试将值传递给后台 worker 。我之前发帖询问如何做到here 。一个答案将我引向了这段代码,但我在尝试使用它时遇到了问题。这就是我正在做的事情: Class MyParameters
问题是这样的:程序的输出是什么? #include int main() { int A[2][10]={{1,2,3,4,5,6,7,8,9,10},{11,12,13,14,15,16,17,1
我可以像这样内联创建一个 jquery 对象(这段代码有效) $('#tip').qtip({ content: el.REASON, position
我正在尝试模拟一维伊辛模型。该模型包含自旋链(100 次自旋),并使用 Mont Carlo - Metropolis 在系统能量(单一)下降或小于随机数时接受自旋翻转。在正确的程序中,能量和磁化强度
我试图从 Ruby 调用的 C 函数是这样的: void foo(double *in_array, double *out_array) 哪里: in_array 是一组数组,“foo”将使用这些数
我正在尝试校准加速度计,但我无法获得校准所需的 6 个不同加速度读数的 6 个样本值。 PreliminaryW 是一个 double[6][3] 数组,用于填充这些样本值。它是 6 x 3,因为每个
我很难想出一种有效的方法来找到二维容器中给定值的所有相邻方 block 。假设我有一个容器表示为: . . . . . . G . . . . . . . . . . . . . . . . . G
例如:一个二维数组可以想象成一堵方砖砌成的砖墙,其中每 block 砖代表我们数组中的一个坐标。 3 维数组可以同样的方式可视化为一个盒子或立方体。 但是,这是棘手的部分,您如何可视化具有多个(超过
我有以下代码通过 seaborn 创建一个表格和一个条形图。 #Building a dataframe grouped by the # of Engagement Types sales_type
我在 excel 中有一系列单元格,左列有人名,顶行有某些任务(作为数字),比如两个暗淡的数组。我想做的就是能够搜索名称和编号的任务,以查看该单元格在 VBA 中是否为空白。 在 excel 表中,我
问题总结:自定义函数的backward pass中如何处理输入和输出的维度? 根据manual ,自定义函数的基本结构如下: class MyFunc(torch.autograd.Function)
我一直在用 Javascript 开发一个项目,在达到一定的开发阶段后,代码停止工作。我已将问题范围缩小到在 Javascript 中创建和索引“多维”数组。我包含的代码只是为了测试创建数组的数组、为
package arrays; import java.util.Arrays; public class Route { int cityindex; int stadtwahl; String[]
我是一名优秀的程序员,十分优秀!