ImperialCollegeLondon / ICLOCS

Imperial College London Optimal Control Software (ICLOCS)
MIT License
74 stars 44 forks source link

Issue with Adigator related to vectorization #5

Closed VishnuDevA7 closed 4 years ago

VishnuDevA7 commented 4 years ago

Hi, I am using ICLOCS version 2.5 with Adigator. The dynamics calculations for a n-DoF robot involve three dimensional mass matrix, the third dimensions being the length of the time vector, dim : n x n x t. When I tried to use Adigator with 3 dimensional matrices, it did not allow me to have arrays greater than two dimensions. Therefore, I changed my dynamics file and converted the 3 dimensional matrices to 2 dimensional matrices by stacking every time slice along the 2nd dimension. Now my mass matrix is as (n x n*t). However, I cannot avoid using for loops. Now when I call adigator again, I get the error "Conversion to double from cada is not possible" exactly at the point where a for loop is employed. Is there a fix for this issue ?

yuanbonie commented 4 years ago

I think using loops are not supported by Adigator but do double check whether you really need it. The dynamic equations should be ODEs and DAEs and they should be able to be expressed without use of loops.

For high dimesion data with Adigator, see Matt's replay (https://sourceforge.net/p/adigator/discussion/help/thread/8501decd/).

If there is no way around this, I suggest you to prototype with finite difference and later consider the use of analytical derivatives for speed and accuracy.

VishnuDevA7 commented 4 years ago

Thanks for the reply. However, I need the loops because there are matrices involved in dynamics calculations that varies with states of system and as the states change with time, these matrices also change and hence have to be propagated across time. My equations are all DAEs only, except that they involve matrix multiplications in the intermediate steps. Thereby, I end up with three-dimensional arrays. I already saw the link that you mentioned, unfortunately I cannot used nested cells or structures for my problems.

VishnuDevA7 commented 4 years ago

Is there a way to retain a double variable as double inside the dynamics function even when using Adigator? The dynamics function needs the size of an input variable that is passed on to it to compute the output. This variable is related to the number of nodes and cannot be known beforehand (for instance, during mesh refinement). When I try n = size(u,1), I get an error : The following error occurred converting from cada to double: Conversion to double from cada is not possible.

Is there another way to retain a variable just as a double instead of converting it to a cada variable ?

yuanbonie commented 4 years ago

Hi Vishnu,

I believe converting to cada class is required as that is the way how algorithm differentiation functions. However I have create a work around for you. If you use the latest version on Github, you can call n = vdat.N_tNode; inside a function Dynamics_Internal(x,u,p,t,vdat) for example to get this dimension.

Cheers, Yuanbo

VishnuDevA7 commented 4 years ago

Thanks for the update. However, my operations involve obtaining the matrix inverse and this solution does not work yet, Is there a workaround ?. To make it more clear, if x being the states is n x 1 dimensions, (n x m when vectorized), the output from the dynamics function is dx = inv(A)*B where A is n x (n x m) and B is n x m. Now since a blockwise inverse is required for every n x n elements of A, I can only use a for or a while loop for this matter. So when I try something like :

for i = 1:n
       %% ---dynamics computation using blockwise inverse of the matrix A----%% 
end

I get the error :

Conversion to double from cada is not possible. Error in Dynamics_Internal (line 46) for i = 1:n

yuanbonie commented 4 years ago

Hi Vishnu,

I think that means all variables are converted to cada class, including pre-defined data. In this way, I can not think of an easy workaround unfortuately.

Regards, Yuanbo