#include "../lib/includes.h"
#include "../lib/utils.h"
#include "../lib/methnum.h"

#include "../Enum_type.h"
#include "../Var/var.h"
#include "../T0/t0.h"

#include "upot.h"

void test_der ( Upot * U , U_type upot_t , T0_type t0_t, bool phi_eq_phibar );


#define TD_T(f,df,nf,str) do{   testderiv(f,df,Tmin  ,Tmax  ,N,0.001,nf,str); } while(0)
#define TD_phi(f,df,nf,str) do{ testderiv(f,df,phimin,phimax,N,0.001,nf,str); } while(0)
#define TD_mu(f,df,nf,str) do{  testderiv(f,df,mumin ,mumax ,N,0.001,nf,str); } while(0)

#define TOTO(i) do{ printf("toto %d\n", i); } while(0)

int main ( void )
{
  bool all_test = false ;
  printf ("testing Gluonic potential derivatives \n" );

  bool phi_eq_phibar = true ;

  Upot * U = Upot_alloc_32 ( U_POW, T0_CST, C_ANA, 0.650, phi_eq_phibar );

   test_der ( U , U_POW , T0_T2, true );

  if ( false ){
    test_der ( U , U_POW , T0_CST, phi_eq_phibar );
    test_der ( U , U_POW , T0_T1, phi_eq_phibar  );
    test_der ( U , U_POW , T0_T2, phi_eq_phibar  );
  }

  if ( false )
    {
      test_der ( U , U_POW , T0_T2, phi_eq_phibar  );
      test_der ( U , U_LOG , T0_T2, phi_eq_phibar  );
      test_der ( U , U_FUK , T0_T2, phi_eq_phibar  );
      test_der ( U , U_DEX , T0_T2, phi_eq_phibar  );
    }

  if ( all_test )
    {
      test_der ( U , U_POW , T0_CST, phi_eq_phibar );
      test_der ( U , U_POW , T0_T1, phi_eq_phibar  );
      test_der ( U , U_POW , T0_T2, phi_eq_phibar  );
            
      test_der ( U , U_LOG , T0_CST, phi_eq_phibar );
      test_der ( U , U_LOG , T0_T1, phi_eq_phibar  );
      test_der ( U , U_LOG , T0_T2, phi_eq_phibar  );

      test_der ( U , U_FUK , T0_CST, phi_eq_phibar );

      test_der ( U , U_DEX , T0_CST, phi_eq_phibar );
      test_der ( U , U_DEX , T0_T1, phi_eq_phibar );
      test_der ( U , U_DEX , T0_T2, phi_eq_phibar );
    }

  Upot_free ( U );

  return 0 ;
}



void test_der ( Upot * U , U_type upot_t , T0_type t0_t, bool phi_eq_phibar )
{
  //  Upot_set_type ( U , upot_t, t0_t, ANA );

  double T_ = 0.1 , phi_ = 0.5 , phibar_= 0.7, mu_= 0.1 ;
  double Tmin= 0.1, Tmax=0.5, mumin=0., mumax=0.2, phimin=0.01,phimax=0.1 ;
  int N = 40 ;

  char pref[128], nf[256] ;

  Var * V = Var_alloc ( );

  if ( upot_t == U_POW )
    {
      Upot_set_pow_std ( U , t0_t, phi_eq_phibar);
      if ( phi_eq_phibar )
	{
	  if ( t0_t == T0_CST ) sprintf (pref ,"%s" ,"Upow_T0_cst_phi_eq_phibar" );
	  else if ( t0_t == T0_T1 ) sprintf (pref ,"%s" ,"Upow_T0_t1_phi_eq_phibar" );
	  else if ( t0_t == T0_T2 ) sprintf (pref ,"%s" ,"Upow_T0_t2_phi_eq_phibar" );
	  else { Var_free ( V ); Upot_free ( U ); ERROR ( stdout , "T0 type error"); }
	}
      else
	{
	  if ( t0_t == T0_CST ) sprintf (pref ,"%s" ,"Upow_T0_cst" );
	  else if ( t0_t == T0_T1 ) sprintf (pref ,"%s" ,"Upow_T0_t1" );
	  else if ( t0_t == T0_T2 ) sprintf (pref ,"%s" ,"Upow_T0_t2" );
	  else { Var_free ( V ); Upot_free ( U ); ERROR ( stdout , "T0 type error"); }
	}
    }
  else if ( upot_t == U_LOG )
    {
      Upot_set_log_std ( U , t0_t, phi_eq_phibar);
      if ( phi_eq_phibar )
	{
	  if ( t0_t == T0_CST ) sprintf (pref ,"%s" ,"Ulog_T0_cst_phi_eq_phibar" );
	  else if ( t0_t == T0_T1 ) sprintf (pref ,"%s" ,"Ulog_T0_t1_phi_eq_phibar" );
	  else if ( t0_t == T0_T2 ) sprintf (pref ,"%s" ,"Ulog_T0_t2_phi_eq_phibar" );
	  else { Var_free ( V ); Upot_free ( U ); ERROR ( stdout , "T0 type error");}
	}
      else
	{
	  if ( t0_t == T0_CST ) sprintf (pref ,"%s" ,"Ulog_T0_cst" );
	  else if ( t0_t == T0_T1 ) sprintf (pref ,"%s" ,"Ulog_T0_t1" );
	  else if ( t0_t == T0_T2 ) sprintf (pref ,"%s" ,"Ulog_T0_t2" );
	  else { Var_free ( V ); Upot_free ( U ); ERROR ( stdout , "T0 type error");}
	}
    }
  else if ( upot_t == U_FUK )
    {
      Upot_set_fuk_std ( U , t0_t, 0.650, phi_eq_phibar);
      if ( phi_eq_phibar )
	sprintf (pref ,"%s" ,"Ufuk_phi_eq_phibar" );
      else
	sprintf (pref ,"%s" ,"Ufuk" );
    }  
  else if ( upot_t == U_DEX )
    {
      Upot_set_dex_std ( U , t0_t, phi_eq_phibar);
      if ( phi_eq_phibar )
	{
	  if ( t0_t == T0_CST ) sprintf (pref ,"%s" ,"Udex_T0_cst_phi_eq_phibar" );
	  else if ( t0_t == T0_T1 ) sprintf (pref ,"%s" ,"Udex_T0_t1_phi_eq_phibar" );
	  else if ( t0_t == T0_T2 ) sprintf (pref ,"%s" ,"Udex_T0_t2_phi_eq_phibar" );
	  else { Var_free ( V ); Upot_free ( U );ERROR ( stdout , "T0 type error"); }
	}
      else
	{
	  if ( t0_t == T0_CST ) sprintf (pref ,"%s" ,"Udex_T0_cst" );
	  else if ( t0_t == T0_T1 ) sprintf (pref ,"%s" ,"Udex_T0_t1" );
	  else if ( t0_t == T0_T2 ) sprintf (pref ,"%s" ,"Udex_T0_t2" );
	  else { Var_free ( V ); Upot_free ( U );ERROR ( stdout , "T0 type error"); }
	}
    }
  else
    {
       Var_free ( V ); Upot_free ( U );ERROR ( stdout , "U type error"); 
    }

  
  if ( !phi_eq_phibar )
    {
      //////////// GRAND POTENTIAL
      ////

      double u_phi    ( double phi    ) { Var_set_Tmuphiphibar ( V, T_, mu_, phi , phibar_); return U->Ug( U->P , U->T0, V ); }
      double u_phibar ( double phibar ) { Var_set_Tmuphiphibar ( V, T_, mu_, phi_, phibar ); return U->Ug( U->P , U->T0, V ); }
      double u_T      ( double T      ) { Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->Ug( U->P , U->T0, V ); }
      double u_mu     ( double mu     ) { Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->Ug( U->P , U->T0, V ); }
   
      sprintf ( nf , "%s_phi"   , pref ); plot_2D ( u_phi   , phimin,phimax,N,nf,"set xla \"phi\"; set xzeroaxis"   , false );
      sprintf ( nf , "%s_phibar", pref ); plot_2D ( u_phibar, phimin,phimax,N,nf,"set xla \"phibar\"; set xzeroaxis", false );
      sprintf ( nf , "%s_T"     , pref ); plot_2D ( u_T     , Tmin  ,Tmax  ,N,nf,"set xla \"T\"; set xzeroaxis"     , false );
      sprintf ( nf , "%s_mu"    , pref ); plot_2D ( u_mu    , mumin ,mumax ,N,nf,"set xla \"mu\"; set xzeroaxis"    , false );

      //////////// 1ST DERIVATIVES
      ////

      double dphi_u_phi       ( double phi    ) { Var_set_Tmuphiphibar ( V, T_, mu_, phi , phibar_); return U->dphi_Ug   ( U->P , U->T0, V ); }
      double dphibar_u_phibar ( double phibar ) { Var_set_Tmuphiphibar ( V, T_, mu_, phi_, phibar ); return U->dphibar_Ug( U->P , U->T0, V ); }
      double dT_u_T           ( double T      ) { Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dT_Ug     ( U->P , U->T0, V ); }
      double dmu_u_mu         ( double mu     ) { Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dmu_Ug    ( U->P , U->T0, V ); }

      sprintf ( nf , "dphi_%s_phi"      , pref ); TD_phi(u_phi   ,dphi_u_phi      ,nf,"set xla\"phi\"");
      sprintf ( nf , "dphibar_%s_phibar", pref ); TD_phi(u_phibar,dphibar_u_phibar,nf,"set xla\"phibar\"");
      sprintf ( nf , "dT_%s_T"          , pref ); TD_T  (u_T     ,dT_u_T          ,nf,"set xla\"T\"");
      sprintf ( nf , "dmu_%s_mu"        , pref ); TD_mu (u_mu    ,dmu_u_mu        ,nf,"set xla\"mu\"");


      //////////// 2ND DERIVATIVES
      ////
      double dT2_u_T ( double T ) { Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dT2_Ug     ( U->P , U->T0, V ); }
      double dTmu_u_T ( double T ) { Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dTmu_Ug ( U->P , U->T0, V ); }
      double dmu_u_T  ( double T ) { Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dmu_Ug  ( U->P , U->T0, V ); }
      double dTphi_u_T( double T ) { Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dTphi_Ug ( U->P , U->T0, V ); }
      double dphi_u_T ( double T ) { Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dphi_Ug ( U->P , U->T0, V ); }
      double dTphibar_u_T( double T ) { Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dTphibar_Ug ( U->P , U->T0, V ); }
      double dphibar_u_T ( double T ) { Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dphibar_Ug ( U->P , U->T0, V ); }
      double dmu2_u_mu ( double mu ) { Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dmu2_Ug    ( U->P , U->T0, V ); }
      double dmuphi_u_mu ( double mu ){ Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dmuphi_Ug    ( U->P , U->T0, V ); }
      double dphi_u_mu( double mu ){ Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dphi_Ug    ( U->P , U->T0, V ); }
      double dmuphibar_u_mu ( double mu ){ Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dmuphibar_Ug    ( U->P , U->T0, V ); }
      double dphibar_u_mu( double mu ){ Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dphibar_Ug    ( U->P , U->T0, V ); }
      double dphi2_u_phi ( double phi ) { Var_set_Tmuphiphibar ( V, T_, mu_, phi , phibar_); return U->dphi2_Ug   ( U->P , U->T0, V ); }
      double dphiphibar_u_phi ( double phi ) { Var_set_Tmuphiphibar ( V, T_, mu_, phi , phibar_); return U->dphiphibar_Ug   ( U->P , U->T0, V ); }
      double dphibar_u_phi ( double phi ) { Var_set_Tmuphiphibar ( V, T_, mu_, phi , phibar_); return U->dphibar_Ug   ( U->P , U->T0, V ); }
      double dphibar2_u_phibar ( double phibar ) { Var_set_Tmuphiphibar ( V, T_, mu_, phi_, phibar ); return U->dphibar2_Ug( U->P , U->T0, V ); }

      sprintf ( nf , "dT2_%s_T", pref ); TD_T(dT_u_T, dT2_u_T,nf , "set xla\"T\"");
      sprintf ( nf , "dTmu_%s_T", pref ); TD_T(dmu_u_T, dTmu_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTphi_%s_T", pref ); TD_T(dphi_u_T, dTphi_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTphibar_%s_T", pref ); TD_T(dphibar_u_T, dTphibar_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dmu2_%s_mu", pref ); TD_mu (dmu_u_mu,dmu2_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dmuphi_%s_mu", pref ); TD_mu (dphi_u_mu,dmuphi_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dmuphibar_%s_mu", pref ); TD_mu (dphibar_u_mu,dmuphibar_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dphi2_%s_phi" , pref ); TD_phi(dphi_u_phi,dphi2_u_phi,nf,"set xla\"phi\"");
      sprintf ( nf , "dphiphibar_%s_phi" , pref ); TD_phi(dphibar_u_phi,dphiphibar_u_phi,nf,"set xla\"phi\"");
      sprintf ( nf , "dphibar2_%s_phibar", pref ); TD_phi(dphibar_u_phibar,dphibar2_u_phibar,nf,"set xla\"phibar\"");

      //////////// 3RD DERIVATIVES
      ////
  
      double dT3_u_T ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dT3_Ug ( U->P , U->T0, V ); } 
      double dT2mu_u_T ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dT2mu_Ug ( U->P , U->T0, V ); }
      double dT2phi_u_T ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dT2phi_Ug ( U->P , U->T0, V ); }
      double dT2phibar_u_T ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dT2phibar_Ug ( U->P , U->T0, V ); }
      double dTmu2_u_T ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dTmu2_Ug ( U->P , U->T0, V ); }
      double dmu2_u_T  ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dmu2_Ug ( U->P , U->T0, V ); }
      double dTmuphi_u_T ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dTmuphi_Ug ( U->P , U->T0, V ); }
      double dmuphi_u_T  ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dmuphi_Ug ( U->P , U->T0, V ); }
      double dTmuphibar_u_T ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dTmuphibar_Ug ( U->P , U->T0, V ); }
      double dmuphibar_u_T  ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dmuphibar_Ug ( U->P , U->T0, V ); }
      double dTphi2_u_T ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dTphi2_Ug ( U->P , U->T0, V ); }
      double dphi2_u_T  ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dphi2_Ug ( U->P , U->T0, V ); }
      double dTphiphibar_u_T ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dTphiphibar_Ug ( U->P , U->T0, V ); }
      double dphiphibar_u_T  ( double T ) {  Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dphiphibar_Ug ( U->P , U->T0, V ); }
      double dTphibar2_u_T ( double T ) { Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dTphibar2_Ug ( U->P , U->T0, V ); }
      double dphibar2_u_T ( double T ) { Var_set_Tmuphiphibar ( V, T , mu_, phi_, phibar_); return U->dphibar2_Ug ( U->P , U->T0, V ); }
      double dmu3_u_mu ( double mu ) { Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dmu3_Ug    ( U->P , U->T0, V ); } 
      double dmu2phi_u_mu ( double mu ) { Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dmu2phi_Ug    ( U->P , U->T0, V ); } 
      double dmu2phibar_u_mu ( double mu ) { Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dmu2phibar_Ug    ( U->P , U->T0, V ); } 
      double dmuphi2_u_mu ( double mu ) { Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dmuphi2_Ug    ( U->P , U->T0, V ); } 
      double dphi2_u_mu  ( double mu ) {Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dphi2_Ug    ( U->P , U->T0, V ); } 
      double dmuphiphibar_u_mu ( double mu ) { Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dmuphiphibar_Ug    ( U->P , U->T0, V ); } 
      double dphiphibar_u_mu  ( double mu ) {Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dphiphibar_Ug    ( U->P , U->T0, V ); } 
      double dmuphibar2_u_mu ( double mu ) { Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dmuphibar2_Ug    ( U->P , U->T0, V ); } 
      double dphibar2_u_mu ( double mu ) { Var_set_Tmuphiphibar ( V, T_, mu , phi_, phibar_); return U->dphibar2_Ug    ( U->P , U->T0, V ); } 
      double dphi3_u_phi ( double phi ) { Var_set_Tmuphiphibar ( V, T_, mu_, phi , phibar_); return U->dphi3_Ug   ( U->P , U->T0, V ); }    
      double dphi2phibar_u_phi ( double phi ) { Var_set_Tmuphiphibar ( V, T_, mu_, phi , phibar_); return U->dphi2phibar_Ug   ( U->P , U->T0, V ); }    
      double dphiphibar2_u_phi ( double phi ) {Var_set_Tmuphiphibar ( V, T_, mu_, phi , phibar_); return U->dphiphibar2_Ug   ( U->P , U->T0, V ); }
      double dphibar2_u_phi ( double phi ){Var_set_Tmuphiphibar ( V, T_, mu_, phi , phibar_); return U->dphibar2_Ug   ( U->P , U->T0, V ); }
      double dphibar3_u_phibar ( double phibar )  { Var_set_Tmuphiphibar( V, T_, mu_, phi_, phibar ); return U->dphibar3_Ug( U->P , U->T0, V ); }

      sprintf ( nf , "dT3_%s_T", pref ); TD_T(dT2_u_T, dT3_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dT2mu_%s_T", pref ); TD_T(dTmu_u_T, dT2mu_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dT2phi_%s_T", pref ); TD_T(dTphi_u_T, dT2phi_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dT2phibar_%s_T", pref ); TD_T(dTphibar_u_T, dT2phibar_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTmu2_%s_T", pref ); TD_T(dmu2_u_T, dTmu2_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTmuphi_%s_T", pref ); TD_T(dmuphi_u_T, dTmuphi_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTmuphibar_%s_T", pref ); TD_T(dmuphibar_u_T, dTmuphibar_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTphi2_%s_T", pref ); TD_T(dphi2_u_T, dTphi2_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTphiphibar_%s_T", pref ); TD_T(dphiphibar_u_T, dTphiphibar_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTphibar2_%s_T", pref ); TD_T(dphibar2_u_T, dTphibar2_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dmu3_%s_mu", pref ); TD_mu (dmu2_u_mu,dmu3_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dmu2phi_%s_mu", pref ); TD_mu (dmuphi_u_mu,dmu2phi_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dmu2phibar_%s_mu", pref ); TD_mu (dmuphibar_u_mu,dmu2phibar_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dmuphi2_%s_mu", pref ); TD_mu (dphi2_u_mu,dmuphi2_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dmuphiphibar_%s_mu", pref ); TD_mu (dphiphibar_u_mu,dmuphiphibar_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dmuphibar2_%s_mu", pref ); TD_mu (dphibar2_u_mu,dmuphibar2_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dphi3_%s_phi", pref ); TD_phi(dphi2_u_phi,dphi3_u_phi,nf,"set xla\"phi\"");
      sprintf ( nf , "dphiphibar_%s_phi", pref ); TD_phi(dphiphibar_u_phi,dphi2phibar_u_phi,nf,"set xla\"phi\"");
      sprintf ( nf , "dphiphibar2_%s_phi", pref ); TD_phi(dphibar2_u_phi,dphiphibar2_u_phi,nf,"set xla\"phi\"");
      sprintf ( nf , "dphibar3_%s_phibar", pref ); TD_phi(dphibar2_u_phibar,dphibar3_u_phibar,nf,"set xla\"phibar\"");
    }
  else // phi = phibar
    {
      //////////// GRAND POTENTIAL
      ////

      double u_phi    ( double phi    ) { Var_set_Tmuphi ( V, T_, mu_, phi ); return U->Ug( U->P , U->T0, V ); }
      double u_T      ( double T      ) { Var_set_Tmuphi ( V, T , mu_, phi_); return U->Ug( U->P , U->T0, V ); }
      double u_mu     ( double mu     ) { Var_set_Tmuphi ( V, T_, mu , phi_); return U->Ug( U->P , U->T0, V ); }
   
      sprintf ( nf , "%s_phi"   , pref ); plot_2D ( u_phi   , phimin,phimax,N,nf,"set xla \"phi\"; set xzeroaxis"   , false );
      sprintf ( nf , "%s_T"     , pref ); plot_2D ( u_T     , Tmin  ,Tmax  ,N,nf,"set xla \"T\"; set xzeroaxis"     , false );
      sprintf ( nf , "%s_mu"    , pref ); plot_2D ( u_mu    , mumin ,mumax ,N,nf,"set xla \"mu\"; set xzeroaxis"    , false );

      //////////// 1ST DERIVATIVES
      ////

      double dphi_u_phi       ( double phi    ) { Var_set_Tmuphi ( V, T_, mu_, phi ); return U->dphi_Ug   ( U->P , U->T0, V ); }
      double dT_u_T           ( double T      ) { Var_set_Tmuphi ( V, T , mu_, phi_); return U->dT_Ug     ( U->P , U->T0, V ); }
      double dmu_u_mu         ( double mu     ) { Var_set_Tmuphi ( V, T_, mu , phi_); return U->dmu_Ug    ( U->P , U->T0, V ); }

      sprintf ( nf , "dphi_%s_phi"      , pref ); TD_phi(u_phi   ,dphi_u_phi      ,nf,"set xla\"phi\"");
      sprintf ( nf , "dT_%s_T"          , pref ); TD_T  (u_T     ,dT_u_T          ,nf,"set xla\"T\"");
      sprintf ( nf , "dmu_%s_mu"        , pref ); TD_mu (u_mu    ,dmu_u_mu        ,nf,"set xla\"mu\"");


      //////////// 2ND DERIVATIVES
      ////
      double dT2_u_T ( double T ) { Var_set_Tmuphi ( V, T , mu_, phi_); return U->dT2_Ug     ( U->P , U->T0, V ); }
      double dTmu_u_T ( double T ) { Var_set_Tmuphi ( V, T , mu_, phi_); return U->dTmu_Ug ( U->P , U->T0, V ); }
      double dmu_u_T  ( double T ) { Var_set_Tmuphi ( V, T , mu_, phi_); return U->dmu_Ug  ( U->P , U->T0, V ); }
      double dTphi_u_T( double T ) { Var_set_Tmuphi ( V, T , mu_, phi_); return U->dTphi_Ug ( U->P , U->T0, V ); }
      double dphi_u_T ( double T ) { Var_set_Tmuphi ( V, T , mu_, phi_); return U->dphi_Ug ( U->P , U->T0, V ); }
      double dmu2_u_mu ( double mu ) { Var_set_Tmuphi ( V, T_, mu , phi_); return U->dmu2_Ug    ( U->P , U->T0, V ); }
      double dmuphi_u_mu ( double mu ){ Var_set_Tmuphi ( V, T_, mu , phi_); return U->dmuphi_Ug    ( U->P , U->T0, V ); }
      double dphi_u_mu( double mu ){ Var_set_Tmuphi ( V, T_, mu , phi_); return U->dphi_Ug    ( U->P , U->T0, V ); }
      double dphi2_u_phi ( double phi ) { Var_set_Tmuphi ( V, T_, mu_, phi ); return U->dphi2_Ug   ( U->P , U->T0, V ); }

      //if ( false ) { 
      sprintf ( nf , "dT2_%s_T", pref ); TD_T(dT_u_T, dT2_u_T,nf , "set xla\"T\"");
      sprintf ( nf , "dTmu_%s_T", pref ); TD_T(dmu_u_T, dTmu_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTphi_%s_T", pref ); TD_T(dphi_u_T, dTphi_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dmu2_%s_mu", pref ); TD_mu (dmu_u_mu,dmu2_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dmuphi_%s_mu", pref ); TD_mu (dphi_u_mu,dmuphi_u_mu ,nf,"set xla\"mu\"");   //}
      sprintf ( nf , "dphi2_%s_phi" , pref ); TD_phi(dphi_u_phi,dphi2_u_phi,nf,"set xla\"phi\""); //}

      //////////// 3RD DERIVATIVES
      ////
  
      double dT3_u_T ( double T ) {  Var_set_Tmuphi ( V, T , mu_, phi_); return U->dT3_Ug ( U->P , U->T0, V ); } 
      double dT2mu_u_T ( double T ) {  Var_set_Tmuphi ( V, T , mu_, phi_); return U->dT2mu_Ug ( U->P , U->T0, V ); }
      double dT2phi_u_T ( double T ) {  Var_set_Tmuphi ( V, T , mu_, phi_); return U->dT2phi_Ug ( U->P , U->T0, V ); }
      double dTmu2_u_T ( double T ) {  Var_set_Tmuphi ( V, T , mu_, phi_); return U->dTmu2_Ug ( U->P , U->T0, V ); }
      double dmu2_u_T  ( double T ) {  Var_set_Tmuphi ( V, T , mu_, phi_); return U->dmu2_Ug ( U->P , U->T0, V ); }
      double dTmuphi_u_T ( double T ) {  Var_set_Tmuphi ( V, T , mu_, phi_); return U->dTmuphi_Ug ( U->P , U->T0, V ); }
      double dmuphi_u_T  ( double T ) {  Var_set_Tmuphi ( V, T , mu_, phi_); return U->dmuphi_Ug ( U->P , U->T0, V ); }
      double dTphi2_u_T ( double T ) {  Var_set_Tmuphi ( V, T , mu_, phi_); return U->dTphi2_Ug ( U->P , U->T0, V ); }
      double dphi2_u_T  ( double T ) {  Var_set_Tmuphi ( V, T , mu_, phi_); return U->dphi2_Ug ( U->P , U->T0, V ); }
      double dmu3_u_mu ( double mu ) { Var_set_Tmuphi ( V, T_, mu , phi_); return U->dmu3_Ug    ( U->P , U->T0, V ); } 
      double dmu2phi_u_mu ( double mu ) { Var_set_Tmuphi ( V, T_, mu , phi_); return U->dmu2phi_Ug    ( U->P , U->T0, V ); } 
      double dmuphi2_u_mu ( double mu ) { Var_set_Tmuphi ( V, T_, mu , phi_); return U->dmuphi2_Ug    ( U->P , U->T0, V ); } 
      double dphi2_u_mu  ( double mu ) {Var_set_Tmuphi ( V, T_, mu , phi_); return U->dphi2_Ug    ( U->P , U->T0, V ); } 
      double dphi3_u_phi ( double phi ) { Var_set_Tmuphi ( V, T_, mu_, phi ); return U->dphi3_Ug   ( U->P , U->T0, V ); }    

      //if ( false ){
      sprintf ( nf , "dT3_%s_T", pref ); TD_T(dT2_u_T, dT3_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dT2mu_%s_T", pref ); TD_T(dTmu_u_T, dT2mu_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dT2phi_%s_T", pref ); TD_T(dTphi_u_T, dT2phi_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTmu2_%s_T", pref ); TD_T(dmu2_u_T, dTmu2_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTmuphi_%s_T", pref ); TD_T(dmuphi_u_T, dTmuphi_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dTphi2_%s_T", pref ); TD_T(dphi2_u_T, dTphi2_u_T,nf,"set xla\"T\"");
      sprintf ( nf , "dmu3_%s_mu", pref ); TD_mu (dmu2_u_mu,dmu3_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dmu2phi_%s_mu", pref ); TD_mu (dmuphi_u_mu,dmu2phi_u_mu ,nf,"set xla\"mu\"");
      sprintf ( nf , "dmuphi2_%s_mu", pref ); TD_mu (dphi2_u_mu,dmuphi2_u_mu ,nf,"set xla\"mu\"");    // }
      sprintf ( nf , "dphi3_%s_phi", pref ); TD_phi(dphi2_u_phi,dphi3_u_phi,nf,"set xla\"phi\"");
    

      /* double dphi2_u ( double phi ) */
      /* { */
      /* 	Var_set_Tmuphi( V, T_, mu_, phi ); */
      /* 	double dphi_u( double phi ) */
      /* 	{ */
      /* 	  Var_set_Tmuphi( V, T_, mu_, phi); */
      /* 	  double u ( double phi ) */
      /* 	  { */
      /* 	    Var_set_Tmuphi( V, T_, mu_, phi); */
      /* 	    return upot_pow_phi_eq_phibar( U->P, U->T0, V ); */
      /* 	  } */
      /* 	  return deriv ( u, phi, 0.001 ) ; */
      /* 	} */
      /* 	return deriv( dphi_u, phi, 0.001 ) ; */
      /* } */

      /* sprintf( nf, "all-num__dphi3_%s_phi", pref ) ; TD_phi( dphi2_u, dphi3_u_phi,nf, "set xla\"phi\"" ); */

	  
    }

  Var_free ( V );
}
