├── README.md ├── calcRectangleDims.m ├── main.m └── runParOptimization.m /README.md: -------------------------------------------------------------------------------- 1 | # COMSOL-MATLAB-livelink 2 | This repository contains MATLAB .m files that automate topology optimization in COMSOL using the LiveLink interface. These files are associated with the research presented in this [paper](https://dx.doi.org/10.1088/2632-959X/acef44) and in this [article](https://chaozhuang22.github.io/fea/to/). 3 | 4 | ## Why Is This Workflow Necessary? 5 | In topology optimization, the slots usually reserved for parametric and auxiliary sweeps are already occupied by beta continuation and eta robust formulation. Therefore, an external control for parameter sweeping is required. This is particularly useful for handling cases with varying piezoresistor aspect ratios while maintaining a fixed area. The dimensions (length and width) of the piezoresistor are calculated using a separate [function](calcRectangleDims.m). 6 | 7 | ## Workflow Overview 8 | The [main](main.m) script performs the following steps: 9 | 1. Generates a list of parameters based on a specified area and a provided list of aspect ratios. 10 | 2. Feeds these parameters into the COMSOL optimization function. 11 | 3. Produces optimized designs that meet the specified geometric constraints, using the function [runParOptimization](runParOptimization.m). 12 | Note: The optimization function is embedded within a parfor loop, which requires MATLAB's Parallel Computing Toolbox. 13 | 14 | ## Usage 15 | To use these scripts, place them in the same folder as your COMSOL .mph files. After specifying the path name variables, run the main.m script. 16 | 17 | ## Troubleshooting 18 | The connection between COMSOL and MATLAB can be vulnerable, often resulting in ambiguous errors that are difficult to interpret. If an error occurs: 19 | 1. Verify if the parameters you are sweeping will cause errors in the original COMSOL .mph or not. 20 | 2. If the .mph model is fine, proceed to debug the MATLAB script. 21 | 22 | For example, a common error looks like the following: 23 | ``` 24 | Error using parallel.internal.pool.deserialize (line 45) 25 | Unable to read data stream because the data contains a bad version or endian-key 26 | 27 | Error in distcomp.remoteparfor/deserialize (line 284) 28 | data = parallel.internal.pool.deserialize(... 29 | 30 | Error in distcomp.remoteparfor>@(data)deserialize(obj,data) (line 242) 31 | [err, workerAborted] = iIntervalErrorDispatch(r, @(data) deserialize(obj, data)); 32 | 33 | Error in distcomp.remoteparfor>iIntervalErrorDispatch (line 547) 34 | origErr = deserFcn(intervalError); 35 | 36 | Error in distcomp.remoteparfor/handleIntervalErrorResult (line 242) 37 | [err, workerAborted] = iIntervalErrorDispatch(r, @(data) deserialize(obj, data)); 38 | 39 | Error in distcomp.remoteparfor/getCompleteIntervals (line 395) 40 | [r, err] = obj.handleIntervalErrorResult(r); 41 | 42 | Error in scriptForAspectRatioAndPositionSweep (line 25) 43 | parfor i = 1:length(jobSequence) 44 | ``` 45 | Solution: 46 | 1. Check if variable names in loadAndSetupModel() match with the variables defined in the .mph file. 47 | 2. Check if the study nodes (e.g. std1) specified in runAndVerifyModel() match with the study node tag in the .mph file. 48 | 3. Make sure the generic COMSOL can build that specified geometry. 49 | -------------------------------------------------------------------------------- /calcRectangleDims.m: -------------------------------------------------------------------------------- 1 | function geometrySpecs = calcRectangleDims(edgeLength, aspectRatio, position) 2 | area = edgeLength^2; % Calculate the area of the rectangle 3 | 4 | % Preallocate memory for the output cell array for efficiency 5 | geometrySpecs = cell(numel(aspectRatio)*numel(position), 3); 6 | counter = 1; % Index counter for filling the output cell array 7 | 8 | % Iterate over each aspect ratio 9 | for i = 1:numel(aspectRatio) 10 | % Calculate the length and width for the current aspect ratio 11 | length = sqrt(area*aspectRatio(i)); 12 | width = area/length; 13 | 14 | % Iterate over each position 15 | for j = 1:numel(position) 16 | % Save the calculated length, width, and current position to the output matrix 17 | % Convert the numeric values to strings and append the units 18 | geometrySpecs(counter, :) = {strcat(num2str(length), ' [um]'), strcat(num2str(width), ' [um]'), num2str(position(j))}; 19 | counter = counter + 1; 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /main.m: -------------------------------------------------------------------------------- 1 | % Define paths and parameters 2 | comsolPath = "E:\COMSOL\COMSOL61\Multiphysics\bin\win64"; 3 | storage = "E:\comsolStorage\"; 4 | model = "TO-mss"; 5 | edgeLength = 40; 6 | aspectRatio = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,2,3,4,5,6,7,8,9]; 7 | position = 0; 8 | 9 | % Start COMSOL server 10 | startComsolServer(comsolPath); 11 | 12 | % Prepare job sequence 13 | jobSequence = calcRectangleDims(edgeLength, aspectRatio, position); 14 | 15 | % Initialize output file 16 | initDataFile(storage + "TO_data.csv"); 17 | 18 | % Measure the execution time 19 | tStart = tic; 20 | 21 | % Start parallel pool 22 | pool = startParallelPool(); 23 | 24 | % Run optimization for each job 25 | parfor i = 1:length(jobSequence) 26 | runParOptimization(model, jobSequence, storage, i); 27 | end 28 | 29 | % Print total execution time and clean up 30 | cleanupAfterRun(tStart, pool); 31 | 32 | function initDataFile(fileName) 33 | writematrix([], fileName); 34 | end 35 | 36 | function startComsolServer(comsolPath) 37 | currentDir = pwd; 38 | cd(comsolPath); 39 | system('comsolmphserver.exe &'); 40 | pause(10); 41 | system('comsolmphserver.exe &'); 42 | cd(currentDir); 43 | end 44 | 45 | function pool = startParallelPool() 46 | if isempty(gcp('nocreate')) 47 | pool = parpool(2); 48 | else 49 | pool = gcp; 50 | end 51 | end 52 | 53 | function cleanupAfterRun(tStart, pool) 54 | tEnd = toc(tStart); 55 | fprintf("All Done, tea time!\nThis run takes %d h %d min.\n", floor(tEnd/3600), floor(rem(tEnd,3600)/60)); 56 | delete(pool); 57 | end 58 | -------------------------------------------------------------------------------- /runParOptimization.m: -------------------------------------------------------------------------------- 1 | function runParOptimization(model, jobSequence, storage, jobIndex) 2 | 3 | numOfJobs = length(jobSequence); 4 | fprintf("Optimizing %d/%d ...\n", jobIndex, numOfJobs); 5 | 6 | % Extract geometry dimensions for the current job 7 | [l, w, h] = jobSequence{jobIndex,:}; 8 | 9 | % Initialize COMSOL server 10 | initializeComsolServer(); 11 | 12 | % Set model parameters and run optimization 13 | model1 = loadAndSetupModel(model, string(l), string(w), string(h)); 14 | 15 | % Run and verify the model 16 | runAndVerifyModel(model1, jobIndex, numOfJobs); 17 | 18 | % Export results and save the model 19 | exportResultsAndSaveModel(model1, storage, num2str(jobIndex), string(l), string(w), string(h)); 20 | end 21 | 22 | function initializeComsolServer() 23 | comsolPort = [2036 2037]; 24 | task = getCurrentTask(); 25 | labIndex = task.ID; 26 | currentDir = pwd; 27 | cd("E:\COMSOL\COMSOL61\Multiphysics\mli") 28 | try 29 | mphstart(comsolPort(labIndex)); 30 | catch 31 | fprintf("Something went wrong!\n"); 32 | end 33 | cd(currentDir); 34 | end 35 | 36 | function model = loadAndSetupModel(modelName, l, w, h) 37 | import('com.comsol.model.*'); 38 | import('com.comsol.model.util.*'); 39 | model = mphload(modelName, 'model1'); 40 | model.param.set('legl', l); 41 | model.param.set('legw', w); 42 | model.param.set('fraction', h); 43 | end 44 | 45 | function runAndVerifyModel(model, jobIndex, numOfJobs) 46 | model.study('std1').run; 47 | model.mesh('mpart1').feature('imp1').importData; 48 | fprintf("Verifying TO %d/%d ...\n", jobIndex, numOfJobs); 49 | try 50 | model.study('std2').run; 51 | catch 52 | fprintf("Extrude function failed, skipped.\n"); 53 | end 54 | end 55 | 56 | function exportResultsAndSaveModel(model, storage, jobIndex, l, w, h) 57 | exportTags = ["img1","img2","img3","img4","img5","tbl1","tbl2"]; 58 | postfixes = ["_sol.png","_out.png","_sx.png","_sy.png","_disp.png","_accu.csv","_veri.csv"]; 59 | numOfExports = length(postfixes); 60 | for exportIndex = 1:numOfExports 61 | fileName = storage + num2str(jobIndex) + "_" + num2str(exportIndex) + "_" + l + "_" + w + "_" + h + postfixes(exportIndex); 62 | model.result.export(exportTags(exportIndex)).set("filename", fileName); 63 | model.result.export(exportTags(exportIndex)).run; 64 | if exportIndex == numOfExports 65 | writematrix([jobIndex, l, w, h, readmatrix(fileName)], storage + "TO_data.csv",'WriteMode','append'); 66 | end 67 | end 68 | %mphsave(model, storage + "TO_" + num2str(jobIndex), 'copy', 'on'); 69 | end --------------------------------------------------------------------------------