// File: /workspace/github_c_makefile_star2_O2/radforddc[P]icpc_siggen/icpc_fieldgen_opt.c
// Function: ev_relax_undep
int ev_relax_undep(MJD_Siggen_Setup *setup) {
  int    old = 1, new = 0, iter, r, z, bvn;
  float  grid = setup->xtal_grid;
  int    L  = lrint(setup->xtal_length/grid)+2;
  int    R  = lrint(setup->xtal_radius/grid)+2;
  double eps_sum, v_sum, save_dif, min;
  double dif, sum_dif=0, max_dif, bubble_volts=0;
  double ***v = setup->v, **eps_dr = setup->eps_dr, **eps_dz = setup->eps_dz;
  double ***dr = setup->dr, ***dz = setup->dz;
  double *s1 = setup->s1, *s2 = setup->s2;
  char   **undep = setup->undepleted;
  double e_over_E = 11.310; // e/epsilon; for 1 mm2, charge units 1e10 e/cm3, espilon = 16*epsilon0


  // save impurity value along passivated surface
  for (r = 1; r < R; r++)
    setup->impurity[0][r] = setup->impurity[1][r];

  /* initialise the undepleted array for use with bubble depletion */
  for (z = 1; z < L; z++) {
    for (r = 1; r < R; r++) {
      if (setup->point_type[z][r] >= INSIDE) undep[r][z] = 0;
    }
  }

  for (iter = 0; iter < setup->max_iterations; iter++) {

    double OR_fact = ((0.997 - 300.0/(L*R)) * (1.0 - 0.9/(double)(1+iter/6)));
    if (300.0/(L*R) > 0.5) OR_fact = (0.5 * (1.0 - 0.9/(double)(1+iter/6)));
    if (iter < 2) OR_fact = 0.0;

    old = new;
    new = 1 - new;
    sum_dif = 0;
    max_dif = 0;
    bubble_volts = 0;
    bvn = 0;

    if (setup->vacuum_gap > 0) {   // modify impurity value along passivated surface
      for (r = 1; r < R; r++)      //   due to surface charge induced by capacitance
        setup->impurity[1][r] = setup->impurity[0][r] -
          v[old][1][r] * 5.52e-4 * e_over_E * grid / setup->vacuum_gap;
    }

    /* start from z=1 and r=1 so that (z,r)=0 can be
       used for reflection symmetry around r=0 or z=0 */
    for (z = 1; z < L; z++) {
      /* manage r=0 reflection symmetry */
      setup->v[old][z][0] = setup->v[old][z][2];

      for (r = 1; r < R; r++) {
        if (setup->point_type[z][r] < INSIDE) continue;   // HV or point contact
        save_dif = v[old][z][r] - v[new][z][r];      // step difference from previous iteration

        if (setup->point_type[z][r] < DITCH) {       // normal bulk or passivated surface, no complications
          v_sum = (v[old][z+1][r] + v[old][z][r+1]*s1[r] +
                   v[old][z-1][r] + v[old][z][r-1]*s2[r]);
          if (r > 1) eps_sum = 4;
          else       eps_sum = 2 + s1[r] + s2[r];
        } else if (setup->point_type[z][r] == CONTACT_EDGE) {  // adjacent to the contact
          v_sum = (v[old][z+1][r]*dz[1][z][r] + v[old][z][r+1]*dr[1][z][r] +
                   v[old][z-1][r]*dz[0][z][r] + v[old][z][r-1]*dr[0][z][r]);
          eps_sum = dz[1][z][r] + dr[1][z][r] + dz[0][z][r] + dr[0][z][r];
        } else if (setup->point_type[z][r] >= DITCH) {  // in or adjacent to the ditch
          v_sum = (v[old][z+1][r]*eps_dz[z  ][r] + v[old][z][r+1]*eps_dr[z][r  ]*s1[r] +
                   v[old][z-1][r]*eps_dz[z-1][r] + v[old][z][r-1]*eps_dr[z][r-1]*s2[r]);
          eps_sum = (eps_dz[z][r]   + eps_dr[z][r]  *s1[r] +
                     eps_dz[z-1][r] + eps_dr[z][r-1]*s2[r]);
        }

        // calculate the interpolated mean potential and the effect of the space charge
        min = fminf(fminf(v[old][z+1][r], v[old][z][r+1]),
                    fminf(v[old][z-1][r], v[old][z][r-1]));
        v[new][z][r] = v_sum / eps_sum + setup->impurity[z][r];

        undep[r][z] /= 2;
        if (v[new][z][r] <= 0) {
          v[new][z][r] = 0;
          undep[r][z] = 4;  // do not do over-relaxation for 3 iterations
        } else if (v[new][z][r] <= min) {
          if (bubble_volts == 0) bubble_volts = min + 0.2*grid*grid; // finer grids require smaller increment here
          v[new][z][r] = bubble_volts;
          bvn++;
          undep[r][z] = 8;  // do not do over-relaxation for 4 iterations
        }

        // calculate difference from last iteration, for convergence check
        dif = v[old][z][r] - v[new][z][r];
        if (dif < 0) dif = -dif;
        sum_dif += dif;
        if (max_dif < dif) max_dif = dif;
        // do over-relaxation
        if (!undep[r][z])  v[new][z][r] += OR_fact*save_dif;
      }
    }

    // report results for some iterations
    if (iter < 10 || (iter < 600 && iter%100 == 0) || iter%1000 == 0) {
      if (0) {
        printf("%5d %d %d %.10f %.10f\n", iter, old, new, max_dif, sum_dif/(L-2)/(R-2));
      } else {
        printf("%5d %d %d %.10f %.10f ; %.10f %.10f bubble %.2f %d\n",
               iter, old, new, max_dif, sum_dif/(L-2)/(R-2),
               v[new][L/2][R/2], v[new][L/3][R/3], bubble_volts, bvn);
      }
    }
    // check for convergence
    if (max_dif < 0.00000008) break;
 
  }
  printf(">> %d %.16f\n\n", iter, sum_dif);

  setup->bubble_volts = bubble_volts;
  setup->fully_depleted = 1;
  for (r=1; r<R; r++) {
    for (z=1; z<L; z++) {
      if (setup->point_type[z][r] < INSIDE) {
        undep[r][z] = ' ';
      } else if (undep[r][z] == 0) {
        undep[r][z] = '.';
      } else {
        if (undep[r][z] > 4) undep[r][z] = 'B';  // identifies pinch-off
        else undep[r][z] = '*';
        setup->fully_depleted = 0;
      }
    }
  }

  if (setup->vacuum_gap > 0) {   // restore impurity value along passivated surface
    for (r = 1; r < R; r++)
      setup->impurity[1][r] = setup->impurity[0][r];
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
movss 0x4(%rdi), %xmm0
movss 0x54(%rdi), %xmm1
movss %xmm1, (%rsp)
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, %r13
movss 0x8(%rbx), %xmm0
divss (%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
leal 0x2(%rax), %ecx
movq 0x3f8(%rbx), %rdx
movq %rdx, 0xc8(%rsp)
movq 0x400(%rbx), %rdx
movq %rdx, 0xc0(%rsp)
movq 0x418(%rbx), %rdx
movq %rdx, 0x68(%rsp)
movq 0x420(%rbx), %rdx
movq %rdx, 0x60(%rsp)
movq 0x438(%rbx), %r15
movslq %ecx, %r14
pushq $0x1
popq %rdx
cmpq %r14, %rdx
jge 0x5272
movq 0x408(%rbx), %rsi
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
movsd (%rsi,%rdx,8), %xmm0
movsd %xmm0, (%rdi,%rdx,8)
incq %rdx
jmp 0x5250
leal 0x2(%r13), %esi
movslq %esi, %rbp
pushq $0x1
popq %rdx
movq %rdx, %rdi
cmpq %rbp, %rdi
jge 0x52b1
movq %rdx, %r8
cmpq %r14, %r8
jge 0x52ac
movq 0x430(%rbx), %r9
movq (%r9,%rdi,8), %r9
cmpb $0x2, (%r9,%r8)
jl 0x52a7
movq (%r15,%r8,8), %r9
movb $0x0, (%r9,%rdi)
incq %r8
jmp 0x5287
incq %rdi
jmp 0x527f
movl %ecx, %edx
imull %esi, %edx
cvtsi2sd %edx, %xmm0
movsd 0x2de6(%rip), %xmm1     # 0x80a8
divsd %xmm0, %xmm1
movsd 0x2de2(%rip), %xmm0     # 0x80b0
subsd %xmm1, %xmm0
movsd 0x2d96(%rip), %xmm2     # 0x8070
movapd %xmm2, %xmm3
cmpltsd %xmm1, %xmm3
movapd %xmm3, %xmm1
andnpd %xmm0, %xmm1
andpd %xmm2, %xmm3
orpd %xmm1, %xmm3
movapd %xmm3, 0xe0(%rsp)
cvtss2sd (%rsp), %xmm0
movsd 0x2d6f(%rip), %xmm1     # 0x8078
mulsd %xmm0, %xmm1
cvtsi2sd %r13d, %xmm2
movsd %xmm2, 0xa0(%rsp)
cvtsi2sd %eax, %xmm2
movsd %xmm2, 0x98(%rsp)
pushq $0x2
popq %rdi
movl %esi, %eax
cltd
idivl %edi
cltq
movq %rax, 0x90(%rsp)
movl %ecx, %eax
cltd
idivl %edi
cltq
movq %rax, 0x88(%rsp)
pushq $0x3
popq %rdi
movl %esi, %eax
cltd
idivl %edi
cltq
movq %rax, 0x80(%rsp)
movl %ecx, %eax
cltd
idivl %edi
cltq
movq %rax, 0x78(%rsp)
xorl %r13d, %r13d
movsd %xmm0, 0xa8(%rsp)
mulsd %xmm0, %xmm1
movsd %xmm1, 0xb8(%rsp)
xorpd %xmm2, %xmm2
xorl %esi, %esi
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movq %r14, 0x70(%rsp)
movq %rbp, 0xb0(%rsp)
cmpl 0x78(%rbx), %r13d
jge 0x594e
movl %r13d, %eax
xorl %edx, %edx
pushq $0x6
popq %rcx
divl %ecx
incl %eax
cvtsi2sd %eax, %xmm1
movsd 0x2cf9(%rip), %xmm0     # 0x80b8
divsd %xmm1, %xmm0
addsd 0x2c95(%rip), %xmm0     # 0x8060
xorpd %xmm1, %xmm1
movsd %xmm1, 0x58(%rsp)
cmpl $0x2, %r13d
jb 0x53ea
mulsd 0xe0(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movss 0x50(%rbx), %xmm0
ucomiss 0x2d46(%rip), %xmm0     # 0x813c
movl %esi, %eax
movq %rax, 0x18(%rsp)
jbe 0x545b
cvtss2sd %xmm0, %xmm0
pushq $0x1
popq %rax
cmpq %r14, %rax
jge 0x545b
movq 0x408(%rbx), %rcx
movq 0x18(%rsp), %rdx
movq 0x3e0(%rbx,%rdx,8), %rdx
movq 0x8(%rdx), %rdx
movsd (%rdx,%rax,8), %xmm1
mulsd 0x2ca8(%rip), %xmm1     # 0x80d8
mulsd 0x2c60(%rip), %xmm1     # 0x8098
mulsd 0xa8(%rsp), %xmm1
divsd %xmm0, %xmm1
movq (%rcx), %rdx
movq 0x8(%rcx), %rcx
addsd (%rdx,%rax,8), %xmm1
movsd %xmm1, (%rcx,%rax,8)
incq %rax
jmp 0x5406
movl %r13d, 0x38(%rsp)
pushq $0x1
popq %rax
movl %esi, 0x34(%rsp)
subl %esi, %eax
movl %eax, 0x8(%rsp)
movl %eax, %r12d
movl $0x0, 0xc(%rsp)
xorpd %xmm2, %xmm2
pushq $0x1
popq %r13
xorps %xmm4, %xmm4
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
cmpq %rbp, %r13
jge 0x5863
movq 0x18(%rsp), %rax
movq 0x3e0(%rbx,%rax,8), %rcx
movq (%rcx,%r13,8), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, (%rax)
pushq $0x1
popq %rbp
cmpq %r14, %rbp
jge 0x5853
movq 0x430(%rbx), %rax
movq (%rax,%r13,8), %rax
movb (%rax,%rbp), %dl
cmpb $0x2, %dl
jl 0x584b
movq (%rcx,%r13,8), %rax
cmpb $0x4, %dl
movsd %xmm2, (%rsp)
movsd %xmm4, 0x10(%rsp)
ja 0x555b
movq -0x8(%rcx,%r13,8), %rdx
movq 0x8(%rcx,%r13,8), %rcx
movsd (%rcx,%rbp,8), %xmm0
movsd -0x8(%rax,%rbp,8), %xmm5
movsd 0x8(%rax,%rbp,8), %xmm1
movq 0x68(%rsp), %rcx
movsd (%rcx,%rbp,8), %xmm2
movapd %xmm1, %xmm4
mulsd %xmm2, %xmm4
addsd %xmm0, %xmm4
movsd (%rdx,%rbp,8), %xmm6
addsd %xmm6, %xmm4
movq 0x60(%rsp), %rcx
movsd (%rcx,%rbp,8), %xmm3
movapd %xmm5, %xmm9
mulsd %xmm3, %xmm9
addsd %xmm4, %xmm9
cmpq $0x1, %rbp
movsd %xmm5, 0x28(%rsp)
movsd %xmm6, 0x20(%rsp)
jbe 0x560c
movsd 0x2b72(%rip), %xmm2     # 0x80c8
jmp 0x56ca
movq 0x8(%rcx,%r13,8), %rsi
movsd (%rsi,%rbp,8), %xmm0
cmpb $0x7, %dl
jne 0x5619
movq 0x1468(%rbx), %rdx
movq (%rdx,%r13,8), %rdx
movsd (%rdx,%rbp,8), %xmm2
movsd -0x8(%rax,%rbp,8), %xmm7
movsd 0x8(%rax,%rbp,8), %xmm1
movq 0x1450(%rbx), %rdx
movq 0x1458(%rbx), %rsi
movq (%rsi,%r13,8), %rsi
movsd (%rsi,%rbp,8), %xmm3
movapd %xmm1, %xmm4
mulsd %xmm3, %xmm4
movapd %xmm0, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm4, %xmm5
movq -0x8(%rcx,%r13,8), %rcx
movsd (%rcx,%rbp,8), %xmm6
movq 0x1460(%rbx), %rcx
movq (%rcx,%r13,8), %rcx
movsd (%rcx,%rbp,8), %xmm4
movsd %xmm6, 0x20(%rsp)
mulsd %xmm4, %xmm6
addsd %xmm5, %xmm6
movq (%rdx,%r13,8), %rcx
movsd (%rcx,%rbp,8), %xmm5
movsd %xmm7, 0x28(%rsp)
movapd %xmm7, %xmm9
mulsd %xmm5, %xmm9
addsd %xmm6, %xmm9
addsd %xmm3, %xmm2
addsd %xmm4, %xmm2
addsd %xmm5, %xmm2
jmp 0x56ca
addsd 0x2abc(%rip), %xmm2     # 0x80d0
jmp 0x56c6
movq 0xc0(%rsp), %rsi
movq -0x8(%rsi,%r13,8), %rdx
movq (%rsi,%r13,8), %rsi
movsd (%rsi,%rbp,8), %xmm4
movsd -0x8(%rax,%rbp,8), %xmm9
movsd 0x8(%rax,%rbp,8), %xmm1
movq 0xc8(%rsp), %rsi
movq (%rsi,%r13,8), %rsi
movsd -0x8(%rsi,%rbp,8), %xmm2
movsd (%rsi,%rbp,8), %xmm3
movapd %xmm1, %xmm5
mulsd %xmm3, %xmm5
movq 0x68(%rsp), %rsi
movsd (%rsi,%rbp,8), %xmm6
mulsd %xmm6, %xmm5
movapd %xmm0, %xmm7
mulsd %xmm4, %xmm7
addsd %xmm5, %xmm7
movq -0x8(%rcx,%r13,8), %rcx
movsd (%rcx,%rbp,8), %xmm8
movsd (%rdx,%rbp,8), %xmm5
movsd %xmm8, 0x20(%rsp)
mulsd %xmm5, %xmm8
addsd %xmm7, %xmm8
movsd %xmm9, 0x28(%rsp)
mulsd %xmm2, %xmm9
movq 0x60(%rsp), %rcx
movsd (%rcx,%rbp,8), %xmm7
mulsd %xmm7, %xmm9
addsd %xmm8, %xmm9
mulsd %xmm6, %xmm3
addsd %xmm4, %xmm3
addsd %xmm5, %xmm3
mulsd %xmm7, %xmm2
addsd %xmm3, %xmm2
divsd %xmm2, %xmm9
movsd %xmm9, 0xd8(%rsp)
movsd (%rax,%rbp,8), %xmm2
movq 0x3e0(%rbx,%r12,8), %rax
movq (%rax,%r13,8), %r14
subsd (%r14,%rbp,8), %xmm2
movsd %xmm2, 0xd0(%rsp)
cvtsd2ss %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm1
callq 0x11a0
movss %xmm0, 0x3c(%rsp)
cvtsd2ss 0x20(%rsp), %xmm0
cvtsd2ss 0x28(%rsp), %xmm1
callq 0x11a0
movaps %xmm0, %xmm1
movss 0x3c(%rsp), %xmm0
callq 0x11a0
movq 0x408(%rbx), %rax
movq (%rax,%r13,8), %rax
movsd 0xd8(%rsp), %xmm1
addsd (%rax,%rbp,8), %xmm1
movsd %xmm1, (%r14,%rbp,8)
movq (%r15,%rbp,8), %rcx
movsbl (%rcx,%r13), %eax
movb $0x2, %dl
idivb %dl
movb %al, (%rcx,%r13)
movq 0x3e0(%rbx,%r12,8), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm1, %xmm2
jae 0x57c6
cvtss2sd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
movq 0x70(%rsp), %r14
movsd 0x10(%rsp), %xmm3
jb 0x57e0
addsd 0xb8(%rsp), %xmm0
movapd 0x40(%rsp), %xmm2
movapd %xmm2, %xmm1
cmpeqsd 0x2941(%rip), %xmm1     # 0x80e8
andpd %xmm1, %xmm0
andnpd %xmm2, %xmm1
orpd %xmm0, %xmm1
movlpd %xmm1, (%rax,%rbp,8)
incl 0xc(%rsp)
movb $0x8, %al
movapd %xmm1, 0x40(%rsp)
jmp 0x57d8
andq $0x0, (%rax,%rbp,8)
movb $0x4, %al
movq 0x70(%rsp), %r14
movsd 0x10(%rsp), %xmm3
movq (%r15,%rbp,8), %rcx
movb %al, (%rcx,%r13)
movq 0x18(%rsp), %rax
movq 0x3e0(%rbx,%rax,8), %rcx
movq (%rcx,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm2
movq 0x3e0(%rbx,%r12,8), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm1
subsd %xmm1, %xmm2
movapd %xmm2, %xmm4
xorpd 0x2949(%rip), %xmm4     # 0x8160
maxsd %xmm2, %xmm4
movsd (%rsp), %xmm2
addsd %xmm4, %xmm2
maxsd %xmm3, %xmm4
movq (%r15,%rbp,8), %rdx
cmpb $0x0, (%rdx,%r13)
jne 0x584b
movsd 0xd0(%rsp), %xmm3
mulsd 0x58(%rsp), %xmm3
addsd %xmm1, %xmm3
movsd %xmm3, (%rax,%rbp,8)
incq %rbp
jmp 0x54b1
incq %r13
movq 0xb0(%rsp), %rbp
jmp 0x548b
movl 0x38(%rsp), %r13d
cmpl $0xa, %r13d
jae 0x58fd
movapd %xmm2, %xmm1
divsd 0xa0(%rsp), %xmm1
divsd 0x98(%rsp), %xmm1
movq 0x3e0(%rbx,%r12,8), %rax
movq 0x90(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
movq 0x88(%rsp), %rdx
movsd %xmm2, (%rsp)
movsd (%rcx,%rdx,8), %xmm2
movq 0x80(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x78(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm3
leaq 0x33e2(%rip), %rdi      # 0x8cad
movl %r13d, %esi
movl 0x34(%rsp), %edx
movl 0x8(%rsp), %ecx
movaps %xmm4, %xmm0
movsd %xmm4, 0x10(%rsp)
movaps 0x40(%rsp), %xmm4
movl 0xc(%rsp), %r8d
movb $0x5, %al
callq 0x10f0
movsd 0x10(%rsp), %xmm4
movsd (%rsp), %xmm2
jmp 0x5934
cmpl $0x258, %r13d           # imm = 0x258
setb %cl
movl %r13d, %eax
xorl %edx, %edx
pushq $0x64
popq %rsi
divl %esi
testl %edx, %edx
sete %sil
movl %r13d, %eax
xorl %edx, %edx
movl $0x3e8, %edi            # imm = 0x3E8
divl %edi
testl %edx, %edx
je 0x5872
andb %sil, %cl
jne 0x5872
movsd 0x27a4(%rip), %xmm0     # 0x80e0
ucomisd %xmm4, %xmm0
ja 0x594e
incl %r13d
movl 0x8(%rsp), %esi
jmp 0x539d
leaq 0x338c(%rip), %rdi      # 0x8ce1
movl %r13d, %esi
movapd %xmm2, %xmm0
movb $0x1, %al
callq 0x10f0
cvtsd2ss 0x40(%rsp), %xmm0
movss %xmm0, 0x444(%rbx)
movl $0x1, 0x440(%rbx)
pushq $0x1
popq %rax
pushq $0x42
popq %rcx
pushq $0x2a
popq %rdx
movq %rax, %rsi
cmpq %r14, %rsi
jge 0x59e1
movq %rax, %rdi
cmpq %rbp, %rdi
jge 0x59dc
movq 0x430(%rbx), %r8
movq (%r8,%rdi,8), %r8
cmpb $0x1, (%r8,%rsi)
movq (%r15,%rsi,8), %r8
jg 0x59b1
movb $0x20, (%r8,%rdi)
jmp 0x59d7
movb (%r8,%rdi), %r9b
testb %r9b, %r9b
je 0x59d2
cmpb $0x5, %r9b
movl %edx, %r9d
cmovgel %ecx, %r9d
movb %r9b, (%r8,%rdi)
andl $0x0, 0x440(%rbx)
jmp 0x59d7
movb $0x2e, (%r8,%rdi)
incq %rdi
jmp 0x598f
incq %rsi
jmp 0x5987
movss 0x50(%rbx), %xmm0
xorpd %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x5a14
pushq $0x1
popq %rax
cmpq %r14, %rax
jge 0x5a14
movq 0x408(%rbx), %rcx
movq (%rcx), %rdx
movq 0x8(%rcx), %rcx
movsd (%rdx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %rax
jmp 0x59f2
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq