Page 1 of 1

Closing Connection to Log File via Matlab - OpenSim 3.2

Posted: Wed Apr 10, 2024 11:12 am
by novot
I am using Matlab 2015b to interface to OpenSim 3.2 and run a series of static optimizations. The script runs just fine and is working as expected; however, I would like to improve how I check whether an optimization was successful.

After an optimization is run, I load and check the `out.log` file for any warnings that occurred:

Code: Select all

% Check for Simulation Warning
outputContent = fileread('out.log');
% Determine if Warnings Appear
warnStrings = strfind(outputContent, 'WARN- The optimizer could not find a solution at time');
if(~isempty(warnStrings))
    currentLogLine = warnStrings(end);
end
I'd like input on two questions:
1) Is there a way to delete the log file after each run so that the file does not become massive? When I attempt to delete it at the end of each run, it states that Matlab is still using the file. I'm assuming that a connection to the out.log file is not being closed when the optimization ends.
2) Is there a different way to check if the optimization was successful without using the log file?

Thanks in advance.

Re: Closing Connection to Log File via Matlab - OpenSim 3.2

Posted: Wed Apr 10, 2024 2:39 pm
by novot
As an update - I have mostly figured out the first question.

After checking for the warning in the out.log file, even though the file is technically open and can't be deleted, I'm able to open the file using `fopen()`, clear the file, and then close the handle:

Code: Select all

% Check for Simulation Warning
outputContent = fileread('out.log');
% Determine if Warnings Appear
warnStrings = strfind(outputContent, 'WARN- The optimizer could not find a solution at time');

% If Warning is Present
if(~isempty(warnStrings))
    fprintf('Warning Detected - Reseting\n');
    fid = fopen('out.log','w'); % Clear the file upon opening
    fclose(fid);

    break;
end
This allows for a clean out.log file for each simulation going forward.

Re: Closing Connection to Log File via Matlab - OpenSim 3.2

Posted: Thu Apr 11, 2024 5:05 pm
by novot
It turns out that essentially carpet-pulling OpenSim and overwriting the out.log file results in thousands of NULL values the next time the file is read (duh! :lol:).

Due to this, upon completion of the first simulation, I make a copy of the log file for the next iteration. OpenSim then writes to the original out.log file, and I can compare the difference in the two files to look for any warnings that have occurred:

Code: Select all

% Check for Simulation Warning
if(set == 1)
    % No Prior Runs - Check Log File
    outputContent = fileread('out.log');
    copyfile out.log out_old.log;
else
    % Prior Runs - Compare New Log and Old Log
    [~, outputContent] = system('FC out.log out_old.log');
    copyfile out.log out_old.log;
end

% Determine if Warnings Appear
warnStrings = strfind(outputContent, 'WARN- The optimizer could not find a solution at time');
This avoids loading large out.log files as variables in Matlab and does not interrupt OpenSim's connection with the out.log file itself. I think that I am satisfied with this solution, and hopefully, it can aid others down the road :)