
// Note : this file defines macros used to generate the geometry of the
// Eucard coil. To open the model, you need to run 2D_eucard.pro.

// ----------------------------------------------------------------- //
//                   __  ___                                         //
//                  /  |/  /___  ______________  _____               //
//                 / /|_/ / __ `/ ___/ ___/ __ \/ ___/               //
//                / /  / / /_/ / /__/ /  / /_/ (__  )                //
//               /_/  /_/\__,_/\___/_/   \____/____/                 //
//                                                                   //
// ----------------------------------------------------------------- //

// Macro generating geometry for a single conductor ---------------- //

Macro MakeConductor
  
  // Generate the geometry for a simple tape of superconductor

  x1 = xTurnCorner + InsThick + CuBeThick + HastThick ;
  x2 = x1          + SupraThick ;
  x3 = x2          + CuThick ;
  x4 = x3          + SupraThick ;
  x5 = x4          + HastThick + CuBeThick + InsThick ;
  y1 = yTurnCorner ;
  y2 = yTurnCorner +    alpha  * CondWidth ;
  y3 = yTurnCorner + (1-alpha) * CondWidth ;
  y4 = yTurnCorner + CondWidth ;

  pc = newp ;
  Point( pc    ) = { x1 , y1 , 0. } ;
  Point( pc+1  ) = { x2 , y1 , 0. } ;
  Point( pc+2  ) = { x2 , y4 , 0. } ;
  Point( pc+3  ) = { x1 , y4 , 0. } ;
  Point( pc+4  ) = { x3 , y1 , 0. } ;
  Point( pc+5  ) = { x4 , y1 , 0. } ;
  Point( pc+6  ) = { x4 , y4 , 0. } ;
  Point( pc+7  ) = { x3 , y4 , 0. } ;
  
  lc = newl ;
  Line( lc   ) = { pc    , pc+1  } ;
  Line( lc+1 ) = { pc+1  , pc+2  } ;
  Line( lc+2 ) = { pc+2  , pc+3  } ;
  Line( lc+3 ) = { pc+3  , pc    } ;
  Line( lc+4 ) = { pc+4  , pc+5  } ;
  Line( lc+5 ) = { pc+5  , pc+6  } ;
  Line( lc+6 ) = { pc+6  , pc+7  } ;
  Line( lc+7 ) = { pc+7  , pc+4  } ;
  
  llc = newll ;
  Line Loop( llc   ) = {   lc:lc+3 } ;
  Line Loop( llc+1 ) = { lc+4:lc+7 } ;

  sc = news ;
  Plane Surface( sc   ) = { llc   } ;
  Plane Surface( sc+1 ) = { llc+1 } ;

  // Set the conductor to be meshed with the transfinite algorithm
  // in order to get a structured mesh
  Transfinite Line { lc   , -(lc+2) , lc+4 , -(lc+6) } = 1 ;
  Transfinite Line { lc+1 , -(lc+3) , lc+5 , -(lc+7) } =
    ( CondMeshDivs + 1 ) Using Bump ( 1. / CondMeshBump ) ;
  Transfinite Surface { sc , sc+1 } ;
  Recombine Surface { sc , sc+1 } ;

  // Store the elementary geometry

  lSupra[]  += { lc:lc+7     } ;
  llSupra[] += { llc , llc+1 } ;
  sSupra[]  += { sc , sc+1   } ;
  
  If ( iTurn < ( NbTurnsBlock[ iBlock ] - 1 ) )

    Point( pc+8  ) = { x5 , y1 , 0. , MeshCarLen1 } ;
    Point( pc+9  ) = { x5 , y2 , 0. , MeshCarLen1 } ;
    Point( pc+10 ) = { x5 , y3 , 0. , MeshCarLen1 } ;
    Point( pc+11 ) = { x5 , y4 , 0. , MeshCarLen1 } ;

    Line( lc+8 ) = { pc+8  , pc+9  } ;
    Line( lc+9 ) = { pc+10 , pc+11 } ;

    lMeshC[] += { lc+8 , lc+9 } ;
  
  EndIf

Return

// Macro generating a block of conductor --------------------------- //

Macro MakeBlock

  xTurnCorner = xBlockCorner ;
  yTurnCorner = yBlockCorner ;

  x1 = xBlockCorner + InsThick + CuBeThick + HastThick ;
  x2 = xBlockCorner + CondThick * NbTurnsBlock[ iBlock ] 
         - InsThick - CuBeThick - HastThick ;
  y1 = yBlockCorner + CondWidth + SpacerThick/2. ;
    
  pc = newp ;
  Point( pc   ) = { x1 , y1 , 0. , MeshCarLen2 } ;
  Point( pc+1 ) = { x2 , y1 , 0. , MeshCarLen2 } ;
      
  lc = newl ;
  Line( lc ) = { pc , pc+1 } ;
  lMeshC[] += { lc } ;
      
  // Compute the location of the bottom-left corner of the next tape
  // to generate
  For iTurn In { 0:(NbTurnsBlock[ iBlock ] - 1 ) }
    Call MakeConductor ;
     
    xTurnCorner += CondThick ;
    yTurnCorner += 0.        ;

  EndFor

Return

// Macro building the top-rigth quarter of the coil ---------------- //

Macro MakeCoil

  // Parameters used to contraol the mesh density in the air

  alpha = 0.05 ;
  MeshCarLen1 = ( InsThick + CuBeThick + HastThick ) / 3. ;
  MeshCarLen2 = SpacerThick / 3. ;

  // Prepare lists to store the elementary geometry identifiers
  
  sSupra[]  = {} ; // ReBCO surface identifiers
  lSupra[]  = {} ; // IDs of lines making the conductor skin
  llSupra[] = {} ; // Line loops around the conductors
  lMeshC[]  = {} ; // Lines used to control mesh generation

  // Generate coil blocks by repetitively calling MakeBlock

  Printf( "Generating coil (this may take a while)..." ) ;
  For iBlock In { 0:(#NbTurnsBlock[]-1) }
    xBlockCorner = PoleWidth ;
    yBlockCorner = iBlock * ( CondWidth + SpacerThick )    -
                   (#NbTurnsBlock[]/2)       * CondWidth   -
		   (#NbTurnsBlock[]/2 - 0.5) * SpacerThick ;
    Call MakeBlock ;
  EndFor
  Printf( "Coil generated !" ) ;
  
  // Monitoring output

  Printf( "Counting : ----------------------" ) ;
  Printf( " - %g superconducting surface(s) " , #sSupra[] ) ;
  Printf( "---------------------------------" ) ;

Return
