<commit_before>#include "AssemblyVisitor.h"


#include <sofa/component/linearsolver/EigenVectorWrapper.h>
#include <sofa/component/linearsolver/SingleMatrixAccessor.h>

#include "./utils/scoped.h"
#include "./utils/cast.h"
#include "./utils/sparse.h"


namespace sofa {
namespace simulation {

using namespace component::linearsolver;
using namespace core::behavior;

typedef EigenVectorWrapper<SReal> wrap;









AssemblyVisitor::AssemblyVisitor(const core::MechanicalParams* mparams, const core::MechanicalParams* mparamsWithoutStiffness, MultiVecDerivId velId, MultiVecDerivId lagrangeId)
	: base( mparams ),
      mparams( mparams ),
      mparamsWithoutStiffness( mparamsWithoutStiffness ),
      _velId(velId),
      lagrange(lagrangeId),
      start_node(0),
      _processed(0)
{
}


AssemblyVisitor::~AssemblyVisitor()
{
    if( _processed ) delete _processed;
}


AssemblyVisitor::chunk::chunk()
	: offset(0),
	  size(0),
	  damping(0),
	  mechanical(false),
	  vertex(-1),
	  dofs(0) {

}

// this is not thread safe lol
void AssemblyVisitor::vector(dofs_type* dofs, core::VecId id, const vec::ConstSegmentReturnType& data) {
    assert( dofs->getMatrixSize() == data.size() );

	// realloc
	if( data.size() > tmp.size() ) tmp.resize(data.size() );
	tmp.head(data.size()) = data;

	unsigned off = 0;
	wrap w(tmp);
	dofs->copyFromBaseVector(id, &w, off);
	assert( off == data.size() );
}


AssemblyVisitor::vec AssemblyVisitor::vector(dofs_type* dofs, core::ConstVecId id) {
	unsigned size = dofs->getMatrixSize();

	const bool fast = false;

	if( fast ) {
		// map data
		const void* data = dofs->baseRead(id)->getValueVoidPtr();

		// TODO need to know if we're dealing with double or floats
		return Eigen::Map<const vec>( reinterpret_cast<const double*>(data), size);
	} else {

		// realloc
		if( size > tmp.size() ) tmp.resize(size);

		unsigned off = 0;
		wrap w(tmp);

		dofs->copyToBaseVector(&w , id, off);

		return tmp.head(size);
	}
}


// pretty prints a mapping
static inline std::string mapping_name(simulation::Node* node) {
	return node->mechanicalMapping->getName() + " (class: " + node->mechanicalMapping->getClassName() + ") ";
}

// mapping informations as a map (parent dofs -> (J, K) matrices )
AssemblyVisitor::chunk::map_type AssemblyVisitor::mapping(simulation::Node* node) {
	chunk::map_type res;

	if( !node->mechanicalMapping ) return res;

	using helper::vector;

	const vector<sofa::defaulttype::BaseMatrix*>* js = node->mechanicalMapping->getJs();
	const vector<sofa::defaulttype::BaseMatrix*>* ks = node->mechanicalMapping->getKs();

	assert( node->mechanicalMapping->getTo().size() == 1 &&
	        "only n -> 1 mappings are handled");

	vector<core::BaseState*> from = node->mechanicalMapping->getFrom();

//	dofs_type* to = safe_cast<dofs_type>(node->mechanicalMapping->getTo()[0]);
//	unsigned rows = to->getMatrixSize();

	for( unsigned i = 0, n = from.size(); i < n; ++i ) {

		// parent dofs<commit_msg>r10042/sofa : Compliant: assembly now uses BaseMechanicalState copy[To/From]Buffer<commit_after>#include "AssemblyVisitor.h"


#include <sofa/component/linearsolver/EigenVectorWrapper.h>
#include <sofa/component/linearsolver/SingleMatrixAccessor.h>

#include "./utils/scoped.h"
#include "./utils/cast.h"
#include "./utils/sparse.h"


namespace sofa {
namespace simulation {

using namespace component::linearsolver;
using namespace core::behavior;

typedef EigenVectorWrapper<SReal> wrap;









AssemblyVisitor::AssemblyVisitor(const core::MechanicalParams* mparams, const core::MechanicalParams* mparamsWithoutStiffness, MultiVecDerivId velId, MultiVecDerivId lagrangeId)
	: base( mparams ),
      mparams( mparams ),
      mparamsWithoutStiffness( mparamsWithoutStiffness ),
      _velId(velId),
      lagrange(lagrangeId),
      start_node(0),
      _processed(0)
{
}


AssemblyVisitor::~AssemblyVisitor()
{
    if( _processed ) delete _processed;
}


AssemblyVisitor::chunk::chunk()
	: offset(0),
	  size(0),
	  damping(0),
	  mechanical(false),
	  vertex(-1),
	  dofs(0) {

}

// this is not thread safe lol
void AssemblyVisitor::vector(dofs_type* dofs, core::VecId id, const vec::ConstSegmentReturnType& data) {
	assert( id.type == sofa::core::V_DERIV );

	unsigned size = dofs->getMatrixSize();

	assert( size == data.size() );
	
    // realloc if needed
	if( data.size() > tmp.size() ) tmp.resize(data.size() );
	tmp.head(data.size()) = data;

	unsigned off = 0;
	
	dofs->copyFromBuffer(id, tmp.data(), size);
}


AssemblyVisitor::vec AssemblyVisitor::vector(dofs_type* dofs, core::ConstVecId id) {
	assert( id.type == sofa::core::V_DERIV );

	unsigned size = dofs->getMatrixSize();
	
	// realloc if needed
	if( size > tmp.size() ) tmp.resize(size);
	
	unsigned off = 0;
	dofs->copyToBuffer( tmp.data(), id, size );
	
	return tmp.head(size);
}


// pretty prints a mapping
static inline std::string mapping_name(simulation::Node* node) {
	return node->mechanicalMapping->getName() + " (class: " + node->mechanicalMapping->getClassName() + ") ";
}

// mapping informations as a map (parent dofs -> (J, K) matrices )
AssemblyVisitor::chunk::map_type AssemblyVisitor::mapping(simulation::Node* node) {
	chunk::map_type res;

	if( !node->mechanicalMapping ) return res;

	using helper::vector;

	const vector<sofa::defaulttype::BaseMatrix*>* js = node->mechanicalMapping->getJs();
	const vector<sofa::defaulttype::BaseMatrix*>* ks = node->mechanicalMapping->getKs();

	assert( node->mechanicalMapping->getTo().size() == 1 &&
	        "only n -> 1 mappings are handled");

	vector<core::BaseState*> from = node->mechanicalMapping->getFrom();

//	dofs_type* to = safe_cast<dofs_type>(node->mechanicalMapping->getTo()[0]);
//	unsigned rows = to->

	for( unsigned i = 0, n = from.size(); i < n; ++i ) {

		// parent dofs
{'fraction_non_alphanumeric': 0.15467128027681662, 'fraction_numerical': 0.005363321799307958, 'mean_word_length': 4.684365781710914, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6100001', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1864, 'n_words': 414}