2010-04-09 13:51:37 +00:00
/*
* Copyright ( C ) 2010 Thorsten Liebig ( Thorsten . Liebig @ gmx . de )
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2010-05-08 16:12:44 +00:00
# include "engine.h"
2012-06-06 08:25:40 +00:00
# include "engine_cylinder.h"
2010-12-06 09:44:25 +00:00
# include "Common/processfields.h"
2010-04-09 13:51:37 +00:00
# include "operator_cylinder.h"
2010-12-06 14:30:47 +00:00
# include "extensions/operator_extension.h"
# include "extensions/operator_ext_cylinder.h"
2010-04-09 13:51:37 +00:00
2010-06-05 23:50:58 +00:00
Operator_Cylinder * Operator_Cylinder : : New ( unsigned int numThreads )
2010-04-09 13:51:37 +00:00
{
2010-05-19 19:25:15 +00:00
cout < < " Create cylindrical FDTD operator " < < endl ;
2010-04-09 13:51:37 +00:00
Operator_Cylinder * op = new Operator_Cylinder ( ) ;
2010-06-05 23:50:58 +00:00
op - > setNumThreads ( numThreads ) ;
2010-04-09 13:51:37 +00:00
op - > Init ( ) ;
return op ;
}
2010-08-15 12:11:42 +00:00
Operator_Cylinder : : Operator_Cylinder ( ) : Operator_Multithread ( )
2010-04-09 13:51:37 +00:00
{
2011-08-16 15:03:57 +00:00
m_MeshType = CYLINDRICAL ;
2012-06-06 08:29:57 +00:00
m_Cyl_Ext = NULL ;
2010-04-09 13:51:37 +00:00
}
Operator_Cylinder : : ~ Operator_Cylinder ( )
{
2010-05-08 16:12:44 +00:00
}
2012-06-06 08:25:40 +00:00
Engine * Operator_Cylinder : : CreateEngine ( ) const
{
//! create a special cylindrical-engine
Engine_Cylinder * eng = Engine_Cylinder : : New ( this , m_numThreads ) ;
return eng ;
}
2010-04-09 13:51:37 +00:00
void Operator_Cylinder : : Init ( )
{
CC_closedAlpha = false ;
CC_R0_included = false ;
2010-08-15 12:11:42 +00:00
Operator_Multithread : : Init ( ) ;
2010-04-09 13:51:37 +00:00
}
2011-03-18 13:17:09 +00:00
double Operator_Cylinder : : GetRawDiscDelta ( int ny , const int pos ) const
{
if ( CC_closedAlpha & & ny = = 1 & & pos = = - 1 )
{
// cerr << (discLines[1][numLines[1]-2] - discLines[1][numLines[1]-3]) << " vs " << Operator_Multithread::GetRawDiscDelta(ny,pos) << endl;
return ( discLines [ 1 ] [ numLines [ 1 ] - 2 ] - discLines [ 1 ] [ numLines [ 1 ] - 3 ] ) ;
}
return Operator_Multithread : : GetRawDiscDelta ( ny , pos ) ;
}
double Operator_Cylinder : : GetMaterial ( int ny , const double * coords , int MatType , bool markAsUsed ) const
{
double l_coords [ ] = { coords [ 0 ] , coords [ 1 ] , coords [ 2 ] } ;
if ( CC_closedAlpha & & ( coords [ 1 ] > GetDiscLine ( 1 , 0 , false ) + 2 * PI ) )
l_coords [ 1 ] - = 2 * PI ;
if ( CC_closedAlpha & & ( coords [ 1 ] < GetDiscLine ( 1 , 0 , false ) ) )
l_coords [ 1 ] + = 2 * PI ;
return Operator_Multithread : : GetMaterial ( ny , l_coords , MatType , markAsUsed ) ;
}
2010-10-27 12:49:16 +00:00
int Operator_Cylinder : : CalcECOperator ( DebugFlags debugFlags )
{
// debugs only work with the native vector dumps
bool natDump = g_settings . NativeFieldDumps ( ) ;
g_settings . SetNativeFieldDumps ( true ) ;
int rc = Operator_Multithread : : CalcECOperator ( debugFlags ) ;
// reset original settings
g_settings . SetNativeFieldDumps ( natDump ) ;
return rc ;
}
2013-02-06 15:33:12 +00:00
inline unsigned int Operator_Cylinder : : GetNumberOfLines ( int ny , bool full ) const
2010-04-11 21:52:38 +00:00
{
2013-02-06 15:33:12 +00:00
if ( full )
return Operator_Multithread : : GetNumberOfLines ( ny , full ) ;
2010-04-11 21:52:38 +00:00
//this is necessary for a correct field processing... cylindrical engine has to reset this by adding +1
if ( CC_closedAlpha & & ny = = 1 )
2013-02-06 15:33:12 +00:00
return Operator_Multithread : : GetNumberOfLines ( ny , true ) - 2 ;
2010-04-11 21:52:38 +00:00
2013-02-06 15:33:12 +00:00
return Operator_Multithread : : GetNumberOfLines ( ny , full ) ;
2010-04-11 21:52:38 +00:00
}
2010-04-23 06:17:42 +00:00
string Operator_Cylinder : : GetDirName ( int ny ) const
{
if ( ny = = 0 ) return " rho " ;
if ( ny = = 1 ) return " alpha " ;
if ( ny = = 2 ) return " z " ;
return " " ;
}
2011-03-16 11:26:41 +00:00
bool Operator_Cylinder : : GetYeeCoords ( int ny , unsigned int pos [ 3 ] , double * coords , bool dualMesh ) const
{
2012-06-06 08:19:30 +00:00
bool ret = Operator_Multithread : : GetYeeCoords ( ny , pos , coords , dualMesh ) ;
if ( CC_closedAlpha & & ( coords [ 1 ] > = GetDiscLine ( 1 , 0 , false ) + 2 * PI ) )
2011-03-16 11:26:41 +00:00
coords [ 1 ] - = 2 * PI ;
2011-03-18 13:17:09 +00:00
if ( CC_closedAlpha & & ( coords [ 1 ] < GetDiscLine ( 1 , 0 , false ) ) )
coords [ 1 ] + = 2 * PI ;
2011-03-16 11:26:41 +00:00
2012-06-06 08:19:30 +00:00
return ret ;
2011-03-16 11:26:41 +00:00
}
2010-09-17 12:51:07 +00:00
double Operator_Cylinder : : GetNodeWidth ( int ny , const unsigned int pos [ 3 ] , bool dualMesh ) const
2010-09-02 13:35:13 +00:00
{
if ( ( ny < 0 ) | | ( ny > 2 ) ) return 0.0 ;
2010-09-17 12:51:07 +00:00
if ( pos [ ny ] > = numLines [ ny ] ) return 0.0 ;
2010-12-08 15:55:27 +00:00
double width = Operator_Multithread : : GetEdgeLength ( ny , pos , ! dualMesh ) ;
2010-09-02 13:35:13 +00:00
if ( ny = = 1 )
width * = GetDiscLine ( 0 , pos [ 0 ] , dualMesh ) ;
return width ;
}
2011-03-18 13:17:09 +00:00
double Operator_Cylinder : : GetNodeWidth ( int ny , const int pos [ 3 ] , bool dualMesh ) const
{
if ( ( pos [ 0 ] < 0 ) | | ( pos [ 1 ] < 0 & & CC_closedAlpha = = false ) | | ( pos [ 2 ] < 0 ) )
return 0.0 ;
2012-11-15 20:44:43 +00:00
unsigned int uiPos [ ] = { ( unsigned int ) pos [ 0 ] , ( unsigned int ) pos [ 1 ] , ( unsigned int ) pos [ 2 ] } ;
2011-03-18 13:17:09 +00:00
if ( pos [ 1 ] < 0 & & CC_closedAlpha = = true )
uiPos [ 1 ] + = numLines [ 1 ] - 2 ;
return GetNodeWidth ( ny , uiPos , dualMesh ) ;
}
2010-09-17 12:51:07 +00:00
double Operator_Cylinder : : GetNodeArea ( int ny , const unsigned int pos [ 3 ] , bool dualMesh ) const
2010-07-29 16:30:50 +00:00
{
2010-09-17 12:51:07 +00:00
if ( pos [ ny ] > = numLines [ ny ] ) return 0.0 ;
if ( pos [ 0 ] > = numLines [ 0 ] ) return 0.0 ;
2010-07-29 16:30:50 +00:00
if ( ny = = 2 )
{
2010-12-08 15:55:27 +00:00
double da = Operator_Multithread : : GetEdgeLength ( 1 , pos , dualMesh ) / gridDelta ;
2010-07-29 16:30:50 +00:00
double r1 , r2 ;
2010-12-08 15:55:27 +00:00
if ( dualMesh )
2010-07-29 16:30:50 +00:00
{
2010-12-08 15:55:27 +00:00
r1 = GetDiscLine ( 0 , pos [ 0 ] , false ) * gridDelta ;
r2 = r1 + GetEdgeLength ( 0 , pos , false ) ;
2010-07-29 16:30:50 +00:00
}
else
{
2010-12-08 15:55:27 +00:00
r2 = GetDiscLine ( 0 , pos [ 0 ] , ! dualMesh ) * gridDelta ;
r1 = r2 - GetEdgeLength ( 0 , pos , true ) ;
2010-07-29 16:30:50 +00:00
}
2010-09-02 13:35:13 +00:00
2010-12-08 15:55:27 +00:00
if ( r1 < = 0 )
return da / 2 * pow ( r2 , 2 ) ;
2010-09-02 13:35:13 +00:00
else
2010-12-08 15:55:27 +00:00
return da / 2 * ( pow ( r2 , 2 ) - pow ( r1 , 2 ) ) ;
2010-09-02 13:35:13 +00:00
}
2010-12-08 15:55:27 +00:00
2010-08-15 12:11:42 +00:00
return Operator_Multithread : : GetNodeArea ( ny , pos , dualMesh ) ;
2010-07-29 16:30:50 +00:00
}
2010-04-11 21:52:38 +00:00
2011-03-18 13:17:09 +00:00
double Operator_Cylinder : : GetNodeArea ( int ny , const int pos [ 3 ] , bool dualMesh ) const
{
if ( ( pos [ 0 ] < 0 ) | | ( pos [ 1 ] < 0 & & CC_closedAlpha = = false ) | | ( pos [ 2 ] < 0 ) )
return 0.0 ;
2012-11-15 20:44:43 +00:00
unsigned int uiPos [ ] = { ( unsigned int ) pos [ 0 ] , ( unsigned int ) pos [ 1 ] , ( unsigned int ) pos [ 2 ] } ;
2011-03-18 13:17:09 +00:00
if ( pos [ 1 ] < 0 & & CC_closedAlpha = = true )
uiPos [ 1 ] + = numLines [ 1 ] - 2 ;
return GetNodeArea ( ny , uiPos , dualMesh ) ;
}
2010-09-17 12:51:07 +00:00
double Operator_Cylinder : : GetEdgeLength ( int ny , const unsigned int pos [ 3 ] , bool dualMesh ) const
2010-09-02 13:35:13 +00:00
{
2010-12-08 15:55:27 +00:00
double length = Operator_Multithread : : GetEdgeLength ( ny , pos , dualMesh ) ;
2010-09-02 13:35:13 +00:00
if ( ny ! = 1 )
return length ;
return length * GetDiscLine ( 0 , pos [ 0 ] , dualMesh ) ;
}
2012-04-27 14:31:36 +00:00
double Operator_Cylinder : : GetCellVolume ( const unsigned int pos [ 3 ] , bool dualMesh ) const
{
return GetEdgeArea ( 2 , pos , dualMesh ) * GetEdgeLength ( 2 , pos , dualMesh ) ;
}
2010-09-17 12:51:07 +00:00
double Operator_Cylinder : : GetEdgeArea ( int ny , const unsigned int pos [ 3 ] , bool dualMesh ) const
2010-09-02 13:35:13 +00:00
{
if ( ny ! = 0 )
return GetNodeArea ( ny , pos , dualMesh ) ;
return GetEdgeLength ( 1 , pos , ! dualMesh ) * GetEdgeLength ( 2 , pos , ! dualMesh ) ;
}
2013-02-06 15:33:12 +00:00
double Operator_Cylinder : : FitToAlphaRange ( double a_coord , bool fullMesh ) const
2012-11-21 15:41:53 +00:00
{
double min = GetDiscLine ( 1 , 0 ) ;
2013-02-06 15:33:12 +00:00
double max = GetDiscLine ( 1 , GetNumberOfLines ( 1 , fullMesh ) - 1 ) ;
2012-11-21 15:41:53 +00:00
if ( ( a_coord > = min ) & & ( a_coord < = max ) )
return a_coord ;
while ( a_coord < min )
{
a_coord + = 2 * PI ;
if ( a_coord > max )
return a_coord - 2 * PI ;
if ( a_coord > min )
return a_coord ;
}
while ( a_coord > max )
{
a_coord - = 2 * PI ;
if ( a_coord < min )
return a_coord + 2 * PI ;
if ( a_coord < max )
return a_coord ;
}
// this cannot happen
return a_coord ;
}
2013-02-06 15:33:12 +00:00
unsigned int Operator_Cylinder : : SnapToMeshLine ( int ny , double coord , bool & inside , bool dualMesh , bool fullMesh ) const
2012-11-21 15:41:53 +00:00
{
if ( ny = = 1 )
coord = FitToAlphaRange ( coord ) ;
2013-02-06 15:33:12 +00:00
return Operator_Multithread : : SnapToMeshLine ( ny , coord , inside , dualMesh , fullMesh ) ;
2012-11-21 15:41:53 +00:00
}
2013-02-06 15:33:12 +00:00
int Operator_Cylinder : : SnapBox2Mesh ( const double * start , const double * stop , unsigned int * uiStart , unsigned int * uiStop , bool dualMesh , bool fullMesh , int SnapMethod , bool * bStartIn , bool * bStopIn ) const
2012-11-21 15:41:53 +00:00
{
double a_min = GetDiscLine ( 1 , 0 ) ;
2013-02-06 15:33:12 +00:00
double a_max = GetDiscLine ( 1 , GetNumberOfLines ( 1 , fullMesh ) - 1 ) ;
2012-11-21 15:41:53 +00:00
double a_size = stop [ 1 ] - start [ 1 ] ;
double a_center = FitToAlphaRange ( 0.5 * ( stop [ 1 ] + start [ 1 ] ) ) ;
double a_start = a_center - a_size / 2 ;
double a_stop = a_start + a_size ;
if ( a_stop > a_max )
a_stop = a_max ;
if ( a_stop < a_min )
a_stop = a_min ;
if ( a_start > a_max )
a_start = a_max ;
if ( a_start < a_min )
a_start = a_min ;
double l_start [ 3 ] = { start [ 0 ] , a_start , start [ 2 ] } ;
double l_stop [ 3 ] = { stop [ 0 ] , a_stop , stop [ 2 ] } ;
2013-02-06 15:33:12 +00:00
return Operator_Multithread : : SnapBox2Mesh ( l_start , l_stop , uiStart , uiStop , dualMesh , fullMesh , SnapMethod , bStartIn , bStopIn ) ;
2012-11-21 15:41:53 +00:00
}
2011-03-18 13:17:09 +00:00
bool Operator_Cylinder : : SetupCSXGrid ( CSRectGrid * grid )
2010-04-09 13:51:37 +00:00
{
2011-03-18 13:17:09 +00:00
unsigned int alphaNum ;
double * alphaLines = NULL ;
alphaLines = grid - > GetLines ( 1 , alphaLines , alphaNum , true ) ;
2010-04-09 13:51:37 +00:00
2011-03-18 13:17:09 +00:00
double minmaxA = fabs ( alphaLines [ alphaNum - 1 ] - alphaLines [ 0 ] ) ;
if ( fabs ( minmaxA - 2 * PI ) < OPERATOR_CYLINDER_CLOSED_ALPHA_THRESHOLD )
2010-04-09 13:51:37 +00:00
{
2011-11-08 10:49:14 +00:00
if ( g_settings . GetVerboseLevel ( ) > 0 )
cout < < " Operator_Cylinder::SetupCSXGrid: Alpha is a full 2*PI => closed Cylinder... " < < endl ;
2010-04-11 21:52:38 +00:00
CC_closedAlpha = true ;
2011-03-18 13:17:09 +00:00
grid - > SetLine ( 1 , alphaNum - 1 , 2 * PI + alphaLines [ 0 ] ) ;
grid - > AddDiscLine ( 1 , 2 * PI + alphaLines [ 1 ] ) ;
2010-04-09 13:51:37 +00:00
}
else if ( minmaxA > 2 * PI )
2010-12-06 12:04:37 +00:00
{
2011-03-18 13:17:09 +00:00
cerr < < " Operator_Cylinder::SetupCSXGrid: Alpha Max-Min must not be larger than 2*PI!!! " < < endl ;
2010-12-06 12:04:37 +00:00
Reset ( ) ;
return false ;
}
2010-04-24 12:06:00 +00:00
else
{
CC_closedAlpha = false ;
}
2010-04-09 13:51:37 +00:00
2011-03-28 08:38:48 +00:00
CC_R0_included = false ;
2011-03-18 13:17:09 +00:00
if ( grid - > GetLine ( 0 , 0 ) < 0 )
2010-12-06 12:04:37 +00:00
{
2011-03-18 13:17:09 +00:00
cerr < < " Operator_Cylinder::SetupCSXGrid: r<0 not allowed in Cylinder Coordinates!!! " < < endl ;
2010-12-06 12:04:37 +00:00
Reset ( ) ;
return false ;
}
2011-03-18 13:17:09 +00:00
else if ( grid - > GetLine ( 0 , 0 ) = = 0.0 )
2010-04-09 13:51:37 +00:00
{
2011-11-08 10:49:14 +00:00
if ( g_settings . GetVerboseLevel ( ) > 0 )
cout < < " Operator_Cylinder::SetupCSXGrid: r=0 included... " < < endl ;
2011-03-28 08:38:48 +00:00
CC_R0_included = CC_closedAlpha ; //needed for correct ec-calculation, deactivate if closed cylinder is false... --> E_r = 0 anyways
2011-11-16 11:46:49 +00:00
// use conservative timestep for a mesh including the r==0 singularity
m_TimeStepVar = 1 ;
2011-03-28 08:38:48 +00:00
}
# ifdef MPI_SUPPORT
// Setup an MPI split in alpha direction for a closed cylinder
CC_MPI_Alpha = false ;
if ( ( m_NeighborUp [ 1 ] > = 0 ) | | ( m_NeighborDown [ 1 ] > = 0 ) ) //check for MPI split in alpha direction
{
double minmaxA = 2 * PI ; // fabs(m_OrigDiscLines[1][m_OrigNumLines[1]-1]-m_OrigDiscLines[1][0]);
if ( fabs ( minmaxA - 2 * PI ) < OPERATOR_CYLINDER_CLOSED_ALPHA_THRESHOLD ) //check for closed alpha MPI split
{
CC_MPI_Alpha = true ;
if ( m_OrigDiscLines [ 0 ] [ 0 ] = = 0 )
{
cerr < < " Operator_Cylinder::SetupCSXGrid: Error: MPI split in alpha direction for closed cylinder including r==0 is currently not supported! Exit! " < < endl ;
exit ( - 2 ) ;
}
if ( m_NeighborUp [ 1 ] < 0 ) //check if this process is at the alpha-end
{
grid - > SetLine ( 1 , alphaNum - 1 , 2 * PI + m_OrigDiscLines [ 1 ] [ 0 ] ) ;
grid - > AddDiscLine ( 1 , 2 * PI + m_OrigDiscLines [ 1 ] [ 1 ] ) ;
SetNeighborUp ( 1 , m_ProcTable [ m_ProcTablePos [ 0 ] ] [ 0 ] [ m_ProcTablePos [ 2 ] ] ) ;
}
if ( m_NeighborDown [ 1 ] < 0 ) //check if this process is at the alpha-start
{
SetNeighborDown ( 1 , m_ProcTable [ m_ProcTablePos [ 0 ] ] [ m_SplitNumber [ 1 ] - 1 ] [ m_ProcTablePos [ 2 ] ] ) ;
}
//Note: the process table will not reflect this up/down neighbors necessary for a closed cylinder
}
2010-04-09 13:51:37 +00:00
}
2011-03-28 08:38:48 +00:00
# endif
2010-04-09 13:51:37 +00:00
2011-03-18 13:17:09 +00:00
if ( Operator_Multithread : : SetupCSXGrid ( grid ) = = false )
return false ;
2010-08-23 19:53:29 +00:00
if ( CC_closedAlpha | | CC_R0_included )
2012-06-06 08:29:57 +00:00
{
m_Cyl_Ext = new Operator_Ext_Cylinder ( this ) ;
this - > AddExtension ( m_Cyl_Ext ) ;
}
2010-08-23 19:53:29 +00:00
2010-04-09 13:51:37 +00:00
return true ;
}
void Operator_Cylinder : : ApplyMagneticBC ( bool * dirs )
{
if ( dirs = = NULL ) return ;
if ( CC_closedAlpha )
{
2010-12-06 12:04:37 +00:00
dirs [ 2 ] = 0 ;
dirs [ 3 ] = 0 ; //no PMC in alpha directions...
2010-04-09 13:51:37 +00:00
}
if ( CC_R0_included )
{
2010-04-23 14:31:00 +00:00
dirs [ 0 ] = 0 ; //no PMC in r_min directions...
2010-04-09 13:51:37 +00:00
}
2010-08-15 12:11:42 +00:00
Operator_Multithread : : ApplyMagneticBC ( dirs ) ;
2010-04-09 13:51:37 +00:00
}
2010-05-03 20:37:29 +00:00
void Operator_Cylinder : : AddExtension ( Operator_Extension * op_ext )
{
2012-02-10 10:55:55 +00:00
if ( op_ext - > IsCylinderCoordsSave ( CC_closedAlpha , CC_R0_included ) )
2012-09-17 14:54:55 +00:00
Operator_Multithread : : AddExtension ( op_ext ) ;
2010-05-03 20:37:29 +00:00
else
2012-09-17 14:54:55 +00:00
{
2010-05-03 20:37:29 +00:00
cerr < < " Operator_Cylinder::AddExtension: Warning: Operator extension \" " < < op_ext - > GetExtensionName ( ) < < " \" is not compatible with cylinder-coords!! skipping...! " < < endl ;
2012-10-29 13:51:02 +00:00
delete op_ext ;
2012-09-17 14:54:55 +00:00
}
2010-05-03 20:37:29 +00:00
}