// GenFem - A high-level finite element library
// Copyright (C) 2010-2026 Eric Bechet
//
// See the LICENSE file for license information and contributions.
// Please report all bugs and problems to <bechet@cadxfem.org>.
//
// Initial design: Frederic Duboeuf (rev.699)


#ifndef _LINEAR_SYSTEM_ID_H_
#define _LINEAR_SYSTEM_ID_H_

// Interface to a dummy linear system (identity matrix)

#include "GmshMessage.h"
#include "linearSystem.h"
#include "genMatrix.h"
#include <stdlib.h>
#include <set>

template<class scalar>
class linearSystemId : public linearSystem<scalar> {
 private:
  genVector<scalar>* _x;
 public :
  linearSystemId() : _x(0){}
  virtual bool isAllocated() const { return _x != 0; }
  virtual void allocate(int nbRows)
  {
    clear();
    _x = new genVector<scalar>(nbRows);
  }
  virtual ~linearSystemId()
  {
    clear();
  }
  virtual void clear()
  {
    if(_x) delete _x;
    _x = 0;
  }
  virtual void addToMatrix(int row, int col, const scalar &val)
  {
    Msg::Error("addToMatrix : no matrix in linearSystemId");
  }
  virtual void getFromMatrix(int row, int col, scalar &val) const
  {
    Msg::Error("getFromMatrix : no matrix in linearSystemId");
  }
  virtual void addToRightHandSide(int row, const scalar &val, int ith = 0)
  {
    if(val != 0.0) (*_x)(row) += val;
  }
  virtual void addToSolution(int row, const scalar &val)
  {
    if(val != 0.0) (*_x)(row) += val;
  }
  virtual void getFromRightHandSide(int row, scalar &val) const
  {
    val = (*_x)(row);
  }
  virtual void getFromSolution(int row, scalar &val) const
  {
    val = (*_x)(row);
  }
  virtual void zeroMatrix()
  {
    Msg::Error("zeroMatrix : no matrix in linearSystemId");
  }
  virtual void zeroRightHandSide()
  {
    _x->setAll(0.);
  }
  virtual void zeroSolution()
  {
    _x->setAll(0.);
  }
  virtual double normInfRightHandSide() const{
    double nor = 0.;
    double temp;
    for(int i=0;i<_x->size();++i){
      temp = (*_x)(i);
      if(temp<0) temp = -temp;
      if(nor<temp) nor=temp;
    }
    return nor;
  }
  virtual int systemSolve()
  {
    //    _x->print("X in solve");
    return 1;
  }
};

#endif //_LINEAR_SYSTEM_ID_H_
