<commit_before>/* -------------------------------------------------------------------------- *
 *                      OpenSim:  testAssemblySolver.cpp                      *
 * -------------------------------------------------------------------------- *
 * The OpenSim API is a toolkit for musculoskeletal modeling and simulation.  *
 * See http://opensim.stanford.edu and the NOTICE file for more information.  *
 * OpenSim is developed at Stanford University and supported by the US        *
 * National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA    *
 * through the Warrior Web program.                                           *
 *                                                                            *
 * Copyright (c) 2005-2017 Stanford University and the Authors                *
 * Author(s): <NAME>                                                       *
 *                                                                            *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License. You may obtain a  *
 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
 *                                                                            *
 * Unless required by applicable law or agreed to in writing, software        *
 * distributed under the License is distributed on an "AS IS" BASIS,          *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 * -------------------------------------------------------------------------- */

//=============================================================================
// testAssemblySolver loads models with constraints to verify that constraints
// are adequately satisfied or that an appropriate exception is thrown.
//
//=============================================================================
#include <OpenSim/Simulation/osimSimulation.h>
#include <OpenSim/Auxiliary/auxiliaryTestFunctions.h>
#include <OpenSim/Common/LoadOpenSimLibrary.h>

using namespace OpenSim;
using namespace std;

// Measure how long it takes to perform model.setStateVariableValues() on a 
// model with constraints to evaluate the effect of assembly in the process.
void instrumentSetStateValues(const string& modelFile);
void testAssembleModelWithConstraints(string modelFile);
void testAssemblySatisfiesConstraints(string modelFile);
double calcLigamentLengthError(const SimTK::State &s, const Model &model);

int main()
{
    try {
        LoadOpenSimLibrary("osimActuators");

        //~3.5s for CoordinateStateVariable::setValue() enforcing constraints
        //~0.18s for CoordinateStateVariable::setValue() NOT enforcing constraints
        //       plus explicit Model::assemble() after model.setStateVariableValues()
        instrumentSetStateValues("PushUpToesOnGroundLessPreciseConstraints.osim");
        testAssemblySatisfiesConstraints("knee_patella_ligament.osim");
        testAssembleModelWithConstraints("PushUpToesOnGroundExactConstraints.osim");
        testAssembleModelWithConstraints("PushUpToesOnGroundLessPreciseConstraints.osim");
        testAssembleModelWithConstraints("PushUpToesOnGroundWithMuscles.osim");
    }
    catch (const std::exception& e) {
        cout << "\ntestAssemblySolver FAILED " << e.what() <<endl;
        return 1;
    }
    cout << "\ntestAssemblySolver PASSED" << endl;
    return 0;
}

//==========================================================================================================
// Test Cases
//==========================================================================================================
void instrumentSetStateValues(const string& modelFile)
{
    // Setup OpenSim model
    Model model(modelFile);
    SimTK::State &s = model.initSystem();

    auto names = model.getStateVariableNames();
    SimTK::Vector stateValues = model.getStateVariableValues(s);

    int numLoop = 1000;

    std::clock_t testStartTime = std::clock();

    for (int i = 0; i < numLoop; ++i) {
        model.setStateVariableValues(s, stateValues);
        // Direct set values for coordinates does not ensure they 
        // satisfy kinematic constraints. Explicitly enforce constraints
        // by performing and assembly, now.
        model.assemble(s);
    }

    std::clock_t testEndTime = std::clock();
    double elapsed = testEndTime - testStartTime;
    cout << "model.setStateVariableValues elapsed time = "
        << elapsed / CLOCKS_PER_SEC << "s" << endl;
}


void testAssembleModelWithConstraints(string modelFile)
{
    double accuracy = 1e-5;
    using namespace SimTK;

    cout << "\n****************************************************************************" << endl;
    cout << " testAssembleModelWithConstraints with "<< modelFile << endl;
    cout << "****************************************************************************\n" << endl;

    //==========================================================================================================<commit_msg>Clarify comments [ci skip]<commit_after>/* -------------------------------------------------------------------------- *
 *                      OpenSim:  testAssemblySolver.cpp                      *
 * -------------------------------------------------------------------------- *
 * The OpenSim API is a toolkit for musculoskeletal modeling and simulation.  *
 * See http://opensim.stanford.edu and the NOTICE file for more information.  *
 * OpenSim is developed at Stanford University and supported by the US        *
 * National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA    *
 * through the Warrior Web program.                                           *
 *                                                                            *
 * Copyright (c) 2005-2017 Stanford University and the Authors                *
 * Author(s): <NAME>                                                       *
 *                                                                            *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License. You may obtain a  *
 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
 *                                                                            *
 * Unless required by applicable law or agreed to in writing, software        *
 * distributed under the License is distributed on an "AS IS" BASIS,          *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 * -------------------------------------------------------------------------- */

//=============================================================================
// testAssemblySolver loads models with constraints to verify that constraints
// are adequately satisfied or that an appropriate exception is thrown.
//
//=============================================================================
#include <OpenSim/Simulation/osimSimulation.h>
#include <OpenSim/Auxiliary/auxiliaryTestFunctions.h>
#include <OpenSim/Common/LoadOpenSimLibrary.h>

using namespace OpenSim;
using namespace std;

// Measure how long it takes to perform model.setStateVariableValues() on a 
// model with constraints to evaluate the effect of assembly in the process.
void instrumentSetStateValues(const string& modelFile);
void testAssembleModelWithConstraints(string modelFile);
void testAssemblySatisfiesConstraints(string modelFile);
double calcLigamentLengthError(const SimTK::State &s, const Model &model);

int main()
{
    try {
        LoadOpenSimLibrary("osimActuators");

        //~3.5s for CoordinateStateVariable::setValue() enforcing constraints
        //~0.18s for CoordinateStateVariable::setValue() NOT enforcing constraints
        //       plus explicit Model::assemble() after model.setStateVariableValues()
        instrumentSetStateValues("PushUpToesOnGroundLessPreciseConstraints.osim");
        testAssemblySatisfiesConstraints("knee_patella_ligament.osim");
        testAssembleModelWithConstraints("PushUpToesOnGroundExactConstraints.osim");
        testAssembleModelWithConstraints("PushUpToesOnGroundLessPreciseConstraints.osim");
        testAssembleModelWithConstraints("PushUpToesOnGroundWithMuscles.osim");
    }
    catch (const std::exception& e) {
        cout << "\ntestAssemblySolver FAILED " << e.what() <<endl;
        return 1;
    }
    cout << "\ntestAssemblySolver PASSED" << endl;
    return 0;
}

//==========================================================================================================
// Test Cases
//==========================================================================================================
void instrumentSetStateValues(const string& modelFile)
{
    // Setup OpenSim model
    Model model(modelFile);
    SimTK::State &s = model.initSystem();

    auto names = model.getStateVariableNames();
    SimTK::Vector stateValues = model.getStateVariableValues(s);

    int numLoop = 1000;

    std::clock_t testStartTime = std::clock();

    for (int i = 0; i < numLoop; ++i) {
        model.setStateVariableValues(s, stateValues);
        // Directly setting values for coordinates does not ensure they 
        // satisfy kinematic constraints. Explicitly enforce constraints
        // by performing an assembly, now.
        model.assemble(s);
    }

    std::clock_t testEndTime = std::clock();
    double elapsed = testEndTime - testStartTime;
    cout << "model.setStateVariableValues elapsed time = "
        << elapsed / CLOCKS_PER_SEC << "s" << endl;
}


void testAssembleModelWithConstraints(string modelFile)
{
    double accuracy = 1e-5;
    using namespace SimTK;

    cout << "\n****************************************************************************" << endl;
    cout << " testAssembleModelWithConstraints with "<< modelFile << endl;
    cout << "****************************************************************************\n" << endl;

    //==========================================================================================================
{'fraction_non_alphanumeric': 0.23486729043345428, 'fraction_numerical': 0.008019858697727707, 'mean_word_length': 2.4639550264550265, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '666303', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2199, 'n_words': 685}