// Geometry Setup

h = 0.595; // Filling level
r = 0.317; // Radius Cylinder

// Electrode Setup

s_h = 0.05; // Horizontal electrode spacing
s_v = 0.1; // Vertical electrode spacing

// Meshing

k = r/s_h; // Number of circles to define tubular volumes
z = h/s_v; // Number of Layers 
Point(1) = {r,r,h}; // Midpoint
a = r; // Distance from midpoint to created circle
b = 0.3; // Bumping factor

For i In {0:k} // Creates k+1 circles using 5 points (including midpoint)
  
  a = r-s_h*i; // Distance from midpoint reduces according to the horizontal electrode spacing
  
  // Definition of four points with distance a from the midpoint
  p2[i] = newp; Point(p2[i]) = {r, r+a, h};
  p3[i] = newp; Point(p3[i]) = {r-a, r, h};
  p4[i] = newp; Point(p4[i]) = {r+a, r, h};
  p5[i] = newp; Point(p5[i]) = {r, r-a, h};

  // Creation of four circle arcs
  c1[i] = newreg; Circle(c1[i]) = {p2[i], 1, p4[i]};
  c2[i] = newreg; Circle(c2[i]) = {p4[i], 1, p5[i]};
  c3[i] = newreg; Circle(c3[i]) = {p5[i], 1, p3[i]};
  c4[i] = newreg; Circle(c4[i]) = {p3[i], 1, p2[i]};

  l[i] = newll; Line Loop(l[i]) = {c1[i], c2[i], c3[i], c4[i]}; // Connects circle arcs
  
  Transfinite Line {c1[i],c2[i],c3[i],c4[i]} = ((Pi*a)/(2*s_h)) Using Bump b; // Sets the number of nodes per quadrant
EndFor

surf[k] = news; Plane Surface(surf[k]) = {l[k]}; // Creates a surface for the inner circle

For i In {0:k-1}
  surf[i] = news; Plane Surface(surf[i]) = {l[i],l[i+1]}; // Creates surfaces between the other circles
EndFor

Extrude {0, 0, -h} { // Extrudes all surfaces in negative z-direction to create a cylindrical volume
  Surface{surf[]};Layers{z}; Recombine; // Recombines tetrahedra to triangular prisms 
}

Physical Volume(1) = {1:k+1}; // Defines one physical volume made of all tubular subvolumes


