
//Verbinden, dass Enter bei den Zahleneingaben verwendet werden kann
window.addEventListener('keydown', function (e) { if (e.keyIdentifier == 'U+000A' || e.keyIdentifier == 'Enter' || e.keyCode == 13) { if (e.target.nodeName == 'INPUT' && e.target.type == 'number') { e.preventDefault(); return false; } } }, true);

//Begrenzung von den Zahleneingaben
document.getElementById("dt").addEventListener("change", function () {
  let v = this.value;
  if (v < 0.01) this.value = 0.01;
  if (v > dt_max) this.value = dt_max;

});

function Reset() {
  if (confirm('Wollen Sie wirklich einen Reset durchführen? Alle nichtgespeicherten Informationen gehen verloren')) {
    window.location.reload()
  } else {
  }
}


function reset_Button() {
  document.getElementById("Button_Berechnen").innerHTML = "Berechnen";
}

document.getElementById("Einstellung-Isolierung").addEventListener("click", reset_Button);
document.getElementById("Einstellung-Metall").addEventListener("click", reset_Button);
document.getElementById("Einstellung-Luft").addEventListener("click", reset_Button);
document.getElementById("Simulationseinstellung").addEventListener("click", reset_Button);

//Einlesen von allen Inputs die zur übergebenen Div gehören
function readall(Selector, type) {
  const divElem = document.getElementById(Selector);
  const inputElements = divElem.querySelectorAll(type);
  var Werte = new Array;
  for (var i = 0; i < inputElements.length - 1; i++) {
    Werte[i] = [inputElements[i].id, inputElements[i].value]
  }
  return Werte;
}


function Statusbar() {
  document.getElementById("Prozent").innerHTML = percent;
  console.log("Aufgerufen")
}

//Vorbereiten der Daten zum Download über einen Link
function download(text, name, type, id) {
  var a = document.getElementById(id);
  var file = new Blob([text.join('\n')], { type: type });
  a.href = URL.createObjectURL(file);
  a.download = name;
}


async function loadFile(file, id) {
  let text = await file.text();
  var divElem = document.getElementById(id).querySelectorAll("input");
  for (var i = 0; i < divElem.length - 1; i++) {
    var test = text.split('\n')[i];
    if (divElem[i].id == test.split(',')[0]) {
      divElem[i].value = test.split(',')[1]
    }
    else {
      alert("Datei entspricht nicht den Vorgaben")
    }
  }
}

//Plotten von der Anzeige, welches Element gerade angezeigt wird.
function Kreis() {
  r_mess = document.getElementById("r_Plot").value / document.getElementById("l_disk").value;
  z_mess = document.getElementById("z_Plot").value / document.getElementById("l_disk").value;
  const canvas = document.getElementById("Anzeige_Kreis");
  const ctx = canvas.getContext('2d');
  var center = 145;
  var offset = 10;

  ctx.clearRect(0, 0, canvas.width, canvas.height);
  //Zeichnen vom Metall
  ctx.strokeStyle = "blue";
  for (var r_p = 0; r_p <= 21; r_p++) {
    for (var z_p = 0; z_p < z_max; z_p++) {
      var scale = 10;
      ctx.strokeRect(center + r_p * scale, offset + z_p * scale, scale, scale);
      ctx.strokeRect(center - r_p * scale, offset + z_p * scale, scale, scale);
      if (z_p == z_mess - 1 && r_p == r_mess - 1) {
        ctx.fillStyle = "red";
        ctx.fillRect(center + r_p * scale, offset + z_p * scale, scale, scale);
        ctx.fillRect(center - r_p * scale, offset + z_p * scale, scale, scale);
      }
    }
  }
  //Zeichnen von der Isolierung
  ctx.strokeStyle = "green";
  for (var r_p = 0; r_p <= 21; r_p++) {
    for (var z_p = z_max; z_p < z_max_iso; z_p++) {
      var scale = 10;
      ctx.strokeRect(center + r_p * scale, offset + z_p * scale, scale, scale);
      ctx.strokeRect(center - r_p * scale, offset + z_p * scale, scale, scale);
      if (z_p == z_mess - 1 && r_p == r_mess - 1) {
        ctx.fillStyle = "red";
        ctx.fillRect(center + r_p * scale, offset + z_p * scale, scale, scale);
        ctx.fillRect(center - r_p * scale, offset + z_p * scale, scale, scale);
      }
    }
  }

  //Zeichnen von Pfeil für den Blitzeinschlag
  ctx.strokeStyle = "red";
  ctx.moveTo(center + 5, 0);
  ctx.lineTo(center + 5, 10);
  ctx.lineTo(center + 5 + 5, 5);
  ctx.moveTo(center + 5, 10);
  ctx.lineTo(center + 5 - 5, 5);
  ctx.stroke();

}

//Init
var Firstplot = true;



//Messpunkt
var z_mess = 6;
var r_mess = 4;

//Standard Metall B
a_0 = 3.44e-6;
a_1 = 0.743e-3;
b_1 = -0.324e-6;
c_sss0 = 3.72e6;
a_2 = 0.473e-3;
b_2 = -0.226e-6;
T_s = 1450;
dt_max = 5;

//Standard Tefon
a_i0 = 0.111e-6;
a_3 = 0.56e-3;
b_3 = -10.7e-6;
lambda = 0.369;
a_4 = 3.4e-3;
b_4 = -5.1e-6;
Isolierung = 0;
T_s_iso = 300;

t = 1;
Luft = false;
l = 0.5e-3;  //Diskretisierung der Länge
dt = 0.5e-3; //Zeitschritt
sigma = 2e-3; //Schmeltiefe für Q=200 -> 2mm (Alu 2,5mm) für Q=100 -> 1,5mm (Alu 2mm)
alpha = 3.5; //W/(m^2*K) Wärmeübertragungskoffizient Luft
T_0 = 20; //Umgebungstemp
t_l = 0.5; //Dauer Langzeitstrom
d_mat = 3e-3; //Dicke in mm
r_max = 500;
z_max = d_mat / l;
t_max = 5000;
z_iso = 0;
z_max_iso = z_max + z_iso;

inkerement_save = 1;

function change_Metall() {
  Material = document.getElementById("Metallauswahl").value;
  document.getElementById("Eigenenes_Metall").hidden = true;
  console.log(Material);
  switch (Material) {
    case "Kupfer":
      a_0 = 117e-6;
      a_1 = -0.388e-3;
      b_1 = 0.084e-6;
      c_sss0 = 3.46e6;
      a_2 = 0.167e-3;
      b_2 = 0e-6;
      T_s = 1083;
      dt_max = 0.5;
      document.getElementById("dt").value = dt_max;
      document.getElementById("dt").max = dt_max;
      break;
    case "Aluminium":
      a_0 = 98e-6;
      a_1 = -0.481e-3;
      b_1 = 0e-6;
      c_sss0 = 2.28e6;
      a_2 = 0.361e-3;
      b_2 = 0e-6;
      T_s = 660;
      dt_max = 0.5;
      document.getElementById("dt").value = dt_max;
      document.getElementById("dt").max = dt_max;
      break;
    case "Messing":
      a_0 = 33.8e-6;
      a_1 = 0.895e-3;
      b_1 = 0e-6;
      c_sss0 = 3.27e6;
      a_2 = 0.322e-3;
      b_2 = 0e-6;
      T_s = 890;
      dt_max = 1;
      document.getElementById("dt").value = dt_max;
      document.getElementById("dt").max = dt_max;
      break;
    case "Stahl A":
      a_0 = 21.7e-6;
      a_1 = -1.53e-3;
      b_1 = 0.735e-6;
      c_sss0 = 3.33e6;
      a_2 = 1.95e-3;
      b_2 = -1.04e-6;
      T_s = 1535;
      dt_max = 2;
      document.getElementById("dt").value = dt_max;
      document.getElementById("dt").max = dt_max;
      break;
    case "Stahl B":
      a_0 = 3.44e-6;
      a_1 = 0.743e-3;
      b_1 = -0.324e-6;
      c_sss0 = 3.72e6;
      a_2 = 0.473e-3;
      b_2 = -0.226e-6;
      T_s = 1450;
      dt_max = 5;
      document.getElementById("dt").value = dt_max;
      document.getElementById("dt").max = dt_max;
      break;
    case "Eigenen":
      document.getElementById("Eigenenes_Metall").hidden = false;
      reader = new FileReader();
      break;
    default:
      console.log("Fehler bei der Materialauswahl");
  }
  console.log(a_0);
}


function change_Isolierung() {
  document.getElementById("Eigenene_Isolierung").hidden = true;
  Isoliermaterial = document.getElementById("Isolierstoff").value;
  console.log(Isoliermaterial);
  switch (Isoliermaterial) {
    case "Teflon":
      a_i0 = 0.111e-6;
      a_3 = 0.56e-3;
      b_3 = -10.7e-6;
      lambda = 0.369;
      a_4 = 3.4e-3;
      b_4 = -5.1e-6;
      Isolierung = 1
      T_s_iso = 300;
      if (document.getElementById("d_Iso").value == 0) {
        document.getElementById("d_Iso").value = 3;
      }
      document.getElementById("d_Iso").readOnly = false;
      break;
    case "Viton":
      a_i0 = 0.118e-6;
      a_3 = -13.8e-3;
      b_3 = 80.6e-6;
      lambda = 0.0454;
      a_4 = 7.6e-3;
      b_4 = 7.5e-6;
      Isolierung = 1
      T_s_iso = 350;
      if (document.getElementById("d_Iso").value == 0) {
        document.getElementById("d_Iso").value = 3;
      }
      document.getElementById("d_Iso").readOnly = false;
      break;
    case "Keine":
      Isolierung = 0;
      document.getElementById("d_Iso").value = 0;
      document.getElementById('d_Iso').readOnly = true;
      break;
    case "Eigenen":
      document.getElementById("Eigenene_Isolierung").hidden = false;
      a_i0 = document.getElementById("a_i0");
      a_3 = document.getElementById("a_3");
      b_3 = document.getElementById("b_3");
      lambda = document.getElementById("lambda");
      a_4 = document.getElementById("a_4");
      b_4 = document.getElementById("b_4");
      T_s_iso = document.getElementById("T_s_iso");
      Isolierung = 1
      if (document.getElementById("d_Iso").value == 0) {
        document.getElementById("d_Iso").value = 3;
      }
      document.getElementById("d_Iso").readOnly = false;
      break;
    default:
      console.log("Fehler bei der Isolierstoffauswahl");
  }
  Kreis();
  change_Dicke_Iso()
}

function change_Dicke_Metall() {
  z_max = document.getElementById("d_Metall").value / 1000 / l;
  z_max_iso = z_max + z_iso;
  Kreis()
}

function change_Dicke_Iso() {
  z_iso = document.getElementById("d_Iso").value / 1000 / l;
  z_max_iso = z_max + z_iso;
  Kreis()
}

function update_link_Metall() {
  download(readall("Eigenenes_Metall", "input"), 'Metall', 'text/plain', 'Download_Metall');
}


function update_link_Isolierung() {
  download(readall("Eigenene_Isolierung", "input"), 'Isolierung', 'text/plain', 'Download_Isolierung');
  change_Isolierung();
}



update_link_Metall();
update_link_Isolierung();


function sleep(ms) {
  return new Promise((r) =>
    setTimeout(r, ms));
}

console.log("Initialisierung abgeschlossen");

async function berechnen() {

  var start_time = Date.now();
  dt = document.getElementById("dt").value / 1000;
  t_max = document.getElementById("t_sim").value / document.getElementById("dt").value;
  l = document.getElementById("l_disk").value / 1000;
  r_max = document.getElementById("Radius").value / document.getElementById("l_disk").value;
  W_AK = document.getElementById("Stoßladung").value * 20 * 0.7; //J  Wirksame Fußpunktenergie (Stoßstrom)
  W_AKL = document.getElementById("Langzeitladung").value * 20 * 0.7 // //J Wirksame Fußpunktenergie (Langzeit)
  d_mat = document.getElementById("d_Metall").value;
  z_iso = document.getElementById("d_Iso").value / 1000 / l;
  Material = document.getElementById("Metallauswahl").value;
  Luft = document.getElementById("Luft").checked;
  alpha = document.getElementById("alpha").value;
  console.log("Luft: " + Luft);

  z_max = d_mat / 1000 / l;
  z_max_iso = z_max + z_iso;

  var percent = 0;

  inkerement_save = Math.round(5 / document.getElementById("dt").value);
  console.log("Plot Inkrement: " + inkerement_save);
  console.log("dt = " + dt);
  console.log("t_max = " + t_max);
  console.log("l = " + l);
  console.log("r_max = " + r_max);
  console.log("W_AK = " + W_AK);
  console.log("W_AKL = " + W_AKL);
  console.log("Z_max = " + z_max);
  console.log("Z_max_iso = " + z_max_iso);

  if (Material == "Aluminium") {
    if (W_AKL >= 200) {
      sigma = 2.5e-3;
    }
    else if (W_AKL >= 100) {
      sigma = 2.0e-3;
    }
  } else {
    if (W_AKL >= 200) {
      sigma = 2e-3;
    }
    else if (W_AKL >= 100) {
      sigma = 1.5e-3;
    }
  }


  T = new Array();
  for (var z = 0; z <= z_max_iso + 1; z++) {
    T[z] = new Array();
    for (var r = 0; r <= r_max + 1; r++) {
      T[z][r] = new Array();
      for (var t = 0; t <= 1; t++) {
        T[z][r][t] = T_0;
      }
    }
  }

  T_save = new Array();
  for (var z = 0; z <= z_max_iso + 1; z++) {
    T_save[z] = new Array();
    for (var r = 0; r <= r_max + 1; r++) {
      T_save[z][r] = new Array();
      for (var t = 0; t <= (t_max + 1) / inkerement_save; t++) {
        T_save[z][r][t] = T_0;
      }
    }
  }

  console.log((t_max + 1) / inkerement_save);

  var dT = new Array();
  for (var z = 0; z <= z_max_iso + 1; z++) {
    dT[z] = new Array();
    for (var r = 0; r <= r_max + 1; r++) {
      dT[z][r] = 0;
    }
  }


  dTs = T_s - T_0;
  r_s = Math.sqrt(W_AK / (c_sss0 * Math.PI * sigma * dTs))

  console.log("Anzahl vermolzener Elemente Radius: " + Math.floor(r_s / l))
  console.log("Anzahl vermolzener Elemente Tiefe: " + Math.floor(sigma / l))

  //Berechnung des Anfangswerts für verschmolzenes Material
  for (var r = 1; r <= Math.floor(r_s / l); r++) {
    for (var z = 1; z <= Math.floor(sigma / l); z++) {
      T[z][r][0] = T_s;
    }
  }


  var dTs = new Array();
  for (var z = 0; z <= z_max_iso + 1; z++) {
    dTs[z] = new Array();
    for (var r = 0; r <= r_max + 1; r++) {
      dTs[z][r] = new Array();
      for (var t = 0; t <= 1; t++) {
        dTs[z][r][t] = 0;
      }
    }
  }



  //Berechnung der erneuten Aufwärmung durch die Langzeitkomponente

  tl = Math.floor(t_l / dt);      //Berechnung der Zeitschritte, in denen der Langzeitstrom fließt
  W_LZ = W_AKL / t_l * dt;          //Berechnung der Energie pro Zeitschritt
  W_Rest = 0;

  console.log("tl: " + tl);
  console.log("W_LZ: " + W_LZ);

  c = 0;
  var i = 0;
  var count = 10;
  var t = 0;
  var i_log = t_max / 50;



  for (t_c = 0; t_c <= t_max; t_c++) {

    for (var z = 1; z <= z_max_iso + 1; z++) {
      for (var r = 1; r <= r_max + 1; r++) {
        dT[z][r] = T[z][r][t] - T_0;
      }
    }


    //Wärmeausgleich
    for (var z = 1; z <= z_max_iso; z++) {
      for (var r = 1; r <= r_max; r++) {
        a = a_0 * (1 + a_1 * dT[z][r] + b_1 * dT[z][r] * dT[z][r]);   //in m^2/s

        
        //Zeitliche Änderung der Temperatur je VOlumenelement (S. 130 A2.9)
        // z = tiefe; r = Radius
        //Normalfall
        F_1 = 2 / ((2 * r - 1) * Math.log((2 * r + 1) / (2 * r - 1))); //Einheitenlos
        F_2 = 2 / ((2 * r - 1) * Math.log((2 * r - 1) / (2 * r - 3))); //Einheitenlos
        if ((r > 1) && (z > 1) && (z < z_max) && (r < r_max)) {
          dT[z][r] = a * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] + T[z + 1][r][t] - 2 * T[z][r][t]);
        } else if (z == 1) {
          if (r == 1) {
            dT[z][r] = a * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + T[z + 1][r][t] - T[z][r][t]);
          } else if (r == r_max) {
            dT[z][r] = a * dt / (l ** 2) * (F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z + 1][r][t] - T[z][r][t]);
          } else {
            dT[z][r] = a * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z + 1][r][t] - T[z][r][t]);
          }
        } else if (z < z_max) {
          if (r == 1) {
            dT[z][r] = a * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + T[z - 1][r][t] + T[z + 1][r][t] - 2 * T[z][r][t]);
          } else if (r == r_max) {
            dT[z][r] = a * dt / (l ** 2) * (F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] + T[z + 1][r][t] - 2 * T[z][r][t]);
          }
        } else if (z == z_max) {
          if (Isolierung == 1) {
            c_sss1 = c_sss0 * (1 + a_2 * dT[z][r] + b_2 * dT[z][r] * dT[z][r]);
            lambda2 = lambda * (1 + a_4 * dT[z + 1][r] + b_4 * dT[z + 1][r] * dT[z + 1][r]);
            a2 = 2 * lambda2 / c_sss1; ////Metall -> Isolierung
            if (T[z + 1][r][t] > T_s_iso) {
              a2 = 0;
            }

            if (r == 1) {
              dT[z][r] = a * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]) + a2 * dt / (l ** 2) * (T[z + 1][r][t] - T[z][r][t]);
            }
            else if (r == r_max) {
              dT[z][r] = a * dt / (l ** 2) * (F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]) + a2 * dt / (l ** 2) * (T[z + 1][r][t] - T[z][r][t]);
            }
            else {
              dT[z][r] = a * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]) + a2 * dt / (l ** 2) * (T[z + 1][r][t] - T[z][r][t]);
            }
          } else if (Luft == true && Isolierung == 0) {
            c_sss = c_sss0 * (1 + a_2 * dT[z][r] + b_2 * dT[z][r] * dT[z][r]);
            if (r == 1) {
              dT[z][r] = a * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]) - alpha * dt / (c_sss * l) * (T[z][r][t] - T_0);
            }
            else if (r == r_max) {
              dT[z][r] = a * dt / (l ** 2) * (F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]) - alpha * dt / (c_sss * l) * (T[z][r][t] - T_0);
            }
            else {
              dT[z][r] = a * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]) - alpha * dt / (c_sss * l) * (T[z][r][t] - T_0);
            }
          }
          else {
            if (r == 1) {
              dT[z][r] = a * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]);
            }
            else if (r == r_max) {
              dT[z][r] = a * dt / (l ** 2) * (F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]);
            }
            else {
              dT[z][r] = a * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]);
            }
          }
        } else if (z == z_max + 1) {
          a2s = 2 * a_i0 * (1 + a_3 * dT[z][r] + b_3 * dT[z][r] * dT[z][r]); ////Isolierung -> Metall
          a2 = a_i0 * (1 + a_3 * dT[z][r] + b_3 * dT[z][r] * dT[z][r]);
          if (T[z][r][t] > T_s_iso) {
            a2s = 0;
          }
          if (r == 1) {
            dT[z][r] = a2 * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + T[z + 1][r][t] - T[z][r][t]) + a2s * dt / (l ** 2) * (T[z - 1][r][t] - T[z][r][t]);
          }
          else if (r == r_max) {
            dT[z][r] = a2 * dt / (l ** 2) * (F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z + 1][r][t] - T[z][r][t]) + a2s * dt / (l ** 2) * (T[z - 1][r][t] - T[z][r][t]);
          }
          else {
            dT[z][r] = a2 * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z + 1][r][t] - T[z][r][t]) + a2s * dt / (l ** 2) * (T[z - 1][r][t] - T[z][r][t]);
          }
        } else if (z < z_max_iso) {
          a2 = a_i0 * (1 + a_3 * dT[z][r] + b_3 * dT[z][r] * dT[z][r]);
          if ((r > 1) && (r < r_max)) {
            dT[z][r] = a2 * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] + T[z + 1][r][t] - 2 * T[z][r][t]);
          }
          else if (r == 1) {
            dT[z][r] = a2 * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + T[z - 1][r][t] + T[z + 1][r][t] - 2 * T[z][r][t]);
          }
          else if (r == r_max) {
            dT[z][r] = a2 * dt / (l ** 2) * (F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] + T[z + 1][r][t] - 2 * T[z][r][t]);
          }
        } else if (Luft == false) {

          a2 = a_i0 * (1 + a_3 * dT[z][r] + b_3 * dT[z][r] * dT[z][r]);
          if (r == 1) {
            dT[z][r] = a2 * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]);
          }
          else if (r == r_max) {
            dT[z][r] = a2 * dt / (l ** 2) * (F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]);
          }
          else {
            dT[z][r] = a2 * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]);
          }
        } else {
          c_sss = c_sss0 * (1 + a_2 * dT[z][r] + b_2 * dT[z][r] * dT[z][r]);
          a2 = a_i0 * (1 + a_3 * dT[z][r] + b_3 * dT[z][r] * dT[z][r]);
          if (r == 1) {
            dT[z][r] = a2 * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]) - alpha * dt / (c_sss * l) * (T[z][r][t] - T_0);
          }
          else if (r == r_max) {
            dT[z][r] = a2 * dt / (l ** 2) * (F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]) - alpha * dt / (c_sss * l) * (T[z][r][t] - T_0);
          }
          else {
            dT[z][r] = a2 * dt / (l ** 2) * (F_1 * (T[z][r + 1][t] - T[z][r][t]) + F_2 * (T[z][r - 1][t] - T[z][r][t]) + T[z - 1][r][t] - T[z][r][t]) - alpha * dt / (c_sss * l) * (T[z][r][t] - T_0);
          }
        }
      }
    }

    var t_ink = Math.round(((t_c) / inkerement_save) + 1)
    if (count >= inkerement_save) {
      for (var z = 0; z <= z_max_iso + 1; z++) {
        for (var r = 0; r <= r_max + 1; r++) {
          T_save[z][r][t_ink] = T[z][r][t];
        }
      }
      count = 0;
    }
    count++;

    for (var z = 0; z <= z_max_iso + 1; z++) {
      for (var r = 0; r <= r_max + 1; r++) {
        T[z][r][t] = T[z][r][t] + dT[z][r];
        dTs[z][r][t] = T_s - T[z][r][t];
      }
    }


    //Aufheizen durch Langzeitstrom

    if (t_c <= tl) {
      W_LZr = W_LZ;
      flag = 0;
      c = 0;
      c_sss = c_sss0 * (1 + a_2 * (T_s - T_0) + b_2 * (T_s - T_0) ** 2);
      for (var r = 1; r <= r_max; r++) {
        for (var z = 1; z <= Math.floor(sigma / l); z++) {

          ////c_sss = c_sss0*(1+a_2*dT[z][r]+b_2*dT[z][r]^2);

          W_LZr = W_LZr - (((l * r) ** 2 - (l * (r - 1)) ** 2) * Math.PI * l * c_sss * dTs[z][r][t]);

          if (W_LZr > 0) {             //Überprüfung ob noch Energie vorhanden ist
            T[z][r][t] = T_s;
            c = c + 1;
          }
          else {
            W_Rest = W_LZr + (((l * r) ** 2 - (l * (r - 1)) ** 2) * Math.PI * l * c_sss * dTs[z][r][t]);
            T[z][r][t] = T[z][r][t] + W_Rest / (((l * r) ** 2 - (l * (r - 1)) ** 2) * Math.PI * l * c_sss);
            flag = 1;
            break;
          }
        }
        if (flag == 1) {
          break;
        }
      }
    }

    //Animation vom Fortschritt
    i++;
    if (i >= i_log) {
      i = 0;
      percent = percent + 2;
      //console.log(percent + "%");
      document.getElementById("Button_Berechnen").innerHTML = "Berechnen..." + percent + "%";
      document.getElementById("Button_Berechnen").style.backgroundImage = 'linear-gradient(to left, white ' + (100 - percent) + '%, #e0e0e096 ' + (100 - percent) + '%, #e0e0e096 100%)';
      await sleep(0);
    }

  }
  console.log("Rechendauer: " + (Date.now() - start_time));
  console.log("Berechnung abgeschlossen");
  document.getElementById("Button_Berechnen").innerHTML = "Berechnung abgeschlossen";
  //document.getElementById("2D_Single").hidden = false;
  //document.getElementById("2D_Multi").hidden = false;
  //document.getElementById("3D").hidden = false;
  if (Firstplot) {
    Firstplot = false;
    Show_Single();
    Plot_2D();
    Plot_Multi_2D();
    Plotten_3D();
  }
  Plot_2D();
  Plot_Multi_2D();
  Plotten_3D();

  console.log("Plotten abgeschlossen")
}



function Plot_2D() {
  var t_max_plot = (t_max + 1) / inkerement_save;
  daten = new Array();
  for (var t = 1; t <= t_max_plot; t++) {
    daten[t] = T_save[z_mess][r_mess][t];
  }

  var xValues = [];
  var yValues = [];
  var ySchmelz_Metall = [];
  var ySchmelz_Iso = [];
  // generateData("x * 2 + 7", 0, 10, 0.5);

  var step = Math.round(5e-3 / dt); //Verhindern das bei kleiner Auflösung zu viele Werte geplottet werden
  for (var t = 1; t <= t_max_plot; t++) {
    yValues.push(daten[t]);
    xValues.push(t * dt * inkerement_save);
    ySchmelz_Metall.push(T_s)
    ySchmelz_Iso.push(T_s_iso)
  }



  if (z_mess > z_max) {
    ySchmelz = ySchmelz_Iso;
    name_plot_Schmelz = 'Schmelztemperatur Isolierstoff';
    name_plot = 'Temperatur (Isolierung)'
  } else {
    ySchmelz = ySchmelz_Metall;
    name_plot_Schmelz = 'Schmelztemperatur Metall'
    name_plot = 'Temperatur (Metall)'
  }

  var datensatz = {
    x: xValues,
    y: yValues,
    mode: 'scatter',
    name: name_plot
  }

  var datensatz_schmelz = {
    x: xValues,
    y: ySchmelz,
    mode: 'scatter',
    name: name_plot_Schmelz,
    line: {
      color: 'red',
    }

  }

  var data = [datensatz, datensatz_schmelz]

  var layout = {
    title: 'Erwärmung in Abhängigkeit der Zeit',
    height: document.getElementById('Anzeige').clientHeight,
    width: document.getElementById('Anzeige').clientWidth,
    autosize: true,
    xaxis: {
      title: 't in Sekunden',
      ticksuffix: 's'
    },
    yaxis: {
      title: 'Temperatur in °C',
      ticksuffix: '°C'
    }
  };

  Plotly.newPlot('2D_Single', data, layout);
}

function Plot_Multi_2D() {
  var t_max_plot = (t_max + 1) / inkerement_save;
  var xValues = [];
  var yValues = [];
  var ySchmelz = [];
  var step = Math.round(5e-3 / dt); //Verhindern das bei kleiner Auflösung zu viele Werte geplottet werden
  daten = new Array();
  datensatz = new Array();



  for (var ii = 0; ii <= 3; ii++) {
    var xValues = [];
    var yValues = [];
    for (var t = 1; t <= t_max_plot; t++) {
      daten[t] = T_save[z_max_iso][2 / document.getElementById("l_disk").value * (ii + 1)][t];
    }
    for (var t = 1; t <= t_max_plot; t++) {
      yValues.push(daten[t]);
      xValues.push(t * dt * inkerement_save);
    }

    datensatz[ii] = {
      x: xValues,
      y: yValues,
      type: 'scatter',
      name: 'Radius = ' + ((ii + 1) * 2) + " mm"
    };
  }


  if (Isolierung) {
    for (var t = 1; t <= t_max_plot; t++) {
      ySchmelz.push(T_s_iso);
    }
    name_plot_Schmelz = 'Schmelztemperatur Isolierstoff';
  } else {
    for (var t = 1; t <= t_max_plot; t++) {
      ySchmelz.push(T_s);
    }
    name_plot_Schmelz = 'Schmelztemperatur Metall';
  }

  datensatz[4] = {
    x: xValues,
    y: ySchmelz,
    mode: 'scatter',
    name: name_plot_Schmelz,
    line: {
      color: 'red',
    }
  }

  var layout = {
    title: 'Erwärmung in Abhängigkeit der Zeit des äußersten Elements für verschiedene Radien',
    height: document.getElementById('Anzeige').clientHeight,
    width: document.getElementById('Anzeige').clientWidth,
    xaxis: {
      title: 't in Sekunden',
      ticksuffix: 's'
    },
    yaxis: {
      title: 'Temperatur in °C',
      ticksuffix: '°C'
    }
  };
  Plotly.newPlot('2D_Multi', datensatz, layout);
}

function Plotten_3D() {
  var t_max_plot = (t_max + 1) / inkerement_save;
  daten_2 = new Array();
  t_daten = new Array();
  r_daten = new Array();
  for (var r = 0; r <= 50 + 1; r++) {
    daten_2[r] = new Array();
    t_daten[r] = new Array();
    r_daten[r] = new Array();
    for (var t = 0; t <= t_max_plot + 1; t++) {
      daten_2[r][t] = T_save[z_max_iso][r + 1][t + 1];
      t_daten[r][t] = t * dt * inkerement_save;
      r_daten[r][t] = r * l * 1000;
    }
  }

  var data = [{
    x: t_daten,
    y: r_daten,
    z: daten_2,
    type: 'surface',
    colorscale: 'Jet'
  }];

  var layout = {
    title: 'Temperaturverteilung',
    height: document.getElementById('Anzeige').clientHeight,
    width: document.getElementById('Anzeige').clientWidth,
    scene: {
      xaxis: {
        title: 't in Sekunden',
        ticktext: [],
        ticksuffix: 's'
      },
      yaxis: {
        title: 'r in mm',
        ticksuffix: 'mm'
      },
      zaxis: {
        title: 'Temperatur',
        ticksuffix: '°C'

      }
    }
  };
  Plotly.newPlot('3D', data, layout);
}

Kreis();

function Show_Single() {
  if (Firstplot) {
    alert("Sie müssen zuerst die Berechnung durchführen");
  }
  else {
    document.getElementById("2D_Single").hidden = false;
    document.getElementById("2D_Multi").hidden = true;
    document.getElementById("3D").hidden = true;
    document.getElementById("Einstellung-Plotten").hidden = false;
    document.getElementById("Anzeige-Plotten").hidden = false;
  }
}

function Show_Multi() {
  if (Firstplot) {
    alert("Sie müssen zuerst die Berechnung durchführen");
  }
  else {
    document.getElementById("2D_Single").hidden = true;
    document.getElementById("2D_Multi").hidden = false;
    document.getElementById("3D").hidden = true;
    document.getElementById("Anzeige-Plotten").hidden = true;
    document.getElementById("Einstellung-Plotten").hidden = true;
  }
}

function Show_3D() {
  if (Firstplot) {
    alert("Sie müssen zuerst die Berechnung durchführen");
  }
  else {
    document.getElementById("2D_Single").hidden = true;
    document.getElementById("2D_Multi").hidden = true;
    document.getElementById("3D").hidden = false;
    document.getElementById("Anzeige-Plotten").hidden = true;
    document.getElementById("Einstellung-Plotten").hidden = true;
  }
}