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

📄 element.cpp

📁 用VC写得计算电磁学的有限元分析程序
💻 CPP
字号:
// Element.cpp: implementation of the CElement class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FiniteElec.h"
#include "Element.h"
#include "math.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CElement::CElement()
{
	Init();
}

CElement::CElement(int id)
{
	Init();
	m_elementid=id;
}

CElement::~CElement()
{
	Init();
}
/////////////////////////
//拷贝构造函数
CElement::CElement(const CElement &element)
{
	m_elementid=element.m_elementid;
	for(int i=0;i<3;i++)
	{
		//这是指针,仍是以前的地址,要更改地址
		m_pNodes[i]=element.m_pNodes[i];
	}
	m_value=0.0f;//注意要用0
}

void CElement::Init()
{
	m_elementid=0;
	m_area=0.0f;
	m_value=0.0f;
	m_pNodes[0]=NULL;
	m_pNodes[1]=NULL;
	m_pNodes[2]=NULL;
}
//////////////////////////////////////
//
void CElement::SetNode(CNode *pNode0, CNode *pNode1, CNode *pNode2)
{
	m_pNodes[0]=pNode0;
	m_pNodes[1]=pNode1;
	m_pNodes[2]=pNode2;
	GetArea();//同时可以求出面积
}
///////////////////////////////////////
void CElement::Serialize(CArchive &ar)
{
	CString tempStr;
	if(ar.IsStoring())
	{
		tempStr.Format("%d",m_elementid);
		ar.WriteString(tempStr);
		for(int i=0;i<3;i++)
		{
			tempStr.Format("\t%d",m_pNodes[i]->m_nodeid);
			ar.WriteString(tempStr);
		}
		ar.WriteString("\r\n");
	}
	else
	{
	}
}

/////////////////////////////////////
int CElement::GetID()
{
	return m_elementid;
}
///////////////////////////////////
double CElement::GetArea()
{
	//如果不为零,说明已经求过面积fabs(m_area)<1.0e-6
	if(1)
	{
		double xij,yij,xik,yik;
		xij=m_pNodes[1]->m_nodeXY[0]-m_pNodes[0]->m_nodeXY[0];
		yij=m_pNodes[1]->m_nodeXY[1]-m_pNodes[0]->m_nodeXY[1];	
		xik=m_pNodes[2]->m_nodeXY[0]-m_pNodes[0]->m_nodeXY[0];
		yik=m_pNodes[2]->m_nodeXY[1]-m_pNodes[0]->m_nodeXY[1];
		m_area=(xij*yik-xik*yij)*0.5;
	}
	return m_area;
}
///////////////////////////////////
//---单元刚度矩阵----
void CElement::GetElementStiffMatrix(double **elementStiffMatrix)
{
	int i,j,m;
	double a[3],b[3],c[3];
	for(i=0;i<3;i++)
	{
		//i,j,m 轮换,使用条件运算符
		j=(i+1)==3?0:(i+1);
		m=(j+1)==3?0:(j+1);
		a[i]=m_pNodes[j]->m_nodeXY[0]*m_pNodes[m]->m_nodeXY[1]-
			m_pNodes[m]->m_nodeXY[0]*m_pNodes[j]->m_nodeXY[1];
		b[i]=m_pNodes[j]->m_nodeXY[1]-m_pNodes[m]->m_nodeXY[1];
		c[i]=m_pNodes[m]->m_nodeXY[0]-m_pNodes[j]->m_nodeXY[0];
	}
	for(i=0;i<3;i++)
	{	
		for(j=0;j<3;j++)
		{
			elementStiffMatrix[i][j]=b[i]*b[j]+c[i]*c[j];
			elementStiffMatrix[i][j]/=(2*GetArea());
		}
	}
}

//////////////////////////
//通过已求出的结点电位值计算单元内电位值
double CElement::GetValue()
{
	//这里只是求出单元中心的电位值
	double temp_d;
	temp_d=m_pNodes[0]->GetValue()+m_pNodes[1]->
		GetValue()+m_pNodes[2]->GetValue();
	return temp_d/3.0f;
}

/////////////////////////////
//绘制单元形态//xoy坐标第原心坐标
void CElement::DrawFrame(CDC *pDC,CPoint xoy,double scale)
{
	int i,j;
	CPoint p_1,p_2;
	CString tempStr;
	scale=50*scale;
	CPen redpen(PS_SOLID,1,RGB(0,0,255));
	CBrush redbrush(RGB(255,0,0));
	int savedc;
	for(i=0;i<3;i++)
	{
		//----------------
		j=(((i+1)==3)?0:(i+1));
		p_1=CPoint(int(m_pNodes[i]->m_nodeXY[0]*scale),int(m_pNodes[i]->m_nodeXY[1]*scale));
		p_2=CPoint(int(m_pNodes[j]->m_nodeXY[0]*scale),int(m_pNodes[j]->m_nodeXY[1]*scale));
		p_1.y=0-p_1.y;p_1+=xoy;
		p_2.y=0-p_2.y;p_2+=xoy;
		pDC->MoveTo(p_1);
		pDC->LineTo(p_2);
		//----------------
		if(m_pNodes[i]->m_nodetype==0)//无荷载结点
		{//不画重复结点
			tempStr.Format("%d",m_pNodes[i]->m_nodeid);
			pDC->TextOut(p_1.x,p_1.y,tempStr);
		}
		else if(m_pNodes[i]->m_nodetype==1)//荷载结点
		{
			savedc=pDC->SaveDC();
			pDC->SelectObject(&redpen);
			pDC->SelectObject(&redbrush);
			tempStr.Format("%d",m_pNodes[i]->m_nodeid);
			pDC->TextOut(p_1.x,p_1.y,tempStr);
			pDC->Ellipse((p_1+CPoint(-5,-5)).x,(p_1+CPoint(-5,-5)).y,
				(p_1+CPoint(5,5)).x,(p_1+CPoint(5,5)).y);
			pDC->RestoreDC(savedc);
		}
	}
}
//绘制等值线图
void CElement::DrawEqualValueGraph(CDC *pDC,CPoint xoy,double min,double max,double scale)
{
	int i;
	int  index;//色彩级别数
	int dc=pDC->SaveDC();//保存DC
	CBrush brush;//定义刷子,填充色。
	CPen   pen;	//定义外框线
	COLORREF color;//颜色
	//----------------------- 
	scale=50*scale;
	index=8;
	color=RGB(int(255.0f*(GetValue()-min)/(max-min)),
		int(255.0f*(GetValue()-min)/(max-min)/3),
		int(255.0f*(GetValue()-min)/(max-min)));
	pen.CreatePen(PS_SOLID,1,color);
	brush.CreateSolidBrush(color);
	pDC->SelectObject(&pen);
	pDC->SelectObject(&brush);
	//------------------------
	CPoint points[3];
	for(i=0;i<3;i++)
	{
		points[i]=CPoint(int(m_pNodes[i]->m_nodeXY[0]*scale),
			int(m_pNodes[i]->m_nodeXY[1]*scale));
		points[i].y=0-points[i].y;points[i]+=xoy;
	}
	pDC->Polygon(points,3);
	//-----------------------
	pDC->RestoreDC(dc);//恢复DC
}

⌨️ 快捷键说明

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