// Periodic_genTerm - A solver for periodic problems using FEM
// Copyright (C) 2010-2026 Eric Bechet, Frederic Duboeuf
//
// See the LICENSE file for license information and contributions.
// Please report all bugs and problems to <bechet@cadxfem.org> or <duboeuf@outlook.com>.


#include "GmshGlobal.h"
#if defined(HAVE_POST)
#include "PView.h"
#include "PViewData.h"
#endif
#include "periodicSolver.h"

int main (int argc, char* argv[])
{
  if (argc != 2)
  {
    printf("usage : periodic input_file_name\n");
    return -1;
  }

  GmshInitialize(argc, argv);

  // instanciate a solver
  PeriodicSolver mySolver;

  // read the input file
  mySolver.readInputFile(argv[1]);

  // copy boundary conditions
  std::vector<genBoundaryCondition*> keepBC;
  keepBC = mySolver.copyBCs();

  // for different load case
  genMatrix<double> Stiffness(6,6);

  for(int i = 0; i < 6; i ++)
  {
    // select BCs
    mySolver.selectBCs(argv[1],i,keepBC);
    // solve the problem
    mySolver.solve();
    // solve the problem
    mySolver.buildStiffness(i,Stiffness);

    std::ostringstream os_disp;
    os_disp << "disp_ " << i << ".msh";
    std::ostringstream os_energ;
    os_energ << "energ_ " << i << ".msh";
    std::ostringstream os_stress;
    os_stress << "stress_ " << i << ".msh";

    // post process the results
#if defined(HAVE_POST)
    PView* pv;
    pv = mySolver.buildDisplacementView("displacement");
    pv->getData()->writeMSH(os_disp.str(), 3);
    delete pv;
    pv = mySolver.buildElasticEnergyView("elastic energy");
    pv->getData()->writeMSH(os_energ.str(), 3);
    delete pv;
    pv = mySolver.buildStressView("stress");
    pv->getData()->writeMSH(os_stress.str(), 3);
    delete pv;
#endif
  }

  Stiffness.print("Stiffness Matrix");
  // stop gmsh
  GmshFinalize();
}

