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

📄 testeso1doc.cpp

📁 利用渐进结构优化算法(ESO)实现矩形板的第一阶频率的最大化。用VC实现界面
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// TestESO1Doc.cpp : implementation of the CTestESO1Doc class
//

#include "stdafx.h"
#include "TestESO1.h"
#include "TestESO1Doc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestESO1Doc

IMPLEMENT_DYNCREATE(CTestESO1Doc, CDocument)

BEGIN_MESSAGE_MAP(CTestESO1Doc, CDocument)
	//{{AFX_MSG_MAP(CTestESO1Doc)
	ON_COMMAND(ID_OPTMIZATION, OnOptmization)
	ON_COMMAND(ID_OPTIMIZATION_PREP, OnOptimizationPrep)
	ON_UPDATE_COMMAND_UI(ID_OPTMIZATION, OnUpdateOptmization)
	ON_UPDATE_COMMAND_UI(ID_CONTROL_ONESTEP, OnUpdateControlOnestep)
	ON_COMMAND(ID_CONTROL_ONESTEP, OnControlOnestep)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestESO1Doc construction/destruction

CTestESO1Doc::CTestESO1Doc()
{
	m_bPrepared=FALSE;
	m_nIteration=0;
	m_nTotNumElementNotDeletable=0;

}

CTestESO1Doc::~CTestESO1Doc()
{
}

BOOL CTestESO1Doc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	
	if (!(m_ep = engOpen("\0")))
	{
		AfxMessageBox(_T("无法运行Matlab,请确认Matlab是否正确安装!"));
		return FALSE;
	}
	engSetVisible(m_ep,FALSE);



//	m_MatlabApp.CreateDispatch("Matlab.Application");
//	if(!m_MatlabApp)
//	{
//		AfxMessageBox(_T("无法运行Matlab,请确认Matlab是否正确安装!"));
//		return FALSE;
//	}

	GetSettings();
	((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CTestESO1Doc serialization

void CTestESO1Doc::Serialize(CArchive& ar)
{
	// CEditView contains an edit control which handles all serialization
	((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CTestESO1Doc diagnostics

#ifdef _DEBUG
void CTestESO1Doc::AssertValid() const
{
	CDocument::AssertValid();
}

void CTestESO1Doc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestESO1Doc commands

void CTestESO1Doc::OnOptmization() 
{
}

void CTestESO1Doc::InitNodes()
{
	int nNumofNodes=INITNUMOFNODES;
	int nNodeIndex, i,j;
	double d;
	double xset[51], yset[11];

	d=0;
	for(i=0; i<51; i++)
	{
		xset[i]=d;
		d+=0.004;
	}

	d=-0.01;
	for(j=0; j<11; j++)
	{
		yset[j]=d;
		d+=0.002;
	}

	i=j=0;

	for(nNodeIndex=1; nNodeIndex<=nNumofNodes; nNodeIndex++)
	{
		CNode* pNode;
		pNode=new CNode;
		pNode->m_nNodeID=nNodeIndex;
		m_mapNodeIDToNode[(WORD)nNodeIndex]=pNode;
		pNode->m_dXCoord=xset[i];
		pNode->m_dYCoord=yset[j];
		j++;
		if(j>10)
		{
			i++;
			j=0;
		}
	}
	ASSERT(i==51);
}

void CTestESO1Doc::OnCloseDocument() 
{
	engEvalString(m_ep,_T("clear all"));
	engEvalString(m_ep,_T("close all"));
	engClose(m_ep);
	PutSettings();
	CDocument::OnCloseDocument();
}

void CTestESO1Doc::InitElements()
{
	int nElementIndex, i, j, nDeletable=0;
	for(nElementIndex=1, i=1, j=1; nElementIndex<=INITNUMOFELEMENTS; nElementIndex++)
	{
		CElement* pElement;
		CNode *plftbtm, *prhtBtm, *plftupp, *prhtupp;
		CObject *pObject;
		m_mapNodeIDToNode.Lookup(WORD(j), pObject);
		plftbtm = (CNode*)pObject;
		m_mapNodeIDToNode.Lookup(WORD(j+1), pObject);
		plftupp = (CNode*)pObject;
		m_mapNodeIDToNode.Lookup(WORD(j+11), pObject);
		prhtBtm = (CNode*)pObject;
		m_mapNodeIDToNode.Lookup(WORD(j+12), pObject);
		prhtupp = (CNode*)pObject;

		pElement= new CElement(nElementIndex, plftbtm, prhtBtm, prhtupp, plftupp);
		m_mapElementIDToElement[(WORD)nElementIndex]=pElement;
		if(nElementIndex>10 && nElementIndex<=490)
		{
			pElement->m_bCanbeDeleted=TRUE;
			nDeletable++;
		}

		j++;
		if(j>=10+i)
		{
			i+=11;
			j=i;
		}
	}
	m_nTotNumElementNotDeletable=INITNUMOFELEMENTS-nDeletable;

}

void CTestESO1Doc::DrawSpecifiedElement(int nElementID)
{
	CObject  *pObject;
	CElement *pElement;
	m_mapElementIDToElement.Lookup(WORD(nElementID), pObject);
	pElement=(CElement*)pObject;


	double dWidth, dHeight;
	CString strCmd;
//	mxArray *T = NULL;
//	_variant_t vtX(pElement->m_NodesArray[0]->m_dXCoord);
//	_variant_t vtY(pElement->m_NodesArray[0]->m_dYCoord);
	strCmd.Format(_T("X=%f"),pElement->m_NodesArray[0]->m_dXCoord);
	engEvalString(m_ep, strCmd);
//	m_ep.PutWorkspaceData(_T("X"), _T("base"), vtX);
	strCmd.Format(_T("Y=%f"),pElement->m_NodesArray[0]->m_dYCoord);
	engEvalString(m_ep, strCmd);
//	m_MatlabApp.PutWorkspaceData(_T("Y"), _T("base"), vtY);
	dWidth = pElement->m_NodesArray[1]->m_dXCoord-pElement->m_NodesArray[0]->m_dXCoord;
	dHeight= pElement->m_NodesArray[3]->m_dYCoord-pElement->m_NodesArray[0]->m_dYCoord;
//	_variant_t vtW(dWidth);
//	_variant_t vtH(dHeight);
	strCmd.Format(_T("W=%f"),dWidth);
	engEvalString(m_ep, strCmd);
	strCmd.Format(_T("H=%f"),dHeight);
	engEvalString(m_ep, strCmd);

	engEvalString(m_ep, _T("rectangle('Position',[X,Y,W,H],'FaceColor',[0.8 0.8 0.8])"));
	engEvalString(m_ep,_T("clear X"));
	engEvalString(m_ep,_T("clear Y"));
	engEvalString(m_ep,_T("clear W"));
	engEvalString(m_ep,_T("clear H"));

}

void CTestESO1Doc::DrawElements()
{
	POSITION  pos;

	pos=m_mapElementIDToElement.GetStartPosition();
	
	while (pos != NULL)
	{
		WORD nIndex;
		CObject* pObject;
		m_mapElementIDToElement.GetNextAssoc(pos, nIndex, pObject);
		DrawSpecifiedElement(int(nIndex));
	}
	engEvalString(m_ep,_T("axis equal"));
}

void CTestESO1Doc::RemoveSpecifiedElement(int nElementID)
{
	CObject  *pObject;
	CElement *pElement;
	m_mapElementIDToElement.Lookup(WORD(nElementID), pObject);
	pElement=(CElement*)pObject;


	double dWidth, dHeight;
	CString strCmd;
//	_variant_t vtX(pElement->m_NodesArray[0]->m_dXCoord);
//	_variant_t vtY(pElement->m_NodesArray[0]->m_dYCoord);
	strCmd.Format(_T("X=%f"),pElement->m_NodesArray[0]->m_dXCoord);
	engEvalString(m_ep, strCmd);
	strCmd.Format(_T("Y=%f"),pElement->m_NodesArray[0]->m_dYCoord);
	engEvalString(m_ep, strCmd);

//	m_MatlabApp.PutWorkspaceData(_T("X"), _T("base"), vtX);
//	m_MatlabApp.PutWorkspaceData(_T("Y"), _T("base"), vtY);
	dWidth = pElement->m_NodesArray[1]->m_dXCoord-pElement->m_NodesArray[0]->m_dXCoord;
	dHeight= pElement->m_NodesArray[3]->m_dYCoord-pElement->m_NodesArray[0]->m_dYCoord;
//	_variant_t vtW(dWidth);
//	_variant_t vtH(dHeight);
//	m_MatlabApp.PutWorkspaceData(_T("W"), _T("base"), vtW);
//	m_MatlabApp.PutWorkspaceData(_T("H"), _T("base"), vtH);
	strCmd.Format(_T("W=%f"),dWidth);
	engEvalString(m_ep, strCmd);
	strCmd.Format(_T("H=%f"),dHeight);
	engEvalString(m_ep, strCmd);

	engEvalString(m_ep,_T("rectangle('Position',[X,Y,W,H],'FaceColor','w')"));
	engEvalString(m_ep,_T("clear X"));
	engEvalString(m_ep,_T("clear Y"));
	engEvalString(m_ep,_T("clear W"));
	engEvalString(m_ep,_T("clear H"));

}

void CTestESO1Doc::InitStruct()
{
	POSITION pos;
	pos=GetFirstViewPosition();
	CView* pFirstView = GetNextView( pos );
	pFirstView->SendMessage(WM_USER_ERASE, 0, 0);
	InitNodes();
	InitElements();
	DrawElements();
}

void CTestESO1Doc::OnOptimizationPrep() 
{
	if(m_nIteration)
	{
		int iReply;
		iReply=AfxMessageBox(_T("重新开始将删除以往所有步骤的文件,确信吗?"), MB_YESNO);
		if(iReply==IDNO)
			return;
	}
	CWaitCursor wait;
	// Call the function before set m_nIteration=0
	DeleteAllIterationSubDir();
	engEvalString(m_ep,_T("clear all"));
	engEvalString(m_ep,_T("close all"));
	PutKMMatrix();
	InitStruct();
	m_bPrepared=TRUE;
	m_nIteration=0;

	if(!GenerateBDF())
		return;

	CString strSubDir;
	strSubDir.Format(_T("Iteration %d"), m_nIteration);
	CString strCurrentPath;
	strCurrentPath.Format(_T("%s\\%s"), GetModulePath(),strSubDir);

	::CreateDirectory(strCurrentPath, NULL);
	RunNastran(strCurrentPath);
	GetNodesDisplacements(strCurrentPath);
	m_strInfo.Format(_T("Iteration %d EigenValue=%e\r\n"), m_nIteration, m_dCurrentEigen);
	UpdateAllViews(NULL);
	WriteInfomation(strCurrentPath);
}

void CTestESO1Doc::OnUpdateOptmization(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bPrepared);
}

void CTestESO1Doc::OnUpdateControlOnestep(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bPrepared);
}

BOOL CTestESO1Doc::GenerateBDF()
{
	CString strBDF;
	strBDF.Format(_T("%s\\CurrentStruct.bdf"),GetModulePath());

	HANDLE hDBFFile = CreateFile(strBDF, GENERIC_WRITE, FILE_SHARE_READ,
		NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	if (hDBFFile == INVALID_HANDLE_VALUE)
	{
		AfxMessageBox(_T("无法生成dbf文件!"));
		return FALSE;
	}

	CFile DBFFile((int)hDBFFile);
	

	CTime time = CTime::GetCurrentTime();
	CString strDate;
	strDate.Format(_T("%d-%d-%d  %d:%d:%d"), time.GetDay(), time.GetMonth(), time.GetYear(),
		           time.GetHour(), time.GetMinute(), time.GetSecond());

	CString strUpperContents;
	strUpperContents=CString(_T("SOL 103\r\n"
								"$ Direct Text Input for Executive Control\r\n"));
	strUpperContents+=CString(_T("TITLE = "))+strDate+CString(_T("\r\n"));
	strUpperContents+=CString(_T("ECHO = NONE\r\n"
				"$ Direct Text Input for Global Case Control Data\r\n"
				"SUBCASE 1\r\n"
				"   SUBTITLE=Default\r\n"
				"   METHOD = 1\r\n"
				"   SPC = 2\r\n"
				"   set 1 = 1\r\n"
				"   VECTOR(SORT1,REAL)=ALL\r\n"
				"BEGIN BULK\r\n"
				"PARAM    POST    0\r\n"
				"PARAM   PRTMAXIM YES\r\n"
				"EIGRL    1                       1       0                       MASS\r\n"
				"MAT1     1        100.0             0.3   1.0-5\r\n"));

	DBFFile.Write(strUpperContents, strUpperContents.GetLength());


	// Writes nodes infomation
	CString strTemp;
	strTemp=CString(_T("$ Nodes\r\n"));
	DBFFile.Write(strTemp, strTemp.GetLength());

	POSITION  pos;
	pos=m_mapNodeIDToNode.GetStartPosition();
	while (pos != NULL)
	{
		WORD nIndex;
		CNode *pNode;
		CObject* pObject;
		m_mapNodeIDToNode.GetNextAssoc(pos, nIndex, pObject);
		pNode=(CNode*)pObject;

		CString strNodeInfo;
		strNodeInfo.Format(_T("GRID     %6d         %8.3f%8.3f%8.1f\r\n"), pNode->m_nNodeID,
			               pNode->m_dXCoord, pNode->m_dYCoord, 0.0);

		DBFFile.Write(strNodeInfo, strNodeInfo.GetLength());
	}

	// Writes elements infomation
	strTemp=CString(_T("$ Element Properties\r\nPSHELL   1       1      1.       -1\r\n"
		            "$ Elements\r\n"));
	DBFFile.Write(strTemp, strTemp.GetLength());

	pos=m_mapElementIDToElement.GetStartPosition();
	while (pos != NULL)
	{
		WORD nIndex;
		CElement *pElement;
		CObject* pObject;
		m_mapElementIDToElement.GetNextAssoc(pos, nIndex, pObject);
		pElement=(CElement*)pObject;

		CString strElementInfo;
		strElementInfo.Format(_T("CQUAD4   %6d     1   %8d%8d%8d%8d\r\n"), 
				pElement->m_nElementID, pElement->m_NodesArray[0]->m_nNodeID,
				pElement->m_NodesArray[1]->m_nNodeID, pElement->m_NodesArray[2]->m_nNodeID,
				pElement->m_NodesArray[3]->m_nNodeID);

		DBFFile.Write(strElementInfo, strElementInfo.GetLength());
	}

	CString strLowerContents;
	strLowerContents=CString(_T("$ Loads for Load Case : Default\r\n"
								"SPCADD   2       1       3       4\r\n"
								"$ Displacement Constraints: ALL\r\n"
								"SPC1     1       3456    1       THRU    561\r\n"
								"$ Displacement Constraints: Fixed\r\n"
								"SPC1     3       123456    1     THRU     11\r\n"
								"SPC1     4       123456     551   THRU      561\r\n"
								"$ Referenced Coordinate Frames\r\n"
								"ENDDATA\r\n"));

	DBFFile.Write(strLowerContents, strLowerContents.GetLength());

	// We can call Close() explicitly, but the destructor would have
	// also closed the file for us. Note that there's no need to
	// call the CloseHandle() on the handle returned by the API because
	// MFC will close it for us.
	DBFFile.Close();
	return TRUE;
}

void CTestESO1Doc::OnControlOnestep() 
{
	ManipulateSensitivity();

	if(!GenerateBDF())
		return;

	m_nIteration++;
	CString strSubDir;
	strSubDir.Format(_T("Iteration %d"), m_nIteration);
	CString strCurrentPath;
	strCurrentPath.Format(_T("%s\\%s"), GetModulePath(),strSubDir);

	::CreateDirectory(strCurrentPath, NULL);
	RunNastran(strCurrentPath);
	GetNodesDisplacements(strCurrentPath);
	m_strInfo.Format(_T("\r\nIteration %d EigenValue=%e\r\n"), m_nIteration, m_dCurrentEigen);
	UpdateAllViews(NULL);
	WriteInfomation(strCurrentPath);

}

void CTestESO1Doc::RunNastran(CString strPathUnder)
{

⌨️ 快捷键说明

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