⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 用蒙特卡罗方法计算二维ISING模型磁场强度
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "MCS.h"
#include "W1.h"
#include "MainFrm.h"
#include "Math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_Simulation, OnSimulation)
	ON_COMMAND(ID_Showout, OnShowout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnSimulation() 
{
	int step,i,j,iu,ip,ju,jp,M,M1,M2,ien;
	double t,ranf;
	double ei[10];
	FILE *fp;

	t=0.6/0.221655;
	M=100*100;
	M1=0;
	M2=0;

//*************************init******************//
	for(i=0;i<=100;i++)
	{
		for(j=0;j<=100;j++)
		{
			is[i][j]=1;
		}
	}
/**///**************************end*******************//
	srand(time(NULL));
	for(i=1;i<=9;i++)
	{
		ei[i]=exp(-2.*(i-5)/t);
	}
	fp=fopen("BOUNDARY.txt","w");
	for(step=0;step<1000;step++)
	{
		for(i=0;i<=100;i++)
		{
			iu=i+1;
			ip=i-1;
			if(i==0)
				ip=100;
			if(i==100)
				iu=0;
			for(j=0;j<=100;j++)
			{
				ju=j+1;
				jp=j-1;
				if(j==0)
					jp=100;
				if(j==100)
					ju=0;
				ien=5+is[i][j]*(is[iu][j]+is[ip][j]+is[i][ju]+is[i][jp]);


				ranf=rand();
				ranf=ranf/RAND_MAX;
				if(ei[ien]>ranf)
				{
					is[i][j]=-is[i][j];
				    M=M+2*is[i][j];
				}


			}
		}

		if(step>500)
		{
			M1=M1+M;
			M2=M1/(step-500);
		}
        fprintf(fp,"%d %d %d\n",step,M,M2);
	}
	fclose(fp);
	
	fp=fopen("MODEL.txt","w");
	for(j=100;j>=0;j--)
	{
		for(i=0;i<=100;i++)
		{
			if(is[i][j]==-1)
				is[i][j]=0;
			fprintf(fp,"%d ",is[i][j]);
		}
		fprintf(fp,"\n");
	}
	fclose(fp);

}

void CMainFrame::OnShowout() 
{
	W1 *pW1Wnd = new W1;

	if (!pW1Wnd->Create(_T("Lattice 1"),
		WS_CHILD | WS_VISIBLE | WS_OVERLAPPEDWINDOW,
		CRect (0,0,500,500), this))
		return;		
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -