new: enable cascaded multi-grids... incl. an example

pull/1/head
Thorsten Liebig 2010-09-08 16:07:28 +02:00
parent bd4794ecc4
commit 20ade0f053
8 changed files with 223 additions and 24 deletions

View File

@ -48,7 +48,6 @@ Engine::~Engine()
void Engine::Init()
{
Reset();
numTS = 0;
volt = Create_N_3DArray<FDTD_FLOAT>(numLines);
curr = Create_N_3DArray<FDTD_FLOAT>(numLines);

View File

@ -36,15 +36,27 @@ Engine_CylinderMultiGrid::Engine_CylinderMultiGrid(const Operator_CylinderMultiG
m_WaitOnChild = new boost::barrier(2);
m_WaitOnSync = new boost::barrier(2);
Engine* eng = op->GetInnerOperator()->CreateEngine();
m_InnerEngine = dynamic_cast<Engine_Multithread*>(eng);
m_Eng_Ext_MG = new Engine_Ext_CylinderMultiGrid(NULL,true);
m_Eng_Ext_MG->SetBarrier(m_WaitOnBase, m_WaitOnChild, m_WaitOnSync);
m_Eng_Ext_MG->SetEngine(this);
m_InnerEng_Ext_MG = new Engine_Ext_CylinderMultiGrid(NULL,false);
Engine* eng = op->GetInnerOperator()->CreateEngine();
m_InnerEngine = dynamic_cast<Engine_Multithread*>(eng);
Engine_Ext_CylinderMultiGrid* m_InnerEng_Ext_MG = new Engine_Ext_CylinderMultiGrid(NULL,false);
m_InnerEng_Ext_MG->SetBarrier(m_WaitOnBase, m_WaitOnChild, m_WaitOnSync);
// if already has a base extension, switch places ... seems to be faster...
for (size_t n=0;n<m_InnerEngine->m_Eng_exts.size();++n)
{
Engine_Ext_CylinderMultiGrid* eng_mg = dynamic_cast<Engine_Ext_CylinderMultiGrid*>(m_InnerEngine->m_Eng_exts.at(n));
if (eng_mg)
{
m_InnerEngine->m_Eng_exts.at(n) = m_InnerEng_Ext_MG;
m_InnerEng_Ext_MG = eng_mg;
break;
}
}
m_InnerEngine->m_Eng_exts.push_back(m_InnerEng_Ext_MG);
}
@ -83,10 +95,10 @@ void Engine_CylinderMultiGrid::Init()
boost::thread *t = NULL;
t = new boost::thread( Engine_CylinderMultiGrid_Thread(this,m_startBarrier,m_stopBarrier,&m_Thread_NumTS) );
t = new boost::thread( Engine_CylinderMultiGrid_Thread(this,m_startBarrier,m_stopBarrier,&m_Thread_NumTS, true) );
m_IteratorThread_Group.add_thread( t );
t = new boost::thread( Engine_CylinderMultiGrid_Thread(m_InnerEngine,m_startBarrier,m_stopBarrier,&m_Thread_NumTS) );
t = new boost::thread( Engine_CylinderMultiGrid_Thread(m_InnerEngine,m_startBarrier,m_stopBarrier,&m_Thread_NumTS, false) );
m_IteratorThread_Group.add_thread( t );
}
@ -107,12 +119,6 @@ bool Engine_CylinderMultiGrid::IterateTS(unsigned int iterTS)
return true;
}
void Engine_CylinderMultiGrid::InitExtensions()
{
m_InnerEngine->InitExtensions();
Engine_Multithread::InitExtensions();
}
void Engine_CylinderMultiGrid::InterpolVoltChild2Base(unsigned int rzPlane)
{
//interpolate voltages from child engine to the base engine...
@ -229,11 +235,12 @@ void Engine_CylinderMultiGrid::InterpolCurrChild2Base(unsigned int rzPlane)
}
/****************************************************************************************/
Engine_CylinderMultiGrid_Thread::Engine_CylinderMultiGrid_Thread( Engine_Multithread* engine, boost::barrier *start, boost::barrier *stop, volatile unsigned int* numTS)
Engine_CylinderMultiGrid_Thread::Engine_CylinderMultiGrid_Thread( Engine_Multithread* engine, boost::barrier *start, boost::barrier *stop, volatile unsigned int* numTS, bool isBase)
{
m_startBarrier = start;
m_stopBarrier = stop;
m_Eng=engine;
m_isBase=isBase;
m_numTS = numTS;
}
@ -243,7 +250,10 @@ void Engine_CylinderMultiGrid_Thread::operator()()
while(*m_numTS>0) //m_numTS==0 request to terminate this thread...
{
if (m_isBase)
m_Eng->Engine_Multithread::IterateTS(*m_numTS);
else
m_Eng->IterateTS(*m_numTS);
m_stopBarrier->wait(); //sync all workers after iterations are performed
m_startBarrier->wait(); //wait for Base engine to start the iterations again ...
}

View File

@ -41,8 +41,6 @@ public:
//! Iterate \a iterTS number of timesteps
virtual bool IterateTS(unsigned int iterTS);
virtual void InitExtensions();
protected:
Engine_CylinderMultiGrid(const Operator_CylinderMultiGrid* op);
const Operator_CylinderMultiGrid* Op_CMG;
@ -62,18 +60,18 @@ protected:
boost::barrier *m_WaitOnSync;
Engine_Ext_CylinderMultiGrid* m_Eng_Ext_MG;
Engine_Ext_CylinderMultiGrid* m_InnerEng_Ext_MG;
};
class Engine_CylinderMultiGrid_Thread
{
public:
Engine_CylinderMultiGrid_Thread( Engine_Multithread* engine, boost::barrier *start, boost::barrier *stop, volatile unsigned int* numTS);
Engine_CylinderMultiGrid_Thread( Engine_Multithread* engine, boost::barrier *start, boost::barrier *stop, volatile unsigned int* numTS, bool isBase);
void operator()();
protected:
Engine_Multithread *m_Eng;
bool m_isBase;
boost::barrier *m_startBarrier;
boost::barrier *m_stopBarrier;
volatile unsigned int *m_numTS;

View File

@ -36,6 +36,7 @@ Operator::Operator()
m_MeshType = ProcessFields::CARTESIAN_MESH;
Exc = 0;
dT = 0;
m_InvaildTimestep = false;
}
Operator::~Operator()
@ -535,6 +536,7 @@ int Operator::CalcECOperator()
if (Calc_EC()==0)
return -1;
m_InvaildTimestep = false;
opt_dT = 0;
if (dT>0)
{
@ -542,7 +544,11 @@ int Operator::CalcECOperator()
CalcTimestep();
opt_dT = dT;
if (dT<save_dT)
{
cerr << "Operator::CalcECOperator: Warning, forced timestep: " << save_dT << "s is larger than calculated timestep: " << dT << "s! It is not recommended using this timestep!! " << endl;
m_InvaildTimestep = true;
}
dT = save_dT;
}
else

View File

@ -60,6 +60,7 @@ public:
//! Set a forced timestep to use by the operator
virtual void SetTimestep(double ts) {dT = ts;}
double GetTimestep() const {return dT;};
bool GetTimestepValid() const {return !m_InvaildTimestep;}
virtual double GetNumberCells() const;
//! Returns the number of lines as needed for post-processing etc. (for the engine, use GetOriginalNumLines())
@ -131,6 +132,7 @@ protected:
virtual double CalcTimestep();
double dT; //FDTD timestep!
double opt_dT;
bool m_InvaildTimestep;
string m_Used_TS_Name;
double CalcTimestep_Var1();

View File

@ -21,8 +21,9 @@
Operator_CylinderMultiGrid::Operator_CylinderMultiGrid(vector<double> Split_Radii) : Operator_Cylinder()
{
m_Split_Rad = Split_Radii.back();
Split_Radii.pop_back();
m_Split_Radii = Split_Radii;
m_Split_Rad = m_Split_Radii.back();
m_Split_Radii.pop_back();
}
Operator_CylinderMultiGrid::~Operator_CylinderMultiGrid()
@ -60,6 +61,18 @@ bool Operator_CylinderMultiGrid::SetGeometryCSX(ContinuousStructure* geo)
if (numLines[1]%2 != 1)
{
cerr << "Operator_CylinderMultiGrid::SetGeometryCSX: Error, number of line in alpha direction must be odd... found: " << numLines[1] << endl;
exit(0);
}
//check if mesh is homogenous in alpha-direction
double diff=discLines[1][1]-discLines[1][0];
for (unsigned int n=2;n<numLines[1];++n)
{
if ( fabs((discLines[1][n]-discLines[1][n-1]) - diff)/diff > 1e-10)
{
cerr << "Operator_CylinderMultiGrid::SetGeometryCSX: Error, mesh has to be homogenous in alpha direction for multi grid engine, violation found at: " << n << endl;
exit(0);
}
}
m_Split_Pos = 0;
@ -105,7 +118,11 @@ bool Operator_CylinderMultiGrid::SetGeometryCSX(ContinuousStructure* geo)
void Operator_CylinderMultiGrid::Init()
{
Operator_Cylinder::Init();
if (m_Split_Radii.empty())
m_InnerOp = Operator_Cylinder::New(m_numThreads);
else
m_InnerOp = Operator_CylinderMultiGrid::New(m_Split_Radii,m_numThreads);
}
void Operator_CylinderMultiGrid::CalcStartStopLines(unsigned int &numThreads, vector<unsigned int> &start, vector<unsigned int> &stop) const
@ -132,6 +149,7 @@ void Operator_CylinderMultiGrid::CalcStartStopLines(unsigned int &numThreads, ve
int Operator_CylinderMultiGrid::CalcECOperator()
{
int retCode=0;
if (dT)
m_InnerOp->SetTimestep(dT);
@ -140,12 +158,22 @@ int Operator_CylinderMultiGrid::CalcECOperator()
dT = m_InnerOp->GetTimestep();
retCode = Operator_Cylinder::CalcECOperator();
if (GetTimestepValid()==false)
{
cerr << "Operator_CylinderMultiGrid::CalcECOperator(): Warning, timestep invalid... resetting..." << endl;
dT = opt_dT;
m_InnerOp->SetTimestep(dT);
m_InnerOp->CalcECOperator();
return Operator_Cylinder::CalcECOperator();
}
return retCode;
}
bool Operator_CylinderMultiGrid::SetupExcitation(TiXmlElement* Excite, unsigned int maxTS)
{
if (!m_InnerOp->Exc->setupExcitation(Excite,maxTS))
if (!m_InnerOp->SetupExcitation(Excite,maxTS))
return false;
return Exc->setupExcitation(Excite,maxTS);
}

View File

@ -60,6 +60,7 @@ protected:
virtual void Reset();
double m_Split_Rad;
vector<double> m_Split_Radii;
unsigned int m_Split_Pos;
Operator_Cylinder* m_InnerOp;

View File

@ -0,0 +1,155 @@
close all
clear
clc
%example for an cylindrical mesh, modeling a coaxial cable
% this example is using a multi-grid approach
%% setup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Settings = [];
Settings.LogFile = 'openEMS.log';
physical_constants
f0 = 0.5e9;
epsR = 1; %material filling
length = 1000;
port_dist = length/2;
rad_i = 10; %inner radius
rad_a = 200; %outer radius
partial = 0.5; %e.g. 0.5 means only one half of a coax, should be <1 or change boundary cond.
max_mesh = 10 / sqrt(epsR);
max_alpha = max_mesh;
N_alpha = ceil(rad_a * 2*pi * partial / max_alpha);
%make it even...
N_alpha = N_alpha + mod(N_alpha,2);
%make sure it is multiple of 4, needed for 2 multi-grid steps
N_alpha = ceil((N_alpha)/4) *4 + 1;
openEMS_opts = '';
% openEMS_opts = [openEMS_opts ' --disable-dumps'];
% openEMS_opts = [openEMS_opts ' --debug-material'];
% openEMS_opts = [openEMS_opts ' --numThreads=1'];
def_refSimu = 0; % do a reference simulation without the multi-grid
%% setup done %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (def_refSimu>0)
Sim_Path = 'tmp_ref';
else
Sim_Path = 'tmp';
end
Sim_CSX = 'coax.xml';
if (exist(Sim_Path,'dir'))
rmdir(Sim_Path,'s');
end
mkdir(Sim_Path);
%setup FDTD parameter
if (def_refSimu>0)
FDTD = InitCylindricalFDTD(1e5,1e-5,'OverSampling',5 );
else
FDTD = InitCylindricalFDTD(1e5,1e-5,'OverSampling',5 ,'MultiGrid','60,120');
end
FDTD = SetGaussExcite(FDTD,f0,f0);
BC = [0 0 1 1 2 2];
FDTD = SetBoundaryCond(FDTD,BC);
mesh_res = [max_mesh 2*pi*partial/N_alpha max_mesh];
%setup CSXCAD geometry
CSX = InitCSX();
mesh.x = SmoothMeshLines([rad_i rad_a],mesh_res(1));
mesh.y = linspace(-pi*partial,pi*partial,N_alpha);
mesh.z = SmoothMeshLines([0 port_dist length],mesh_res(3));
CSX = DefineRectGrid(CSX, 1e-3,mesh);
start = [rad_i mesh.y(1) mesh.z(3)];
stop = [rad_a mesh.y(end) mesh.z(3)];
CSX = AddExcitation(CSX,'excite',0,[1 0 0]);
weight{1} = '1/rho';
weight{2} = 0;
weight{3} = 0;
CSX = SetExcitationWeight(CSX, 'excite', weight );
CSX = AddBox(CSX,'excite',0 ,start,stop);
start = [mesh.x(1) mesh.y(1) mesh.z(1)];
stop = [mesh.x(end) mesh.y(end) mesh.z(end)];
CSX = AddMaterial(CSX,'material');
CSX = SetMaterialProperty(CSX,'material','Epsilon',epsR);
CSX = AddBox(CSX,'material',0 ,start,stop);
%dump
CSX = AddDump(CSX,'Et_rz_','DumpMode',0);
start = [mesh.x(1) 0 mesh.z(1)];
stop = [mesh.x(end) 0 mesh.z(end)];
CSX = AddBox(CSX,'Et_rz_',0 , start,stop);
CSX = AddDump(CSX,'Ht_rz_','DumpType',1,'DumpMode',0);
CSX = AddBox(CSX,'Ht_rz_',0 , start,stop);
CSX = AddDump(CSX,'Et_','DumpType',0,'DumpMode',0);
start = [mesh.x(1) mesh.y(1) length/2];
stop = [mesh.x(end) mesh.y(end) length/2];
CSX = AddBox(CSX,'Et_',0,start,stop);
CSX = AddDump(CSX,'Ht_','DumpType',1,'DumpMode',0);
start = [mesh.x(1) mesh.y(1) length/2];
stop = [mesh.x(end) mesh.y(end) length/2];
CSX = AddBox(CSX,'Ht_',0,start,stop);
% voltage calc (take a voltage average to be at the same spot as the
% current calculation)
CSX = AddProbe(CSX,'ut1_1',0);
start = [ rad_i 0 port_dist ];stop = [ rad_a 0 port_dist ];
CSX = AddBox(CSX,'ut1_1', 0 ,start,stop);
CSX = AddProbe(CSX,'ut1_2',0);
start = [ rad_i 0 port_dist+mesh_res(3) ];stop = [ rad_a 0 port_dist+mesh_res(3) ];
CSX = AddBox(CSX,'ut1_2', 0 ,start,stop);
% current calc
CSX = AddProbe(CSX,'it1',1);
mid = 75;
start = [ 0 mesh.y(1) port_dist+mesh_res(3)/2 ];stop = [ mid mesh.y(end) port_dist+mesh_res(3)/2 ];
CSX = AddBox(CSX,'it1', 0 ,start,stop);
%% Write openEMS compatoble xml-file %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
WriteOpenEMS([Sim_Path '/' Sim_CSX],FDTD,CSX);
RunOpenEMS(Sim_Path, Sim_CSX, openEMS_opts, Settings)
%%
close all
freq = linspace(0,2*f0,201);
UI = ReadUI({'ut1_1','ut1_2','it1'},Sim_Path,freq);
u_f = (UI.FD{1}.val + UI.FD{2}.val)/2; %averaging voltages to fit current
i_f = UI.FD{3}.val / partial;
% plot(UI.TD{1}.t,UI.TD{1}.val);
% grid on;
%
% figure
% plot(UI.TD{3}.t,UI.TD{3}.val);
% grid on;
%plot Z_L compare
figure
ZL = Z0/2/pi/sqrt(epsR)*log(rad_a/rad_i); %analytic line-impedance of a coax
plot(UI.FD{1}.f,ZL*ones(size(u_f)),'g','Linewidth',3);
hold on;
grid on;
Z = u_f./i_f;
plot(UI.FD{1}.f,real(Z),'k--','Linewidth',2);
plot(UI.FD{1}.f,imag(Z),'r-','Linewidth',2);
xlim([0 2*f0]);
legend('Z_L - analytic','\Re\{Z\} - FDTD','\Im\{Z\} - FDTD','Location','Best');