Comsol

-added demo scripts and a demo model
parent 1128ae0f
% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>
%% The ITA Toolbox COMSOL interface
% This interface allows to do modifications to COMSOL models and execute
% certain commands. It is based on COMSOL LiveLink for Matlab.
% It is important to mention, that the basis of the model should be
% developed in in COMSOL itself (using the GUI or LiveLink).
%% Demos
% Below you can find a list of demo scripts for the COMSOL interface.
% Open the scripts listed here to learn more!
%% Connecting to COMSOL Server and loading a model
ita_comsol_demo_init();
%% Running a simulation
ita_comsol_demo_simulation();
%% Create sources
ita_comsol_demo_sources();
%% Adjust boundary conditions
ita_comsol_demo_boundary_conditions();
%% Create a binaural receiver
ita_comsol_demo_binaural_receiver();
%% Remove model from server and disconnect
ita_comsol_demo_close();
\ No newline at end of file
% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>
%% Before you start
% Take a look at and run the following function
itaComsolModelObj = ita_comsol_demo_init();
%% Init model
ita_prepare_comsol_demo_model_for_simulation(itaComsolModelObj);
%% --- IMPORTANT NOTE ---
% When using a binaural receiver you have consider the following:
% 1) Make sure that your physics does not consider the interior of the
% receiver geometry. Therefore in the COMSOL GUI, click on the
% physics node and manually select the domain (instead of all domains).
%
% 2) If the geometry has a much finer structure than the rest of the
% model (e.g. pinna), the mesher might through an error. You have to
% make sure that the mesh can be fine enough. Thus, you should allow
% a very small "Minimum element size".
% There is no guaranty that a binaural receiver with arbitrary
% geometries will function. It has only been tested for the ITA
% Kunstkopf geometry.
%% --- Importing CAD data for binaural receivers ---
%% Create ITA Dummyhead
% NOTE:
% The following process requires access to a geometry file of the ITA
% Kunstkopf which is not publicly available. If you dont have access to
% this file, use the next option.
receiver = itaReceiver();
receiver.name = 'ITA_Dummyhead';
receiver.type = ReceiverType.ITADummyHead;
receiver.position = itaCoordinates([0 0 0]);
receiver.orientation = itaOrientation.FromViewUp([-1 -1 0], [0 0 1]);
comsolReceiver = itaComsolReceiver.Create(itaComsolModelObj, receiver);
%% Create user-defined binaural receiver
% NOTE:
% This is just an example. You need to provide a geometry file that can be
% imported in Comsol. The model should be centered at [0 0 0] and oriented
% in a way that the view vector shows in positive x-direction and up vector
% shows in positive z-direction. Additionally, you need to specify the
% relative position of the left and right ear canal (local coordinates).
receiver = itaReceiver();
receiver.name = 'Dummyhead';
receiver.type = ReceiverType.UserDefined;
receiver.position = itaCoordinates([0 0 0]);
receiver.orientation = itaOrientation.FromViewUp([-1 -1 0], [0 0 1]);
% Insert your personal data here!
receiver.geometryFilename = 'myfile.myext';
receiver.relativeLeftEarMicPosition = itaCoordinates([0 0.075 0]);
receiver.relativeRightEarMicPosition = itaCoordinates([0 -0.075 0]);
comsolReceiver = itaComsolReceiver.Create(itaComsolModelObj, receiver);
%% Disable receiver
%Note, that you have to rebuild the geometry in COMSOL UI to see the
%changes. Nevertheless, the changes would apply when starting a simulation.
comsolReceiver.Disable();
%% Enable receiver
comsolReceiver.Enable();
%% --- Run simulation ---
showProgress = true;
itaComsolModelObj.study.Run(showProgress);
%% --- Read binaural results ---
binauralResult = itaComsolModelObj.result.Pressure(receiver);
% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>
%% Before you start
% Take a look at and run the following function
itaComsolModelObj = ita_comsol_demo_init();
%% --- Apply impedances to boundaries ---
%When applying boundary conditions using this interface, COMSOL selections
%have to be defined in COMSOL beforehand. Make sure to label them properly!
%In the demo model, there are three selections: walls, ceiling and floor.
%% Read boundary group names (labels of 2D selections)
%'user' refers to a selection filter. See documentation of
%itaComsolSelection -> filter for more information
boundaryGroupNames = itaComsolModelObj.selection.BoundaryGroupNames('user');
disp(boundaryGroupNames)
ceilingLabel = boundaryGroupNames{2};
floorLabel = boundaryGroupNames{3};
%% Create impedance using itaMaterial
matCeiling = itaMaterial();
matCeiling.impedance = itaResult([1+2i; 2+4i; 3+6i],[20; 100; 1000], 'freq');
comsolImpedanceCeiling = itaComsolImpedance.Create(itaComsolModelObj, ceilingLabel, matCeiling);
%% Create impedance using itaResult
impedanceFloor = itaResult([1+1i; 1+1i; 1+1i],[20; 100; 1000], 'freq');
comsolImpedanceFloor = itaComsolImpedance.Create(itaComsolModelObj, floorLabel, impedanceFloor);
%% Disable impedance
comsolImpedanceFloor.Disable();
\ No newline at end of file
% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>
%% Before you start
% Take a look at and run the following function
itaComsolModelObj = ita_comsol_demo_init();
%% --- COMSOL - Clean up ---
%% Remove model from server to free memory
itaComsolServer.Instance().RemoveModel(itaComsolModelObj.modelNode)
%% Disconnect from server
itaComsolServer.Instance().Disconnect();
function itaComsolModelObj = ita_comsol_demo_init()
% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>
%% Before you start
% - make sure COMSOL is installed on you computer
% - make sure you have the licences for the following COMSOL products
% - COMSOL Multiphysics
% - Acoustics Module
% - LiveLink for Matlab
% - CAD Import Module (only necessary when importing CAD data)
% - if you use this interface the first time you might have to select the
% path to the comsolmphserver.exe and Matlab LiveLink.
% - The COMSOL path is usually located here:
% C:\Program Files\COMSOL\COMSOL5X\Multiphysics
% - comsolmphserver is located in the subfolder \bin\win64
% - Matlab LiveLink folder is called \mli
%
% Note, that this interface is built upon the COMSOL Matlab LiveLink
% application. It might be helpful to check out the documentation
% provided by COMSOL.
%% ---COMSOL initialization---
%% Start and connect Server & LiveLink
%Note that you might have to set credentials for your local server the first
%time it is started!
comsolServer = itaComsolServer.Instance();
comsolServer.Connect();
%% Load a Comsol model via LiveLink
%You should give your model a unique tag to avoid conflicts when running
%multiple models at once
modelTag = 'itaDemoModel';
demofolder = fileparts(mfilename('fullpath'));
comsolModelObj = mphload(fullfile(demofolder, 'ita_comsol_demo_model.mph'), modelTag);
%% Connect COMSOL client to server
%To visualize the changes we are going to apply to the model later on, it
%is helpful to link an instance of the COMSOL application to the local
%server:
%Therefore, start the COMSOL application and select
% File -> COMSOL Multiphysics Server -> Connect to Server
%Then, import the demo model from the server using
% File -> COMSOL Multiphysics Server -> Import Application from Server
%% --- ITA initialization ---
%% Create an itaComsolModel
itaComsolModelObj = itaComsolModel(comsolModelObj);
% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>
%% Before you start
% Take a look at and run the following function
itaComsolModelObj = ita_comsol_demo_init();
%% Init model for simulation
ita_prepare_comsol_demo_model_for_simulation(itaComsolModelObj);
%% --- Simulation from Matlab ---
%% Set frequencies to be solved
itaComsolModelObj.study.SetFrequencyVector([100 200])
%% Run simulation
showProgress = true;
itaComsolModelObj.study.Run(showProgress);
%% --- Results in COMSOL ---
% In the Comsol GUI, take a look under Results -> 3D Plots
%% --- Read results ---
%% at mesh nodes
resultAtMeshNodes = itaComsolModelObj.result.Pressure();
%% at user defined points within mesh
[x,y,z] = meshgrid(-1:1, -1:1, -1:1);
coords = itaCoordinates([x(:) y(:) z(:)]);
resultAtUserDefinedCoords = itaComsolModelObj.result.Pressure(coords);
\ No newline at end of file
% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>
%% Before you start
% Take a look at and run the following function
itaComsolModelObj = ita_comsol_demo_init();
%% Create a monopole source in COMSOL
monopole = itaSource;
monopole.name = 'pointSource';
monopole.type = SourceType.PointSource;
monopole.sensitivityType = SensitivityType.Flat; %Flat source spectrum
monopole.position = itaCoordinates([-1 -1 1]);
comsolMonopole = itaComsolSource.Create(itaComsolModelObj, monopole);
%% Create a piston in COMSOL
%Note: A Piston has to be placed on a wall
piston = itaSource;
piston.name = 'pistonSource';
piston.type = SourceType.Piston;
piston.sensitivityType = SensitivityType.UserDefined; %Arbitry source spectrum
piston.velocityTf = itaResult([0 1 1 0].', [0 100 16000 20000], 'freq');
piston.pistonRadius = 0.3;
piston.position = itaCoordinates([-2 -1 1]);
piston.orientation = itaOrientation.FromViewUp([1 0 0], [0 0 1]);
comsolPiston = itaComsolSource.Create(itaComsolModelObj, piston);
%% Disable a itaComsolSource
%Note, that you have to rebuild the geometry in COMSOL UI to see the
%changes. Nevertheless, the changes would apply when starting a simulation.
comsolPiston.Disable();
\ No newline at end of file
function ita_prepare_comsol_demo_model_for_simulation(itaComsolModelObj)
% ita_prepare_comsol_demo_model_for_simulation This function is a helper
% function of the COMSOL interface demo.
% See ita_comsol_demo
% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>
monopole = itaSource;
monopole.name = 'pointSource';
monopole.type = SourceType.PointSource;
monopole.sensitivityType = SensitivityType.Flat; %Flat source spectrum
monopole.position = itaCoordinates([-1 -1 1]);
itaComsolSource.Create(itaComsolModelObj, monopole);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment