/*******************************************************************************
 * Copyright (C) 2017-2026 Theodore Chang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

#include "T3D2S.h"

#include <Domain/DomainBase.h>
#include <Element/Utility/T3DC.h>
#include <Section/Section.h>

T3D2S::T3D2S(const unsigned T, uvec&& N, const unsigned M, const bool F, const bool LS)
    : SectionElement1D(T, t_node, t_dof, std::move(N), uvec{M}, F, {Node::DOF::U1, Node::DOF::U2, Node::DOF::U3})
    , t_trans(F ? std::make_unique<T3DC>() : std::make_unique<T3DL>())
    , log_strain(LS) {}

int T3D2S::initialize(const shared_ptr<DomainBase>& D) {
    t_trans->set_element_ptr(this);

    access::rw(length) = t_trans->get_length();

    t_section = D->get<Section>(section_tag(0))->unique_copy();

    trial_stiffness = current_stiffness = initial_stiffness = t_trans->to_global_stiffness_mat(t_section->get_initial_stiffness() / length);

    if(const auto linear_density = t_section->get_linear_density(); linear_density > 0.) trial_mass = current_mass = initial_mass = t_trans->to_global_mass_mat(linear_density);

    return SUANPAN_SUCCESS;
}

int T3D2S::update_status() {
    t_trans->update_status();

    if(nlgeom) {
        const auto new_length = t_trans->get_length();

        double d_strain;

        if(t_section->update_trial_status(log_strain ? log((d_strain = new_length) / length) : new_length / (d_strain = length) - 1.) != SUANPAN_SUCCESS) return SUANPAN_FAIL;

        trial_stiffness = t_trans->to_global_stiffness_mat(t_section->get_trial_stiffness() / d_strain);
        trial_geometry = t_trans->to_global_geometry_mat(t_section->get_trial_resistance() / new_length);
    }
    else {
        if(t_section->update_trial_status(t_trans->to_local_vec(get_trial_displacement()) / length) != SUANPAN_SUCCESS) return SUANPAN_FAIL;

        trial_stiffness = t_trans->to_global_stiffness_mat(t_section->get_trial_stiffness() / length);
    }

    trial_resistance = t_trans->to_global_vec(t_section->get_trial_resistance());

    return SUANPAN_SUCCESS;
}

int T3D2S::commit_status() {
    t_trans->commit_status();

    return t_section->commit_status();
}

int T3D2S::clear_status() {
    t_trans->clear_status();

    return t_section->clear_status();
}

int T3D2S::reset_status() {
    t_trans->reset_status();

    return t_section->reset_status();
}

std::vector<vec> T3D2S::record(const OutputType P) const { return t_section->record(P); }

void T3D2S::print() {
    suanpan_info("A 3D truss element with ");
    if(nlgeom)
        suanpan_info("corotational formulation, assuming constant area and {} strain.", log_strain ? "logarithmic" : "engineering");
    else
        suanpan_info("linear formulation.");
    suanpan_info(" The nodes connected are:", node_encoding);
    if(!is_initialized()) return;
    suanpan_info("Section:\n");
    t_section->print();
}

#ifdef SUANPAN_VTK
#include <vtkLine.h>

vtkSmartPointer<vtkCell> T3D2S::GetCell() const { return vtkSmartPointer<vtkLine>::New(); }

mat T3D2S::GetData(const OutputType P) {
    if(OutputType::A == P) return reshape(get_current_acceleration(), t_dof, t_node);
    if(OutputType::V == P) return reshape(get_current_velocity(), t_dof, t_node);
    if(OutputType::U == P) return reshape(get_current_displacement(), t_dof, t_node);

    vec data;
    if(const auto t_data = t_section->record(P); !t_data.empty()) data = t_data[0];
    return repmat(data.resize(6), 1, t_node);
}

mat T3D2S::GetDeformation(const double amplifier) { return get_coordinate(3).t() + amplifier * reshape(get_current_displacement(), t_dof, t_node); }

#endif
