/* SPDX-FileCopyrightText: Copyright (c) 2016 - 2026, the adamantine authors.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "MaterialStates.hh"
#define BOOST_TEST_MODULE Integration_Thermoelastic

#include "../applications/adamantine.hh"

#include <boost/property_tree/info_parser.hpp>

#include <filesystem>
#include <fstream>

#include "main.cc"

namespace utf = boost::unit_test;

BOOST_AUTO_TEST_CASE(integration_thermoelastic, *utf::tolerance(1.0e-5))
{
  MPI_Comm communicator = MPI_COMM_WORLD;

  std::vector<adamantine::Timer> timers;
  initialize_timers(communicator, timers);

  // Read the input.
  std::string const filename = "thermoelastic_bare_plate.info";
  adamantine::ASSERT_THROW(std::filesystem::exists(filename) == true,
                           "The file " + filename + " does not exist.");
  boost::property_tree::ptree database;
  boost::property_tree::info_parser::read_info(filename, database);
  database.put("materials.material_0.solid.thermal_expansion_coef", 17.2e-3);

  auto [temperature, displacement] =
      run<3, -1, 3, adamantine::SolidLiquidPowder, dealii::MemorySpace::Host>(
          communicator, database, timers);

  // For now doing a simple regression test. Without a dof handler, it's hard to
  // do something more meaningful with the vector.

  // To generate a new gold solution
  // std::cout << "dis l2:" << displacement.l2_norm() << std::endl;

  BOOST_TEST(displacement.l2_norm() == 0.38717686373752414);
}

BOOST_AUTO_TEST_CASE(integration_thermoelastic_add_material,
                     *utf::tolerance(1.0e-5))
{
  MPI_Comm communicator = MPI_COMM_WORLD;

  std::vector<adamantine::Timer> timers;
  initialize_timers(communicator, timers);

  // Read the input.
  std::string const filename = "thermoelastic_bare_plate.info";
  adamantine::ASSERT_THROW(std::filesystem::exists(filename) == true,
                           "The file " + filename + " does not exist.");
  boost::property_tree::ptree database;
  boost::property_tree::info_parser::read_info(filename, database);
  database.put("geometry.height", 8.0e-3);
  database.put("geometry.height_division", 4);
  database.put("sources.beam_0.scan_path_file",
               "thermoelastic_bare_plate_add_material_scan_path.txt");
  database.put("materials.material_0.solid.thermal_expansion_coef", 17.2e-3);

  auto [temperature, displacement] =
      run<3, 1, 2, adamantine::SolidLiquidPowder, dealii::MemorySpace::Host>(
          communicator, database, timers);

  // For now doing a simple regression test. Without a dof handler, it's hard to
  // do something more meaningful with the vector.

  BOOST_TEST(displacement.l2_norm() == 0.27838526776051664);
}
