Home › Forum › SOFA › Programming with SOFA › [SOLVED] Access constraint forces from a different class
Tagged: Constraint Forces, Constraint Solver, SOFA_2106, Windows_10
- This topic has 8 replies, 2 voices, and was last updated 2 years, 10 months ago by Hugo.
-
AuthorPosts
-
6 December 2021 at 05:19 #21057TimesBlocked
Hi guys,
I would like to get the constraint forces from the GenericConstraintSolver and pass them to my class but I have not been able to do so as all I get are zero/null values.
The address of the constraint forces can be retrieved within the GenericConstraintSolver class by
current_cp->GetF();
How can I go about doing this outside the GenericConstraintSolver? Any help/ideas would be appreciated. Thanks.
6 December 2021 at 16:31 #21064HugoKeymasterHey @akthetimes
From what I see you are attempting to do this in C++. Correct?
The GenericConstraintSolver has two dedicated options to compute the constraint forces:
–computeConstraintForces
: boolean enablin the storage of the constraintForces
–constraintForces
: vector containing the constraint forcesThe type of
constraintForces
istype::vector< double >
, make sure you are accessing it properly (WriteAccessor) and that you use the proper data type.Since both are SOFA data, you can access them from outside the GenericConstraintSover.
Do you see my point?
Do you have a code snippet on your end that we could edit?Best
Hugo
7 December 2021 at 06:57 #21070TimesBlockedHey @Hugo,
Thanks for the reply! For example if we’d like to print out the values of the constraint forces from another class.sofa::component::constraintset::GenericConstraintSolver* solver; Data<type::vector< double >> constraintForces = solver->d_constraintForces.getValue(); if (condition) msg_info("Constraint Forces") << constraintForces;
How could I go about this?
7 December 2021 at 21:52 #21079HugoKeymasterOk then you could write something like:
sofa::component::constraintset::GenericConstraintSolver* solver; type::vector< double > constraintForces = solver->d_constraintForces.getValue(); if (condition) msg_info("Constraint Forces") << constraintForces;
Keep us posted if this helps.
How do you get the pointer to the GenericConstraintSolver?Best,
Hugo
8 December 2021 at 06:26 #21085TimesBlocked@Hugo, that perhaps is my question. How do I get the pointer to the GenericConstraintSolver?
10 December 2021 at 22:12 #21121HugoKeymasterHey @akthetimes
This depends on where/what is your code supposed to be implemented?
What is this C++ code you want to write?Best
Hugo
11 December 2021 at 10:30 #21122TimesBlockedHey @Hugo,
I would like to impose different constraints depending on the contact force. So for example, in the SOFA class FrictionContact(FrictionContact -SOFA API), in the
createResponse
function, I’d like to reference the ConstraintSolver so I can impose a contact depending on the force. I appreciate any help you can offer on this. Thanks14 December 2021 at 16:41 #21138TimesBlockedI was able to solve it like this:
core::objectmodel::BaseContext* context = this->getContext(); context->get(constraintSolver);
and finally the constraint forces can be printed out:
msg_info("constraint forces") << constraintSolver->getConstraintProblem()->f;
9 January 2022 at 18:40 #21334HugoKeymasterGreat @akthetimes
All my apologies for the delay!
Thanks for closing the topic, do not hesitate to keep us updated!Best
Hugo
-
AuthorPosts
- You must be logged in to reply to this topic.