Page 1 of 1
Question about the source code of MocoSumSquaredStateGoal
Posted: Mon Nov 14, 2022 12:32 pm
by luckyme
Dear Moco development team and Moco users,
Thank you all for your contributions to the biomechanics community!I want to customize a new cost function item about "State" in Moco, so firstly I read "MocoSumSquaredStateGoal.cpp" to learn how to write relevant codes.It can be found from pic1 that
the variable 'integrand' is not initialized to 0 before the for loop starts in the function 'calcIntegrandImpl'.The difference is that the variable 'integrand' is initialized to 0 in MocoControlGoal.cpp (pic2).I also looked at some tracking goals (in these goals relevant value also need to be summed in the for loop), and found that the variable 'integrand' is also assigned to 0 before the for loop starts. So for MocoSumSquaredStateGoal, is it necessary to set the variable 'integrand' to 0 before the for loop starts? Or is the variable 'integrand' has been already set to 0 elsewhere, so it doesn't need to be assigned to 0 here?
https://github.com/opensim-org/opensim- ... teGoal.cpp
![pic1.png](./download/file.php?id=6327&sid=0bca831213933cc81b950c09f1dec1c2)
- pic1.png (35.33 KiB) Viewed 868 times
https://github.com/opensim-org/opensim- ... olGoal.cpp
![pic2.png](./download/file.php?id=6328&sid=0bca831213933cc81b950c09f1dec1c2)
- pic2.png (47.78 KiB) Viewed 868 times
I would be very grateful if someone could clarify the confusion for me.
Best Wishes,
Matthew
Re: Question about the source code of MocoSumSquaredStateGoal
Posted: Mon Nov 21, 2022 12:01 pm
by nbianco
Hi Matthew,
If you take a look at
MocoGoal::calcIntegrand, you can see that the integrand value passed to calcIntegrandImpl in derived goals is initialized to zero.
So, it's not really necessary to initialize to zero again inside calcIntegrandImpl. But it doesn't change anything either, we probably did that in MocoControlGoal out of habit.
-Nick
Re: Question about the source code of MocoSumSquaredStateGoal
Posted: Tue Nov 22, 2022 11:47 am
by luckyme
nbianco wrote: ↑Mon Nov 21, 2022 12:01 pm
If you take a look at
MocoGoal::calcIntegrand, you can see that the integrand value passed to calcIntegrandImpl in derived goals is initialized to zero.
Hi Nick,
Thank you very much for your professional answer! I'm glad to know that the integrand is initialized to 0 in MocoGoal::calcIntegrand. This means whether it can be understood like this: when writing a goal derived from MocoGoal, whether to initialize the integrand to 0 in the function calcIntegrandImpl will not affect the final calculation result?
I recently successfully ran the exampleMocoCustomEffortGoal provided by Moco using
visual studio 2017 C++ and
CMake3.15.5, and then customized a new goal named
MocoSumCubedStateGoal based on MocoSumSquaredStateGoal.cpp. This goal changes the sum of squares of states to
the cubic sum of states by modifying just one line of code in MocoSumSquaredStateGoal.cpp. When buliding my new goal in visual studio 2017 C++, the compiler reports
the following errors:
![pic3.png](./download/file.php?id=6353&sid=0bca831213933cc81b950c09f1dec1c2)
- pic3.png (59.45 KiB) Viewed 687 times
Recently I've double-checked my codes and tried various methods of debugging, but still can't resolve these errors. These errors has bothered me for a few days and made me very distressed.
![Crying or Very Sad :cry:](./images/smilies/icon_cry.gif)
You are the authoritative expert of Moco,could you please help me check where is the problem?My original codes are attached. Thanks a lot for your time and help!
Best Wishes,
Matthew
Re: Question about the source code of MocoSumSquaredStateGoal
Posted: Wed Nov 23, 2022 11:44 am
by nbianco
This means whether it can be understood like this: when writing a goal derived from MocoGoal, whether to initialize the integrand to 0 in the function calcIntegrandImpl will not affect the final calculation result?
Yes, that's correct.
As for the compiler errors, looks like there's some linking issues with the spdlog library. Did you build spdlog along with the rest of the dependencies?
Also, I would recommend upgrading to Visual Studio 2019, as that's the version we use for OpenSim and Moco development.
-Nick
Re: Question about the source code of MocoSumSquaredStateGoal
Posted: Fri Nov 25, 2022 8:47 am
by luckyme
nbianco wrote: ↑Wed Nov 23, 2022 11:44 am
As for the compiler errors, looks like there's some linking issues with the spdlog library. Did you build spdlog along with the rest of the dependencies?
Hi Nick,
Thank you very much for your reply and help! I am a beginner in C++ and CMake, please forgive my seemingly simple question.How should I build spdlog along with the rest of the dependencies? What codes do I need to add in CMakeLists.txt or in .h and .cpp files? The following are codes in my CMakeLists.txt file,I am looking forward to your valuable suggestions,thank you very much.
Code: Select all
cmake_minimum_required(VERSION 3.2)
project(OpenSim_MocoSumCubedStateGoal)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenSim REQUIRED HINTS
"${CMAKE_SOURCE_DIR}/../../../../../../")
include("${OpenSim_USE_FILE}")
add_library(osimMocoSumCubedStateGoal SHARED
MocoSumCubedStateGoal.h
MocoSumCubedStateGoal.cpp
osimMocoSumCubedStateGoalDLL.h
RegisterTypes_osimMocoSumCubedStateGoal.h
RegisterTypes_osimMocoSumCubedStateGoal.cpp
)
target_link_libraries(osimMocoSumCubedStateGoal
osimTools osimExampleComponents osimMoco)
string(TOUPPER MocoSumCubedStateGoal example_name_upper)
set_target_properties(osimMocoSumCubedStateGoal PROPERTIES
DEFINE_SYMBOL OSIM${example_name_upper}_EXPORTS
)
add_executable(exampleMocoSumCubedStateGoal exampleMocoSumCubedStateGoal.cpp)
target_link_libraries(exampleMocoSumCubedStateGoal osimMocoSumCubedStateGoal)
# For Windows: make sure DLLs for dependencies are available.
OpenSimCopyDependencyDLLsForWin(DEP_NAME OpenSim
DEP_BIN_DIR "${OpenSim_BIN_DIR}")
if(WIN32)
add_dependencies(osimMocoSumCubedStateGoal Copy_OpenSim_DLLs)
endif()
file(COPY DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
Best Wishes,
Matthew
Re: Question about the source code of MocoSumSquaredStateGoal
Posted: Mon Nov 28, 2022 4:56 pm
by nbianco
Hi Matthew,
I would refer to the CMake files from the examples for creating plug-ins that are included with OpenSim:
https://github.com/opensim-org/opensim- ... eLists.txt.
When running CMake, you should just be able to provide a directory to an existing OpenSim installation (i.e., OPENSIM_INSTALL_DIR) and then create a standalone build of your plug-in.
-Nick