// This file is part of the Herezh++ application.
//
// The finite element software Herezh++ is dedicated to the field
// of mechanics for large transformations of solid structures.
// It is developed by Gérard Rio (APP: IDDN.FR.010.0106078.000.R.P.2006.035.20600)
// INSTITUT DE RECHERCHE DUPUY DE LÔME (IRDL) <https://www.irdl.fr/>.
//
// Herezh++ is distributed under GPL 3 license ou ultérieure.
//
// Copyright (C) 1997-2022 Université Bretagne Sud (France)
// AUTHOR : Gérard Rio
// E-MAIL  : gerardrio56@free.fr
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// For more information, please consult: <https://herezh.irdl.fr/>.

#include "PtIntegThermiInterne.h"

#include "NevezTenseur.h"
#include "Tenseur3.h" 
#include "Tenseur2.h" 

// contructeur par défaut
PtIntegThermiInterne::PtIntegThermiInterne():
  temperature(0.),temperature_t(0.)
  ,gradTB(),dgradTB(),deltaGradTB(),fluxH(),fluxH_t()
  ,norme_gradT(),norme_dGradT(),norme_flux()
  ,tpsMetrique(),tps_cpu_loi_comp()
    {};
    
// contructeur fonction de la dimension
PtIntegThermiInterne::PtIntegThermiInterne(int dimcoor) :
  temperature(0.),temperature_t(0.)
  ,gradTB(dimcoor),dgradTB(dimcoor),deltaGradTB(dimcoor)
  ,fluxH(dimcoor),fluxH_t(dimcoor)
  ,norme_gradT(0.),norme_dGradT(0.),norme_flux(0.)
  ,tpsMetrique(),tps_cpu_loi_comp()
    { };
    
// contructeur de copie
PtIntegThermiInterne::PtIntegThermiInterne(const PtIntegThermiInterne& pti):
  temperature(pti.temperature),temperature_t(pti.temperature_t)
  ,gradTB(pti.gradTB),dgradTB(pti.dgradTB),deltaGradTB(pti.deltaGradTB)
  ,fluxH(pti.fluxH),fluxH_t(pti.fluxH_t)
  ,norme_gradT(pti.norme_gradT),norme_dGradT(pti.norme_dGradT)
  ,norme_flux(pti.norme_flux)
  ,tpsMetrique(pti.tpsMetrique),tps_cpu_loi_comp(pti.tps_cpu_loi_comp)
	{	};
    
// DESTRUCTEUR :
PtIntegThermiInterne::~PtIntegThermiInterne()
{ };
    
// Surcharge de l'operateur = 
PtIntegThermiInterne& PtIntegThermiInterne::operator= ( const PtIntegThermiInterne& pti)
{ temperature=pti.temperature;temperature_t=pti.temperature_t;
  gradTB=pti.gradTB;dgradTB=pti.dgradTB;deltaGradTB=pti.deltaGradTB;
  fluxH=pti.fluxH;fluxH_t=pti.fluxH_t;
  norme_gradT=pti.norme_gradT;norme_dGradT=pti.norme_dGradT;
  norme_flux=pti.norme_flux;
  // --- les temps cpu
  tpsMetrique=pti.tpsMetrique;tps_cpu_loi_comp=pti.tps_cpu_loi_comp;
 
 // retour
 return *this;

};
    
//============= lecture écriture dans base info ==========
// cas donne le niveau de la récupération
// = 1 : on récupère tout
// = 2 : on récupère uniquement les données variables (supposées comme telles)
void PtIntegThermiInterne::Lecture_base_info (istream& ent,const int )
 {  // lecture des différents éléments que l'on trouve utiles (pour l'instant)
    string nom;
    ent >> nom     >> temperature >> nom >> temperature_t;
    ent >> nom     >> gradTB
        >> nom     >> dgradTB
        >> nom     >> deltaGradTB;
    ent >> nom     >> fluxH_t;
    ent >> nom     >> norme_gradT
        >> nom     >> norme_dGradT
        >> nom     >> norme_flux ;
    // temps cpu
    ent >> nom >> tpsMetrique >> nom >> tps_cpu_loi_comp;
 };

// cas donne le niveau de sauvegarde
// = 1 : on sauvegarde tout
// = 2 : on sauvegarde uniquement les données variables (supposées comme telles)
void PtIntegThermiInterne::Ecriture_base_info(ostream& sort,const int )
 { // les différents éléments que l'on considère intéressant (pour l'instant)
   sort << "\n temperature= " << temperature << " temperature_t= " << temperature_t;
   sort << "\n gradTB= "        << gradTB
        << " dgradTB= "      << dgradTB
        << " deltaGradTB= "  << deltaGradTB
        << "\n fluxDH= "      << fluxH_t;
   sort << "\n norme_gradT " << norme_gradT
        << " norme_dGradT= " << norme_dGradT
        << " norme_fluxD= "   << norme_flux ;
   sort << "\n tpsmetrique= " << tpsMetrique << " tpsLoiComp= " << tps_cpu_loi_comp;
 };
    

// actualisation des grandeurs actives de t+dt vers t, pour celles qui existent 
// sous ces deux formes    
void PtIntegThermiInterne::TdtversT()
	{ fluxH_t = fluxH;temperature_t=temperature;};
	
// actualisation des grandeurs actives de t vers tdt, pour celles qui existent 
// sous ces deux formes          
void PtIntegThermiInterne::TversTdt()
	{ fluxH = fluxH_t;temperature=temperature_t;};

// plusZero: = true: indique qu'il faut complèter les grandeurs manquantes avec des 0
//           = false: on ne complète pas
// il faut que ptintmec comporte des tenseurs d'ordre 2 et this des tenseurs 3D
void PtIntegThermiInterne::Affectation_2D_a_3D(const PtIntegThermiInterne& ptintmec,bool plusZero)
{ // vérif éventuelle des dimensions
 #ifdef MISE_AU_POINT
 {//  - les dimensions
  if (gradTB.Dimension() != 3)
	  {cout << "\n *** erreur la dimension de this->gradTB ("<<gradTB.Dimension() <<") est different de 3"
	        << "\n PtIntegThermiInterne::Affectation_2D_a_3D(.."; Sortie(1);};
  if (ptintmec.gradTB.Dimension() != 2)
	  {cout << "\n *** erreur la dimension de ptintmec.gradTB ("<< ptintmec.gradTB.Dimension() <<") est different de 2"
	        << "\n PtIntegThermiInterne::Affectation_2D_a_3D(.."; Sortie(1);};
  }
 #endif
  // pour tout ce qui est def
  temperature_t=ptintmec.temperature_t;
  temperature=ptintmec.temperature;
  gradTB(1) = ptintmec.gradTB(1);gradTB(2) = ptintmec.gradTB(2);
  dgradTB(1) = ptintmec.dgradTB(1);dgradTB(2) = ptintmec.dgradTB(2);
  deltaGradTB(1) = ptintmec.deltaGradTB(1);deltaGradTB(2) = ptintmec.deltaGradTB(2);
  fluxH(1) = ptintmec.fluxH(1);fluxH(2) = ptintmec.fluxH(2);
  fluxH_t(1) = ptintmec.fluxH_t(1);fluxH_t(2) = ptintmec.fluxH_t(2);
  if (plusZero)
   {gradTB(3) = 0.;
    dgradTB(3) = 0.;
    deltaGradTB(3) = 0.;
    fluxH(3) = 0.;
    fluxH_t(3) = 0.;
   };
  norme_gradT = ptintmec.norme_gradT;
  norme_dGradT = ptintmec.norme_dGradT;
  norme_flux = ptintmec.norme_flux;
  // --- les temps cpu
  tpsMetrique=ptintmec.tpsMetrique;tps_cpu_loi_comp=ptintmec.tps_cpu_loi_comp;
};

// l'inverse: comme le conteneur d'arrivée est plus petit, il n'y a pas de complétion
// il faut que ptintmec comporte des tenseurs d'ordre 3 et this des tenseurs 2D
void PtIntegThermiInterne::Affectation_3D_a_2D(const PtIntegThermiInterne& ptintmec)
{ // vérif éventuelle des dimensions
 #ifdef MISE_AU_POINT
 {//  - les dimensions
  if (gradTB.Dimension() != 2)
	  {cout << "\n *** erreur la dimension de this.gradTB ("<<gradTB.Dimension() <<") est different de 2"
	        << "\n PtIntegThermiInterne::Affectation_3D_a_2D(.."; Sortie(1);};
  if (ptintmec.gradTB.Dimension() != 3)
	  {cout << "\n *** erreur la dimension de ptintmec.gradTB ("<< ptintmec.gradTB.Dimension() <<") est different de 3"
	        << "\n PtIntegThermiInterne::Affectation_3D_a_2D(.."; Sortie(1);};
  }
 #endif
  // pour tout ce qui est def
  temperature_t=ptintmec.temperature_t;
  temperature=ptintmec.temperature;
  gradTB(1) = ptintmec.gradTB(1);gradTB(2) = ptintmec.gradTB(2);
  dgradTB(1) = ptintmec.dgradTB(1);dgradTB(2) = ptintmec.dgradTB(2);
  deltaGradTB(1) = ptintmec.deltaGradTB(1);deltaGradTB(2) = ptintmec.deltaGradTB(2);
  fluxH(1) = ptintmec.fluxH(1);fluxH(2) = ptintmec.fluxH(2);
  fluxH_t(1) = ptintmec.fluxH_t(1);fluxH_t(2) = ptintmec.fluxH_t(2);
  norme_gradT = ptintmec.norme_gradT;
  norme_dGradT = ptintmec.norme_dGradT;
  norme_flux = ptintmec.norme_flux;
  // --- les temps cpu
  tpsMetrique=ptintmec.tpsMetrique;tps_cpu_loi_comp=ptintmec.tps_cpu_loi_comp;
};
	     

// plusZero: = true: indique qu'il faut complèter les grandeurs manquantes avec des 0
//           = false: on ne complète pas
// il faut que ptintmec comporte des tenseurs d'ordre 1 et this des tenseurs 3D
void PtIntegThermiInterne::Affectation_1D_a_3D(const PtIntegThermiInterne& ptintmec,bool plusZero)
{ // vérif éventuelle des dimensions
 #ifdef MISE_AU_POINT
 {//  - les dimensions
  if (gradTB.Dimension() != 3)
	  {cout << "\n *** erreur la dimension de this.gradTB ("<<gradTB.Dimension() <<") est different de 3"
	        << "\n PtIntegThermiInterne::Affectation_1D_a_3D(.."; Sortie(1);};
  if (ptintmec.gradTB.Dimension() != 1)
	  {cout << "\n *** erreur la dimension de ptintmec.gradTB ("<< ptintmec.gradTB.Dimension() <<") est different de 1"
	        << "\n PtIntegThermiInterne::Affectation_1D_a_3D(.."; Sortie(1);};
  }
 #endif
  // pour tout ce qui est def
  temperature_t=ptintmec.temperature_t;
  temperature=ptintmec.temperature;
  gradTB(1) = ptintmec.gradTB(1);
  dgradTB(1) = ptintmec.dgradTB(1);
  deltaGradTB(1) = ptintmec.deltaGradTB(1);
  fluxH(1) = ptintmec.fluxH(1);
  fluxH_t(1) = ptintmec.fluxH_t(1);
  if (plusZero)
   {gradTB(3) = gradTB(2) = 0.;
    dgradTB(3) = dgradTB(2) = 0.;
    deltaGradTB(3) = deltaGradTB(2) = 0.;
    fluxH(3) = fluxH(2) = 0.;
    fluxH_t(3) = fluxH_t(2) = 0.;
   };
  norme_gradT = ptintmec.norme_gradT;
  norme_dGradT = ptintmec.norme_dGradT;
  norme_flux = ptintmec.norme_flux;
  // --- les temps cpu
  tpsMetrique=ptintmec.tpsMetrique;tps_cpu_loi_comp=ptintmec.tps_cpu_loi_comp;
 
};

// l'inverse: comme le conteneur d'arrivée est plus petit, il n'y a pas de complétion
// il faut que ptintmec comporte des tenseurs d'ordre 3 et this des tenseurs 1D
void PtIntegThermiInterne::Affectation_3D_a_1D(const PtIntegThermiInterne& ptintmec)
{ // vérif éventuelle des dimensions
 #ifdef MISE_AU_POINT
 {  //  - les dimensions
  if (gradTB.Dimension() != 1)
	  {cout << "\n *** erreur la dimension de this.gradTB ("<<gradTB.Dimension() <<") est different de 1"
	        << "\n PtIntegThermiInterne::Affectation_3D_a_1D(.."; Sortie(1);};
  if (ptintmec.gradTB.Dimension() != 3)
	  {cout << "\n *** erreur la dimension de ptintmec.gradTB ("<< ptintmec.gradTB.Dimension() <<") est different de 3"
	        << "\n PtIntegThermiInterne::Affectation_3D_a_1D(.."; Sortie(1);};
  }
 #endif
  // pour tout ce qui est def
  temperature_t=ptintmec.temperature_t;
  temperature=ptintmec.temperature;
  gradTB(1) = ptintmec.gradTB(1);
  dgradTB(1) = ptintmec.dgradTB(1);
  deltaGradTB(1) = ptintmec.deltaGradTB(1);
  fluxH(1) = ptintmec.fluxH(1);
  fluxH_t(1) = ptintmec.fluxH_t(1);
  norme_gradT = ptintmec.norme_gradT;
  norme_dGradT = ptintmec.norme_dGradT;
  norme_flux = ptintmec.norme_flux;
  // --- les temps cpu
  tpsMetrique=ptintmec.tpsMetrique;tps_cpu_loi_comp=ptintmec.tps_cpu_loi_comp;
 
};
	     
// surcharge de l'operateur de lecture
istream & operator >> (istream & entree, PtIntegThermiInterne & pti)
  { // vérification du type
    string nom;
    entree >> nom;
   #ifdef MISE_AU_POINT
    if (nom != "PtIntegThermiInterne")
     		{	cout << "\nErreur, en lecture d'une instance  PtIntegThermiInterne "
	             << " on attendait PtIntegThermiInterne et on a lue: " << nom ;
      			cout << "istream & operator >> (istream & entree, PtIntegThermiInterne & pti)\n";
      			Sortie(1);
     		};
   #endif
    // puis lecture des différents éléments
    entree >> nom     >> pti.temperature >> nom >> pti.temperature_t;
    entree >> nom     >> pti.gradTB
           >> nom     >> pti.dgradTB
           >> nom     >> pti.deltaGradTB;
    entree >> nom     >> pti.fluxH_t;
    entree >> nom     >> pti.norme_gradT
           >> nom     >> pti.norme_dGradT
           >> nom     >> pti.norme_flux ;
    // --- les temps cpu
    entree >> nom >> pti.tpsMetrique >> nom >> pti.tps_cpu_loi_comp;

    return entree;
  };
  
// surcharge de l'operateur d'ecriture 
ostream & operator << ( ostream & sort,const  PtIntegThermiInterne & pti)
  { // tout d'abord un indicateur donnant le type
    sort << "PtIntegThermiInterne " ; 
    // puis les différents éléments
    sort << "\n temperature= " << pti.temperature << " temperature_t= " << pti.temperature_t;
    sort << "\n gradTB= "     << pti.gradTB
         << " dgradTB= "      << pti.dgradTB
         << " deltaGradTB= "  << pti.deltaGradTB
         << "\n fluxH= "      << pti.fluxH_t;
    sort << "\n norme_gradT " << pti.norme_gradT
         << " norme_dGradT= " << pti.norme_dGradT
         << " norme_flux= "   << pti.norme_flux ;
    sort << "\n tpsmetrique= " << pti.tpsMetrique << " tpsLoiComp= " << pti.tps_cpu_loi_comp;
    return sort;
  };