// File: /workspace/github_c_makefile_star2_O2/radforddc[P]icpc_siggen/icpc_fieldgen_opt.c
// Function: do_relax
int do_relax(MJD_Siggen_Setup *setup, int ev_calc) {
  int    old = 1, new = 0, iter, r, z;
  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, dif, sum_dif=0, max_dif;
  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;
  double e_over_E = 11.310; // e/epsilon; for 1 mm2, charge units 1e10 e/cm3, espilon = 16*epsilon0


  if (ev_calc) {
    // for field calculation, save impurity value along passivated surface
    for (r = 1; r < R; r++)
      setup->impurity[0][r] = setup->impurity[1][r];
  } else {
    // for WP calculation, clear all impurity values
    for (z = 0; z < L; z++) {
      for (r = 1; r < R; r++) {
        setup->impurity[z][r] = 0;
      }
    }
  }

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

    /* the following definition of the factor for over-relaxation improves convergence
           time by a factor ~ 70-120 for a 2kg ICPC detector, grid = 0.1 mm
         OR_fact increases with increasing volxel count (L*R)
               and with increasing iteration number
         0.997 is maximum asymptote for very large pixel count and iteration number */
    double OR_fact;
    if (ev_calc)  OR_fact = (1.991 - 1500.0/(L*R));
    else          OR_fact = (1.992 - 1500.0/(L*R));
    if (OR_fact < 1.4) OR_fact = 1.4;
    // if (iter == 0) printf("OR_fact = %f\n", OR_fact);
    if (iter < 1) OR_fact = 1.0;

    old = new;
    new = 1 - new;
    sum_dif = 0;
    max_dif = 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[new][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

        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[new][z-1][r] + v[new][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[new][z-1][r]*dz[0][z][r] + v[new][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[new][z-1][r]*eps_dz[z-1][r] + v[new][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

        if ((ev_calc || (setup->vacuum_gap > 0 && z == 1)) &&
            setup->point_type[z][r] < CONTACT_EDGE && r > 1 && z > 1) {   // normal bulk, no complications
          v[new][z][r] = (1.0-OR_fact)*v[old][z][r] + OR_fact * (v_sum / eps_sum + setup->impurity[z][r]);
        } else if (ev_calc || (setup->vacuum_gap > 0 && z == 1)) {
          v[new][z][r] = v_sum / eps_sum + setup->impurity[z][r];
        } else if (setup->point_type[z][r] < CONTACT_EDGE && r > 1 && z > 1) {   // normal bulk, no complications
          v[new][z][r] = (1.0-OR_fact)*v[old][z][r] + OR_fact * v_sum / eps_sum;
        } else {                          // over-relaxation at the edges seems to make things worse
          v[new][z][r] = v_sum / eps_sum;
        }

        // calculate difference from last iteration, for convergence check
        dif = fabs(v[old][z][r] - v[new][z][r]);
        sum_dif += dif;
        if (max_dif < dif) max_dif = dif;
      }
    }

    // report results for some iterations
    if (iter < 10 || (iter < 600 && iter%100 == 0) || iter%1000 == 0) {
      if (0 && ev_calc) {
        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\n",
               iter, old, new, max_dif, sum_dif/(L-2)/(R-2),
               v[new][L/2][R/2], v[new][L/3][R/3]);
      }
    }
    // check for convergence
    if ( ev_calc && max_dif < 0.00000008) break;
    if ( ev_calc && max_dif < 0.0008) break;  // comment out if you want convergence at the numerical error level
    if (!ev_calc && max_dif < 0.0000000001) break;
    if (!ev_calc && max_dif < 0.000001) break;  // comment out if you want convergence at the numerical error level

    /* every 100 iterations, check that detector is really depleted*/
    if (ev_calc && iter > 190 && iter%100 == 0) {
      for (z = 1; z < L; z++) {
        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
          if (v[new][z][r] < 0 ||
              (v[new][z][r] < v[new][z][r] &&
               v[new][z][r] < v[new][z][r] &&
               v[new][z][r] < v[new][z][r] &&
               v[new][z][r] < v[new][z][r])) {
            printf("Detector may not be fully depleted. Switching to ev_relax_undep()\n");
            ev_relax_undep(setup);
            return 0;
          }
        }
      }

    }
  }

  printf(">> %d %.16f\n\n", iter, sum_dif);
  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 $0xa8, %rsp
movl %esi, %r14d
movq %rdi, %rbx
movss 0x4(%rdi), %xmm0
movss 0x54(%rdi), %xmm1
movss %xmm1, 0x10(%rsp)
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, %r12
leal 0x2(%rax), %r15d
movss 0x8(%rbx), %xmm0
divss 0x10(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
leal 0x2(%rax), %ecx
movq 0x3f8(%rbx), %rdx
movq %rdx, 0x88(%rsp)
movq 0x400(%rbx), %rdx
movq %rdx, 0x80(%rsp)
movq 0x418(%rbx), %rdx
movq %rdx, 0x38(%rsp)
movq 0x420(%rbx), %rbp
movslq %ecx, %r13
movl %r14d, 0xc(%rsp)
pushq $0x1
popq %rdx
testl %r14d, %r14d
je 0x5ae9
movsd 0x2637(%rip), %xmm0     # 0x80f8
movss 0x10(%rsp), %xmm3
cmpq %r13, %rdx
jge 0x5b2c
movq 0x408(%rbx), %rsi
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
movsd (%rsi,%rdx,8), %xmm1
movsd %xmm1, (%rdi,%rdx,8)
incq %rdx
jmp 0x5ac7
xorl %esi, %esi
testl %r15d, %r15d
movl $0x0, %edi
cmovgl %r15d, %edi
movsd 0x25f1(%rip), %xmm0     # 0x80f0
movss 0x10(%rsp), %xmm3
cmpq %rdi, %rsi
je 0x5b2c
movq %rdx, %r8
cmpq %r13, %r8
jge 0x5b27
movq 0x408(%rbx), %r9
movq (%r9,%rsi,8), %r9
andq $0x0, (%r9,%r8,8)
incq %r8
jmp 0x5b0d
incq %rsi
jmp 0x5b05
movl %ecx, %edx
imull %r15d, %edx
cvtsi2sd %edx, %xmm2
movsd 0x25c2(%rip), %xmm1     # 0x8100
divsd %xmm2, %xmm1
cvtss2sd %xmm3, %xmm2
movsd %xmm2, 0x78(%rsp)
cvtsi2sd %r12d, %xmm2
movsd %xmm2, 0x70(%rsp)
cvtsi2sd %eax, %xmm2
movsd %xmm2, 0x68(%rsp)
pushq $0x2
popq %rsi
movl %r15d, %eax
cltd
idivl %esi
cltq
movq %rax, 0x60(%rsp)
movl %ecx, %eax
cltd
idivl %esi
cltq
movq %rax, 0x58(%rsp)
pushq $0x3
popq %rsi
movl %r15d, %eax
cltd
idivl %esi
cltq
movq %rax, 0x50(%rsp)
movl %ecx, %eax
cltd
idivl %esi
cltq
movq %rax, 0x48(%rsp)
movslq %r15d, %rax
movq %rax, 0x30(%rsp)
addsd %xmm0, %xmm1
movsd 0x255b(%rip), %xmm0     # 0x8108
maxsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
xorl %esi, %esi
movsd 0x249f(%rip), %xmm4     # 0x8060
xorps %xmm13, %xmm13
movsd 0x250a(%rip), %xmm14    # 0x80d8
movsd 0x24c1(%rip), %xmm15    # 0x8098
movapd 0x2590(%rip), %xmm9     # 0x8170
xorl %eax, %eax
xorpd %xmm12, %xmm12
cmpl 0x78(%rbx), %esi
jge 0x61b8
movl %eax, %edi
movapd %xmm4, %xmm11
movl %esi, 0x8(%rsp)
testl %esi, %esi
je 0x5c06
movsd 0x40(%rsp), %xmm11
movss 0x50(%rbx), %xmm1
ucomiss %xmm13, %xmm1
movl %edi, %eax
movq %rax, 0x10(%rsp)
jbe 0x5c6b
cvtss2sd %xmm1, %xmm2
pushq $0x1
popq %rax
cmpq %r13, %rax
jge 0x5c6b
movq 0x408(%rbx), %rcx
movq 0x10(%rsp), %rdx
movq 0x3e0(%rbx,%rdx,8), %rdx
movq 0x8(%rdx), %rdx
movsd (%rdx,%rax,8), %xmm3
mulsd %xmm14, %xmm3
mulsd %xmm15, %xmm3
mulsd 0x78(%rsp), %xmm3
divsd %xmm2, %xmm3
movq (%rcx), %rdx
movq 0x8(%rcx), %rcx
addsd (%rdx,%rax,8), %xmm3
movsd %xmm3, (%rcx,%rax,8)
incq %rax
jmp 0x5c1f
pushq $0x1
popq %rax
movl %edi, 0x20(%rsp)
subl %edi, %eax
movl %eax, 0x1c(%rsp)
movl %eax, %eax
movq %rax, 0x28(%rsp)
movapd %xmm4, %xmm2
subsd %xmm11, %xmm2
xorpd %xmm12, %xmm12
pushq $0x1
popq %rax
xorpd %xmm0, %xmm0
cmpq 0x30(%rsp), %rax
jge 0x5f9a
ucomiss %xmm13, %xmm1
seta %cl
cmpl $0x0, 0xc(%rsp)
setne %r10b
movq 0x10(%rsp), %rdx
movq 0x3e0(%rbx,%rdx,8), %rdx
movq (%rdx,%rax,8), %rsi
movsd 0x10(%rsi), %xmm3
movq 0x28(%rsp), %rdi
movq 0x3e0(%rbx,%rdi,8), %rdi
movq (%rdi,%rax,8), %r8
movsd %xmm3, (%r8)
movsd %xmm3, (%rsi)
cmpq $0x1, %rax
sete %r9b
andb %cl, %r9b
orb %r10b, %r9b
pushq $0x1
popq %r10
cmpq %r13, %r10
jge 0x5f92
movq 0x430(%rbx), %rcx
movq (%rcx,%rax,8), %rcx
movb (%rcx,%r10), %cl
cmpb $0x2, %cl
jl 0x5f8a
cmpb $0x4, %cl
ja 0x5d78
movq (%rdx,%rax,8), %r11
movq 0x8(%rdx,%rax,8), %r15
movq 0x38(%rsp), %r14
movsd (%r14,%r10,8), %xmm3
movsd 0x8(%r11,%r10,8), %xmm6
mulsd %xmm3, %xmm6
addsd (%r15,%r10,8), %xmm6
movq -0x8(%rdi,%rax,8), %r12
movq (%rdi,%rax,8), %r15
addsd (%r12,%r10,8), %xmm6
movsd (%rbp,%r10,8), %xmm5
movsd -0x8(%r15,%r10,8), %xmm4
mulsd %xmm5, %xmm4
addsd %xmm6, %xmm4
cmpq $0x1, %r10
jbe 0x5e21
movsd 0x2355(%rip), %xmm3     # 0x80c8
jmp 0x5eeb
cmpb $0x7, %cl
jne 0x5e2e
movq (%rdx,%rax,8), %r11
movq 0x8(%rdx,%rax,8), %r15
movq 0x1468(%rbx), %r12
movq (%r12,%rax,8), %r12
movsd (%r12,%r10,8), %xmm3
movq 0x1450(%rbx), %r12
movq 0x1458(%rbx), %r14
movq (%r14,%rax,8), %r14
movsd (%r14,%r10,8), %xmm5
movsd 0x8(%r11,%r10,8), %xmm4
mulsd %xmm5, %xmm4
movsd (%r15,%r10,8), %xmm6
mulsd %xmm3, %xmm6
addsd %xmm4, %xmm6
movq 0x1460(%rbx), %r14
movq (%r14,%rax,8), %r14
movsd (%r14,%r10,8), %xmm7
movq -0x8(%rdi,%rax,8), %r14
movq (%rdi,%rax,8), %r15
movsd (%r14,%r10,8), %xmm8
mulsd %xmm7, %xmm8
addsd %xmm6, %xmm8
movq (%r12,%rax,8), %r14
movsd (%r14,%r10,8), %xmm6
movsd -0x8(%r15,%r10,8), %xmm4
mulsd %xmm6, %xmm4
addsd %xmm8, %xmm4
addsd %xmm5, %xmm3
addsd %xmm7, %xmm3
addsd %xmm6, %xmm3
jmp 0x5eeb
addsd 0x22a7(%rip), %xmm3     # 0x80d0
jmp 0x5ee7
movq (%rdx,%rax,8), %r11
movq 0x8(%rdx,%rax,8), %r14
movq 0x80(%rsp), %r12
movq -0x8(%r12,%rax,8), %r15
movq (%r12,%rax,8), %r12
movsd (%r12,%r10,8), %xmm6
movq 0x88(%rsp), %r12
movq (%r12,%rax,8), %r12
movsd -0x8(%r12,%r10,8), %xmm3
movsd (%r12,%r10,8), %xmm5
movsd 0x8(%r11,%r10,8), %xmm4
mulsd %xmm5, %xmm4
movq 0x38(%rsp), %r12
movsd (%r12,%r10,8), %xmm7
mulsd %xmm7, %xmm4
movsd (%r14,%r10,8), %xmm8
mulsd %xmm6, %xmm8
addsd %xmm4, %xmm8
movsd (%r15,%r10,8), %xmm9
movq -0x8(%rdi,%rax,8), %r14
movq (%rdi,%rax,8), %r15
movsd (%r14,%r10,8), %xmm10
mulsd %xmm9, %xmm10
addsd %xmm8, %xmm10
movsd -0x8(%r15,%r10,8), %xmm4
mulsd %xmm3, %xmm4
movsd (%rbp,%r10,8), %xmm8
mulsd %xmm8, %xmm4
addsd %xmm10, %xmm4
mulsd %xmm7, %xmm5
addsd %xmm6, %xmm5
addsd %xmm9, %xmm5
movapd 0x228e(%rip), %xmm9     # 0x8170
mulsd %xmm8, %xmm3
addsd %xmm5, %xmm3
cmpq $0x2, %rax
setae %r14b
cmpb $0x7, %cl
setb %r12b
cmpq $0x2, %r10
setae %cl
andb %r14b, %cl
andb %r12b, %cl
testb %r9b, %r9b
je 0x5f2c
divsd %xmm3, %xmm4
testb %cl, %cl
je 0x5f49
movq 0x408(%rbx), %rcx
movq (%rcx,%rax,8), %rcx
addsd (%rcx,%r10,8), %xmm4
mulsd %xmm11, %xmm4
jmp 0x5f39
testb %cl, %cl
je 0x5f5c
mulsd %xmm11, %xmm4
divsd %xmm3, %xmm4
movsd (%r11,%r10,8), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm4, %xmm3
jmp 0x5f64
movq 0x408(%rbx), %rcx
movq (%rcx,%rax,8), %rcx
addsd (%rcx,%r10,8), %xmm4
jmp 0x5f60
divsd %xmm3, %xmm4
movapd %xmm4, %xmm3
movsd %xmm3, (%r15,%r10,8)
movsd (%rsi,%r10,8), %xmm3
subsd (%r8,%r10,8), %xmm3
andpd %xmm9, %xmm3
addsd %xmm3, %xmm12
ucomisd %xmm0, %xmm3
jbe 0x5f8a
movapd %xmm3, %xmm0
incq %r10
jmp 0x5cf1
incq %rax
jmp 0x5c94
movl 0x8(%rsp), %eax
cmpl $0xa, %eax
jae 0x604a
movapd %xmm12, %xmm1
divsd 0x70(%rsp), %xmm1
divsd 0x68(%rsp), %xmm1
movq 0x28(%rsp), %rax
movq 0x3e0(%rbx,%rax,8), %rax
movq 0x60(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
movq 0x58(%rsp), %rdx
movsd (%rcx,%rdx,8), %xmm2
movq 0x50(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x48(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm3
leaq 0x2cfd(%rip), %rdi      # 0x8cef
movl 0x8(%rsp), %esi
movl 0x20(%rsp), %edx
movl 0x1c(%rsp), %ecx
movb $0x4, %al
movsd %xmm12, 0x20(%rsp)
movapd %xmm0, 0x90(%rsp)
callq 0x10f0
movapd 0x90(%rsp), %xmm0
movsd 0x20(%rsp), %xmm12
movapd 0x2142(%rip), %xmm9     # 0x8170
movsd 0x2061(%rip), %xmm15    # 0x8098
movsd 0x2098(%rip), %xmm14    # 0x80d8
xorps %xmm13, %xmm13
movl 0x8(%rsp), %esi
jmp 0x6085
cmpl $0x258, %eax            # imm = 0x258
setb %cl
movl 0x8(%rsp), %eax
xorl %edx, %edx
pushq $0x64
popq %rsi
divl %esi
testl %edx, %edx
sete %dil
movl 0x8(%rsp), %eax
xorl %edx, %edx
movl $0x3e8, %esi            # imm = 0x3E8
divl %esi
movl 0x8(%rsp), %esi
testl %edx, %edx
je 0x5fa7
andb %dil, %cl
jne 0x5fa7
cmpl $0x0, 0xc(%rsp)
je 0x60c2
movapd %xmm0, %xmm3
movsd 0x2078(%rip), %xmm1     # 0x8110
cmpltpd %xmm1, %xmm3
movapd %xmm0, %xmm1
movsd 0x2037(%rip), %xmm2     # 0x80e0
cmpltpd %xmm2, %xmm1
orpd %xmm3, %xmm1
movd %xmm1, %eax
testb $0x1, %al
movl 0x8(%rsp), %esi
jne 0x61b8
cmpl $0x0, 0xc(%rsp)
movsd 0x1f91(%rip), %xmm4     # 0x8060
jne 0x6103
movapd %xmm0, %xmm2
movsd 0x203b(%rip), %xmm1     # 0x8118
cmpltpd %xmm1, %xmm2
movsd 0x1fd6(%rip), %xmm1     # 0x80c0
cmpltpd %xmm1, %xmm0
orpd %xmm2, %xmm0
movd %xmm0, %eax
testb $0x1, %al
movl 0x8(%rsp), %esi
jne 0x61b8
movl %esi, %eax
xorl %edx, %edx
pushq $0x64
popq %rcx
divl %ecx
cmpl $0x0, 0xc(%rsp)
je 0x6184
cmpl $0xbf, 0x8(%rsp)
jb 0x6184
testl %edx, %edx
jne 0x6184
pushq $0x1
popq %rax
cmpq 0x30(%rsp), %rax
jge 0x6184
movq 0x10(%rsp), %rcx
movq 0x3e0(%rbx,%rcx,8), %rcx
movq (%rcx,%rax,8), %rcx
movsd 0x10(%rcx), %xmm0
movsd %xmm0, (%rcx)
pushq $0x1
popq %rcx
cmpq %r13, %rcx
jge 0x617f
movq 0x430(%rbx), %rdx
movq (%rdx,%rax,8), %rdx
cmpb $0x2, (%rdx,%rcx)
jl 0x617a
movq 0x28(%rsp), %rdx
movq 0x3e0(%rbx,%rdx,8), %rdx
movq (%rdx,%rax,8), %rdx
xorpd %xmm0, %xmm0
ucomisd (%rdx,%rcx,8), %xmm0
ja 0x6193
incq %rcx
jmp 0x6148
incq %rax
jmp 0x6124
movl 0x8(%rsp), %esi
incl %esi
movl 0x1c(%rsp), %eax
jmp 0x5be7
leaq 0x2f83(%rip), %rdi      # 0x911d
callq 0x1070
movq %rbx, %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x51c4
leaq 0x2b22(%rip), %rdi      # 0x8ce1
movapd %xmm12, %xmm0
movb $0x1, %al
callq 0x10f0
movss 0x50(%rbx), %xmm0
xorpd %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x61fe
pushq $0x1
popq %rax
cmpq %r13, %rax
jge 0x61fe
movq 0x408(%rbx), %rcx
movq (%rcx), %rdx
movq 0x8(%rcx), %rcx
movsd (%rdx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %rax
jmp 0x61dc
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq