<commit_before>/* Siconos is a program dedicated to modeling, simulation and control
 * of non smooth dynamical systems.
 *
 * Copyright 2021 INRIA.
 *
 * 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.
*/

#include "OneStepIntegrator.hpp"
#include "Simulation.hpp"
#include "DynamicalSystem.hpp"
#include "NonSmoothDynamicalSystem.hpp"
#include "ExtraAdditionalTerms.hpp"
#include "Relation.hpp"
#include "EventsManager.hpp"
#include <SiconosConfig.h>
#include <functional>
using namespace std::placeholders;

SP::VectorOfVectors
OneStepIntegrator::_initializeDSWorkVectors(SP::DynamicalSystem ds)
{
  const DynamicalSystemsGraph::VDescriptor& dsv =
    _dynamicalSystemsGraph->descriptor(ds);

  // Create new work buffers, store in the graph
  SP::VectorOfVectors wv = std::make_shared<VectorOfVectors>();
  SP::VectorOfMatrices wm = std::make_shared<VectorOfMatrices>();
  _dynamicalSystemsGraph->properties(dsv).workVectors = wv;
  _dynamicalSystemsGraph->properties(dsv).workMatrices = wm;

  // Initialize memory buffers
  ds->initMemory(getSizeMem());

  // Force dynamical system to its initial state
  ds->resetToInitialState();

  return wv;
}


void OneStepIntegrator::initialize()
{
  if(_extraAdditionalTerms)
  {
    _extraAdditionalTerms->init(*_simulation->nonSmoothDynamicalSystem()->topology()->dSG(0),
                                *_simulation->nonSmoothDynamicalSystem(),
                                _simulation->eventsManager()->timeDiscretisation());
  }

  initialize_nonsmooth_problems();
  _isInitialized=true;

}

void OneStepIntegrator::update_interaction_output(Interaction& inter, double time, InteractionProperties& interaction_properties)
{
  // - compute interaction output (y) for all levels
  // - swaps in memory

  // This function:
  // - is called during osi->initialize()
  // - may be called by simulation when an interaction is inserted on the fly
  //    for instance:
  //      - contact detection
  //      - inter = ew interaction + link with ds
  //      - simu->osi->initializeWorkVectorsForInteraction(inter)
  //      - simu->osi->update_interaction_output()

  if(_steps > 1)  // Multi--step methods
  {
    // Compute the old Values of Output with stored values in Memory
    for(unsigned int k = 0; k < _steps - 1; k++)
    {
      /** ComputeOutput to fill the Memory
       * We assume the state x is stored in xMemory except for the  initial
       * condition which has not been swap yet.
       */
      //        relation()->LinkDataFromMemory(k);
      for(unsigned int i = 0; i < inter.upperLevelForOutput() + 1; ++i)
      {
        inter.computeOutput(time, i);
        //_yMemory[i]->swap(*_y[i]);
      }
    }
    inter.swapInMemory();
  }
  // // Compute a first value for the output
  // inter.computeOutput(time,  0);

  // // prepare the gradients
  // inter.relation()->computeJach(time, inter);
  // for(unsigned int i = 0; i < inter.upperLevelForOutput() + 1; ++i)
  // {
  //   inter.computeOutput(time, i);
  // }
  inter.swapInMemory();
}

void OneStepIntegrator::_check_and_update_interaction_levels(Interaction& inter)
{

  bool isInitializationNeeded = false;
  if(!(inter.lowerLevelForOutput() <= _levelMinForOutput && inter.upperLevelForOutput()  >= _levelMaxForOutput))
  {
    inter.setLowerLevelForOutput(_levelMinForOutput);
    inter.setUpperLevelForOutput(_levelMaxForOutput);
    isInitializationNeeded = true;
  }

  if(!(inter.lowerLevelForInput() <= _levelMinForInput && inter.upperLevelForInput() >= _levelMaxForInput))
  {
    inter.setLowerLevelForInput(_levelMinForInput);
    inter.setUpperLevelForInput(_levelMaxForInput);<commit_msg>[kernel] compute output when adding interaction<commit_after>/* Siconos is a program dedicated to modeling, simulation and control
 * of non smooth dynamical systems.
 *
 * Copyright 2021 INRIA.
 *
 * 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.
*/

#include "OneStepIntegrator.hpp"
#include "Simulation.hpp"
#include "DynamicalSystem.hpp"
#include "NonSmoothDynamicalSystem.hpp"
#include "ExtraAdditionalTerms.hpp"
#include "Relation.hpp"
#include "EventsManager.hpp"
#include <SiconosConfig.h>
#include <functional>
using namespace std::placeholders;

SP::VectorOfVectors
OneStepIntegrator::_initializeDSWorkVectors(SP::DynamicalSystem ds)
{
  const DynamicalSystemsGraph::VDescriptor& dsv =
    _dynamicalSystemsGraph->descriptor(ds);

  // Create new work buffers, store in the graph
  SP::VectorOfVectors wv = std::make_shared<VectorOfVectors>();
  SP::VectorOfMatrices wm = std::make_shared<VectorOfMatrices>();
  _dynamicalSystemsGraph->properties(dsv).workVectors = wv;
  _dynamicalSystemsGraph->properties(dsv).workMatrices = wm;

  // Initialize memory buffers
  ds->initMemory(getSizeMem());

  // Force dynamical system to its initial state
  ds->resetToInitialState();

  return wv;
}


void OneStepIntegrator::initialize()
{
  if(_extraAdditionalTerms)
  {
    _extraAdditionalTerms->init(*_simulation->nonSmoothDynamicalSystem()->topology()->dSG(0),
                                *_simulation->nonSmoothDynamicalSystem(),
                                _simulation->eventsManager()->timeDiscretisation());
  }

  initialize_nonsmooth_problems();
  _isInitialized=true;

}

void OneStepIntegrator::update_interaction_output(Interaction& inter, double time, InteractionProperties& interaction_properties)
{
  // - compute interaction output (y) for all levels
  // - swaps in memory

  // This function:
  // - is called during osi->initialize()
  // - may be called by simulation when an interaction is inserted on the fly
  //    for instance:
  //      - contact detection
  //      - inter = ew interaction + link with ds
  //      - simu->osi->initializeWorkVectorsForInteraction(inter)
  //      - simu->osi->update_interaction_output()

  if(_steps > 1)  // Multi--step methods
  {
    // Compute the old Values of Output with stored values in Memory
    for(unsigned int k = 0; k < _steps - 1; k++)
    {
      /** ComputeOutput to fill the Memory
       * We assume the state x is stored in xMemory except for the  initial
       * condition which has not been swap yet.
       */
      //        relation()->LinkDataFromMemory(k);
      for(unsigned int i = 0; i < inter.upperLevelForOutput() + 1; ++i)
      {
        inter.computeOutput(time, i);
        //_yMemory[i]->swap(*_y[i]);
      }
    }
    inter.swapInMemory();
  }
  // Compute a first value for the output
  inter.computeOutput(time,  0);

  // prepare the gradients
  inter.relation()->computeJach(time, inter);
  for(unsigned int i = 0; i < inter.upperLevelForOutput() + 1; ++i)
  {
    inter.computeOutput(time, i);
  }
  inter.swapInMemory();
}

void OneStepIntegrator::_check_and_update_interaction_levels(Interaction& inter)
{

  bool isInitializationNeeded = false;
  if(!(inter.lowerLevelForOutput() <= _levelMinForOutput && inter.upperLevelForOutput()  >= _levelMaxForOutput))
  {
    inter.setLowerLevelForOutput(_levelMinForOutput);
    inter.setUpperLevelForOutput(_levelMaxForOutput);
    isInitializationNeeded = true;
  }

  if(!(inter.lowerLevelForInput() <= _levelMinForInput && inter.upperLevelForInput() >= _levelMaxForInput))
  {
    inter.setLowerLevelForInput(_levelMinForInput);
    inter.setUpperLevelForInput(_levelMaxForInput);
{'fraction_non_alphanumeric': 0.10982379917933864, 'fraction_numerical': 0.0041033067825247405, 'mean_word_length': 3.7462772050400917, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6808567', 'n_tokens_mistral': 2505, 'n_tokens_neox': 2423, 'n_words': 642}