#include "absmc3D.h"

using namespace absmc;
const std::string solverName = "pressurization";

/// Perform vessel pressurization using displacements generated by FE simulation
int main(int argc, char *argv[])
{
    if (argc!=2) {
        std::cout << "Usage: " << solverName << " configDir/configName" << std::endl;
        return EXIT_FAILURE;
    }

    /// read config filename; output to the config directory
    const std::string configFileName(argv[1]);
    AgentSolver<3> slv(solverName);
    slv.loadConfig(configFileName);

    /// agent input files
    slv.readIOFileNames("deploy_input_file");
    slv.readCenterlineFromFile();

    slv.readCharLengthScales();

    /// load agents from file; includes vessel wall agents. Obstacles are loaded separately.
    slv.readDat3DFile("stage2." + slv.getBaseName() + ".dat");
    slv.initVerletNeighbourDetector();
    slv.readBondsFromFile("stage2." + slv.getBaseName() + "_nb.dat");

    slv.readWallDisplacements();
    slv.setIterationParametersFromDisplacements("deploy_vtk_iter","deploy_dat_iter");


    /// set agents at longitudinal (x) domain boundaries immobile;
    slv.setBoundaryMobility("deploy_boundary_mobility_x", "deploy_boundary_mobility_y", "deploy_boundary_mobility_z");

    slv.detectSurface();
    slv.setDeploymentRules();

    // double unaryMagnitude = 7.794e-5; // 7.794e-5 = inflation test to 1 atm for r = 0.015 mm
    //double unaryMagnitudeInflation = 1.039e-5; // 1.039e-5 = inflation test to 100 mmHg (13 333 Pa) for r = 0.015 mm

    UnaryTrajectoryForce trajForce = slv.createWallTrajectoryForce();
    slv.setForces(&slv.bForce, &slv.threeLayerForce, &trajForce);

    slv.calculateCharForce();

    slv.initializeIntegrator();
    slv.setFixedIntervalController("deploy_iter_time");

    // vtp output parameters
    slv.readVtpParameters("deploy_vtp_scalars", "deploy_vtp_vectors");

    slv.iterateWithTrajectories(trajForce);

    slv.setSurfaceImmobile();
    logger() << "Doing post-pressurization equilibration..." << std::endl;

    slv.setForceResidualMaxNormController("deploy_convergence_level");
    slv.finalIterateWithTrajectories(trajForce);

    /// write the final configuration to file
    logger() << std::endl << "Pressurization complete, writing output file" << std::endl;
    slv.writeAllOutputs("stage3.");

    return EXIT_SUCCESS;
}
