Parallel Simulation Saving Results Periodically

Hi PLECS support team,

I am running parallel simulations using script, but, would need to make sure the results associated with those simulation events that have finished successfully will be saved. How this would be possible?

I have the script based parallel simulations and once all the simulation events in the parallel simulation end successfully, the results will be saved. Since the simulations could take days to finish, I’d like to make sure the results of those events that finished successfully will be saved in case if any event fails or there is an error.

Thanks,
Malek

Hi Malek,

To ensure that results from successful parallel simulation events are saved even if others fail, you can implement a callback function in your PLECS scripting setup. This function is invoked after each individual simulation completes and can be used for post-processing or saving results independently.

A useful reference is the Buck Converter with Parameter Sweep demo model, which illustrates this approach using both internal scripting (Octave) and external scripting via XML/JSON-RPC (Python in the example). Below is a portion of the Octave callback function for clarity:

% Evaluate simulation results in callback function
function result = callback(index, data)
   % hold and label trace
	name = ['L = ', mat2str(inductorValues(index)), 'μH'];
	plecs('scope', './Scope', 'HoldTrace', name);
	
	% Find maximum current values and index
	if isstruct(data)
		[maxi, maxidx] = max(data.Values(1,:));
		maxt = data.Time(maxidx);
		% Reducing simulation results by return value 'result' 
 		result = [maxi, maxt];
 	else
 		% Print error message to Octave console
 		printf(' Error in Simulation %d for L=%duH: %s\n',
                  index, inductorValues(index), data);
	end
end

Please note that in this example, traces are stored using a Scope. While this is effective for demonstrating scripting capabilities, it is not the most efficient method—particularly in parallel simulations—due to the large volume of data that must be transferred between cores. In practice, parallel simulations usually target key metrics, such as peak current values (as in this example), or aggregate measures like efficiency or total losses. Limiting the exchanged data enhances execution speed significantly.

1 Like

Hi Christen,

Thank you for replying to my inquiry.

I am using the call aback function the same way it is implemented in the Buck example. But, I have a command “save -mat7-binary fileName.mat simData” outside the call back function that saves the data as .mat file and then I will be using MATLAB for post processing.

Please note, The simData is defined as a cell array data structure inside the callback function that saves the data of interest under different cells in the array. once the data are saved, the “save -mat7-binary fileName.mat simData” is used at the end to save the entire data as .mat file.

This will only save the results once the entire batch of parallel simulations are done successfully.

UPDATE: Bu moving the “save -mat7-binary fileName.mat simData” command inside the callback function, the data are now being saved after each simulation event is finished.

Thanks,
Malek

Hi Malek,

Thank you for your reply, and especially for the update.
Just make sure to use a unique filename for each run to avoid overwriting the results.

Reto