├── CITATION.cff ├── README.md ├── LICENSE └── voronoi_geometry_comsol_matlab_livelink.m /CITATION.cff: -------------------------------------------------------------------------------- 1 | cff-version: 1.0.0.0 2 | message: "If you use this software, please cite it as below." 3 | authors: 4 | - family-names: Oliveira 5 | given-names: Aloadir Lucas Santos 6 | orcid: https://orcid.org/0000-0002-8006-4789 7 | title: "voronoi_geometry_matlab_comsol" 8 | version: 1.0.0.0 9 | doi: 10.5281/zenodo.6413143 10 | date-released: 2022-04-04 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # voronoi_geometry_matlab_comsol 2 | 3 | This is a MATLAB script that uses the interface LiveLink for MATLAB. 4 | 5 | ## What is this for? 6 | 7 | This script uses the work of Park* to generate a Voronoi diagram and export it to Comsol Multiphysics as a 2D geometry through LiveLink for MATLAB. This is part of my PhD research project. 8 | 9 | * Hyongju Park (2022). Polytope bounded Voronoi diagram in 2D and 3D (https://github.com/hyongju/Polytope-bounded-Voronoi-diagram/releases/tag/1.15), GitHub. Retrieved April 5, 2022. 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Aloadir Oliveira 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /voronoi_geometry_comsol_matlab_livelink.m: -------------------------------------------------------------------------------- 1 | % This script is based on a project on the Mathworks website* that creates 2 | % Voronoi diagrams. Here I developed a way to use these diagrams from 3 | % Matlab and export the to Comsol, via the Livelink for Matlab module of 4 | % Comsol. 5 | 6 | % * https://www.mathworks.com/matlabcentral/fileexchange/50772-polytope-bounded-voronoi-diagram-in-2d-and-3d 7 | 8 | % Download the required files to the Voronoi diagram creation from its Github repository 9 | % If you want to automatic download this files just uncoment the following 10 | % lines: 11 | % urlwrite("https://github.com/hyongju/Polytope-bounded-Voronoi-diagram/blob/master/MY_con2vert.m","MY_con2vert.m"); 12 | % urlwrite("https://github.com/hyongju/Polytope-bounded-Voronoi-diagram/blob/master/MY_intersect.m","MY_intersect.m"); 13 | % urlwrite("https://github.com/hyongju/Polytope-bounded-Voronoi-diagram/blob/master/MY_setdiff.m","MY_setdiff.m"); 14 | % urlwrite("https://github.com/hyongju/Polytope-bounded-Voronoi-diagram/blob/master/demo.m","demo.m"); 15 | % urlwrite("https://github.com/hyongju/Polytope-bounded-Voronoi-diagram/blob/master/inhull.m","inhull.m"); 16 | % urlwrite("https://github.com/hyongju/Polytope-bounded-Voronoi-diagram/blob/master/pbisec.m","pbisec.m"); 17 | % urlwrite("https://github.com/hyongju/Polytope-bounded-Voronoi-diagram/blob/master/polybnd_voronoi.m","polybnd_voronoi.m"); 18 | % urlwrite("https://github.com/hyongju/Polytope-bounded-Voronoi-diagram/blob/master/vert2lcon.m","vert2lcon.m"); 19 | 20 | % The original script ("demo.m") that generates the voronoi geometry can create 2 and 21 | % 3 dimension objectsand the variable "dimension" defines which one. At this 22 | % moment, this code only uses the 2D case. Part of the 3D functions still 23 | % here for future use, if I return to that. 24 | dimension = 2; % dimension of the space 25 | 26 | % The original script ("demo.m") uses a variable named "m" to define 27 | % a number of points that would be the vertices of the polygon that 28 | % would cointain the voronoi geometry. 29 | 30 | % The original script contained the following lines to generate the random 31 | % boundary points for the voronoi geometry and positions for the cells 32 | % m = 50; % number of boundary point-candidates 33 | % bnd0 = rand(m,d); % generate random boundary point-candidates 34 | 35 | % This program substitutes this "m" sided polygon for a square. The size of 36 | % the sides is defined by the variable "s". Here the program uses a 37 | % unitary size, and later it will reescale this multiplying it by a scale 38 | % factor. 39 | s = 1; % Size os the square that delimits the geometry 40 | 41 | % The variable "nPoints" defines the number of cells that the voronoi 42 | % geometry will contain 43 | nPoints = 30; % number of points. 44 | 45 | tolerance = 1e-15; % tolerance value used in "inhull.m" (larger value high precision, possible numerical error) 46 | 47 | % The variable "pos0" defines random positions for the center of each 48 | % voronoi cell 49 | pos0 = rand(nPoints,dimension); % generate random points 50 | 51 | % This is a legacy from the original script of the possibility of 2D and 3D 52 | % geometries. Here we are set to use the "case 2", but the "case 3" ooption 53 | % still here for future users. 54 | switch dimension 55 | case 2 56 | bnd0 = [0 0; s 0; s s; 0 s]; % square boundaries 57 | case 3 58 | bnd0 = rand(m,dimension); % generate boundary point-candidates 59 | end 60 | 61 | % This step is necessary for the more complex polynomial, on the case of 62 | % the 2D square this will only add the [0,0] on the end of the variable 63 | % "bnd0" 64 | K = convhull(bnd0); % computes the 2-D or 3-D convex hull of the points in matrix bnd0 65 | bnd_pnts = bnd0(K,:); % take boundary points from vertices of convex polytope formed with the boundary point-candidates 66 | 67 | % Check if the random generated points are inside a convex hull in n 68 | % dimensions. For the 2D case all the points will be valid and this will 69 | % create a array containing an array of ones with lenght of "nPoints" 70 | 71 | in = inhull(pos0,bnd0,[],tolerance); 72 | 73 | % We use this function to choose points that are inside the defined boundary 74 | % For the case of the 2D square of size "s" every point will be on the 75 | % boundaries, so the condition "if in(i) == 1 will be true for all points 76 | 77 | u1 = 0; 78 | pos = zeros(size(pos0,1),dimension); 79 | 80 | for i = 1:size(pos0,1) 81 | if in(i) == 1 82 | u1 = u1 + 1; 83 | pos(u1,:) = pos0(i,:); 84 | end 85 | end 86 | %% Here the program applies the script that creates the voronoi geometry 87 | % ========================================================================= 88 | % INPUTS: 89 | % pos points that are in the boundary n x d matrix (n: number of points d: dimension) 90 | % bnd_pnts points that defines the boundary m x d matrix (m: number of vertices for the convex polytope 91 | % boundary d: dimension) 92 | % ------------------------------------------------------------------------- 93 | % OUTPUTS: 94 | % vornb Voronoi neighbors for each generator point: n x 1 cells 95 | % vorvx Voronoi vertices for each generator point: n x 1 cells 96 | % ========================================================================= 97 | [vornb,vorvx] = polybnd_voronoi(pos,bnd_pnts); 98 | 99 | clearvars -except vorvx pos s nPoints 100 | %% Initializing the Comsol objects 101 | % The code starts creating the first objects os a Comsol simulation, the model and component nodes. 102 | 103 | % Create Model Object 104 | model = ModelUtil.create("Model"); 105 | % Create 2D Component 106 | comp1 = model.component.create("comp1",true); 107 | 108 | %General properties 109 | % The "s_Matlab" parameter is the same of the "s" variable on the Matlab 110 | % code, that defines the size of the square which is the sample generated. 111 | model.param().set("s_Matlab", "1", "Size os the square that delimits the geometry on Matlab"); 112 | 113 | % Quantity of random directions that the grains of the ceramic will have. 114 | quant_dir = 10; 115 | %% Create Voronoi Geometry 116 | g1 = comp1.geom.create("g1",2); 117 | g1.lengthUnit("mm"); 118 | 119 | square = model.component("comp1").geom("g1").create("sq1", "Square"); 120 | square.set("size", s); 121 | 122 | % This loop 123 | 124 | for i=1:size(vorvx,2) 125 | coord_x = []; 126 | coord_y = []; 127 | for j=1:size(vorvx{1,i},1) 128 | coord_x = [coord_x vorvx{1,i}(j,1)]; 129 | coord_y = [coord_y vorvx{1,i}(j,2)]; 130 | end 131 | coord = []; 132 | coord = [coord_x ; coord_y]; 133 | a = g1.feature.create("c"+i,"BezierPolygon"); 134 | a.set("type","open"); 135 | a.set("degree",1); 136 | a.set("p",coord); 137 | 138 | csol = g1.feature.create("csol"+i,"ConvertToSolid").set("keep", false); 139 | csol.set("repairtoltype", "absolute"); 140 | csol.set("absrepairtol", 1.0E-15); 141 | csol.selection("input").set("c"+i); 142 | end 143 | 144 | % clearvars a inputset j coord coord_x coord_y nPoints 145 | g1.run; --------------------------------------------------------------------------------