Skip to content

Commit

Permalink
Merge pull request #116 from AlbertoCuadra/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
AlbertoCuadra committed Dec 14, 2021
2 parents 8b7a9f2 + f0a88df commit b43bdc9
Show file tree
Hide file tree
Showing 27 changed files with 436 additions and 79 deletions.
8 changes: 8 additions & 0 deletions Databases/Functions/set_g0.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function g0 = set_g0(ls, TP, DB)
% Function that computes the vector of gibbs free energy for the given
% set of species [J/mol]
for i=length(ls):-1:1
species = ls{i};
g0(i, 1) = species_g0(species, TP, DB) * 1e3;
end
end
30 changes: 30 additions & 0 deletions Examples/Example_TP_seminar.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
% -------------------------------------------------------------------------
% EXAMPLE: TP_seminar
%
% Compute equilibrium composition at defined temperature (e.g., 1500 K) and
% pressure (e.g., 1.01325 bar) for a lean H2-Br2 mixture at
% standard conditions, and a set of 3 species considered.
%
% species == {'H2', 'Br2', 'HBr'}
%
% See wiki or ListSpecies() for more predefined sets of species
%
% @author: Alberto Cuadra Lara
% PhD Candidate - Group Fluid Mechanics
% Universidad Carlos III de Madrid
%
% Last update Dec 10 2021
% -------------------------------------------------------------------------

%% INITIALIZE
self = App({'H2', 'Br2', 'HBr'});
%% INITIAL CONDITIONS
self = set_prop(self, 'TR', 300, 'pR', 1 * 1.01325);
self.PD.S_Fuel = {'H2', 'Br2'};
self.PD.N_Fuel = [0.75, 0.25];
%% ADDITIONAL INPUTS (DEPENDS OF THE PROBLEM SELECTED)
self = set_prop(self, 'pP', self.PD.pR.value, 'TP', 1500);
%% SOLVE PROBLEM
self = SolveProblem(self, 'TP');
%% DISPLAY RESULTS (PLOTS)
postResults(self);
39 changes: 26 additions & 13 deletions GUI/Functions/gui_CalculateButtonPushed.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
% Save results
results = save_results(obj, app);
% Update GUI with the last results of the set
obj = update_results_gui(obj, results);
obj = gui_update_results(obj, results);
% Display results (plots)
postResults(app);
% Set lamp to Done color
Expand All @@ -34,27 +34,40 @@
end

% SUB-PASS FUNCTIONS
function obj = update_results_gui(obj, app)
% Update GUI with the results computed
function obj = gui_update_results(obj, results)
% Update:
% 1. GUI with the last results computed
% 2. GUI-UITree with all the results

% Update GUI with the last result computed
gui_write_results(obj, results, 1);
% Update UITree with all the results
gui_add_nodes(obj.Node_Results, results)
end

function obj = get_listSpecies_gui(obj)
obj.LS = obj.listbox_Products.Items;
if isempty(obj.LS)
% Get default value
obj.LS = 'Soot Formation';
obj.LS = ListSpecies([], 'Soot Formation');
end
end
function results = save_results(obj, app)
% Save results in the UITree
% 1. Save data
results.mix1 = app.PS.strR;
results.mix2 = app.PS.strP;
results.ProblemType = obj.ProblemType.Value;
results.reaction = obj.Reaction.Value;
results.LS = obj.LS;
results.LS_products = obj.LS_products;
% 2. Save results in the UITree
% Save results
N = length(app.PS.strR);
for i = N:-1:1
results(i).mix1 = app.PS.strR{i};
results(i).mix2 = app.PS.strP{i};
results(i).ProblemType = obj.ProblemType.Value;
results(i).Reactants = obj.Reactants.Items{sscanf(obj.Reactants.Value, '%d')};
results(i).Products = obj.Products.Value;
if isempty(results(i).Products)
results(i).Products = 'Default';
end
results(i).LS = app.S.LS;
results(i).LS_products = obj.LS;
results(i).UITable_R_Data = obj.UITable_R.Data;
end
end

function app = get_input_constrains(obj, app)
Expand Down
10 changes: 5 additions & 5 deletions GUI/Functions/gui_ProblemTypeValueChanged.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ function gui_ProblemTypeValueChanged(obj)
% Set default input values
obj.PR1.Value = '300';
obj.PR2.Value = '1';
obj.PR3.Value = '0';
obj.PR4.Value = '0';
obj.PR4.Value = '2';
gui_compute_mach_or_velocity(obj, 'Mach');
% Set visible item volume ratio
obj.text_vP_vR.Visible = 'off';
obj.text_vP_vR.Visible = 'off';
% Set invisible shocks/detonation items
gui_visible_shocks(obj, true);
case 'SHOCK_R' % * SHOCK_R: CALCULATE PLANAR POST-REFLECTED SHOCK STATE
Expand All @@ -144,8 +144,8 @@ function gui_ProblemTypeValueChanged(obj)
% Set default input values
obj.PR1.Value = '300';
obj.PR2.Value = '1';
obj.PR3.Value = '0';
obj.PR4.Value = '0';
obj.PR4.Value = '2';
gui_compute_mach_or_velocity(obj, 'Mach');
% Set visible item volume ratio
obj.text_vP_vR.Visible = 'off';
% Set invisible shocks/detonation items
Expand Down
19 changes: 19 additions & 0 deletions GUI/Functions/gui_ProductsValueChanged.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function gui_ProductsValueChanged(obj)
% Update List of species considered as Products
try
if isempty(obj.Products.Value)
temp_app = ListSpecies(obj);
obj.listbox_Products.Items = temp_app.S.LS;
else
% Update List of Products depending of the value of the equivalence ratio
if strcmpi(obj.Products.Value, 'Complete Reaction')
gui_edit_phiValueChanged(obj, []);
else
temp_app = ListSpecies(obj, obj.Products.Value);
obj.listbox_Products.Items = temp_app.S.LS;
end
end
catch
obj.listbox_Products.Items = {};
end
end
9 changes: 0 additions & 9 deletions GUI/Functions/gui_ReactionValueChanged.m

This file was deleted.

48 changes: 48 additions & 0 deletions GUI/Functions/gui_add_nodes.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function gui_add_nodes(obj_parent, results)
% Function that generate nodes in a UITree and save data on them.
% 1. Generate Nodes (childrens)
% * Category 1: new children node: Results -> ProblemType
match_text = strcat('Problem Type: ', results(1).ProblemType);
category1 = set_node(obj_parent, match_text);
% * Category 2: new children node: ProblemType -> Reactants
match_text = strcat('Reactants: ', results(1).Reactants);
category2 = set_node(category1, match_text);
% * Category 3: new children node: Reactants -> ListProducts
match_text = strcat('List Products: ', results(1).Products);
category3 = set_node(category2, match_text);
% * Category 4: new children node: ListProducts -> cases
for i = 1:length(results)
match_text = sprintf('Case %d', category3.UserData);
set_node(category3, match_text, 'NodeData', results(i));
category3.UserData = category3.UserData + 1;
end
end

function category_children = set_node(category_parent, match_text, varargin)
% Default values
NodeData = [];
% Check varargin inputs
for i = 1:nargin - 3
switch lower(varargin{i})
case 'nodedata'
NodeData = varargin{i + 1};
end
end
% Set node
j = 0;
if ~isempty(category_parent.Children)
i = 1;
while i <= length(category_parent.Children)
if strcmpi(category_parent.Children(i).Text, match_text)
category_children = category_parent.Children(i);
j = 1;
break;
end
i = i + 1;
end
end
if j == 0
category_children = uitreenode(category_parent, 'Text', match_text, 'NodeData', NodeData, 'UserData', 1);
end
expand(category_parent,'all');
end
31 changes: 31 additions & 0 deletions GUI/Functions/gui_compute_mach_or_velocity.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function gui_compute_mach_or_velocity(obj, inputname)
% Function that computes the pre-shock Mach number of the mixture from
% a given pre-shock velocity or viceversa.
if any(contains(obj.ProblemType.Value, 'SHOCK', 'IgnoreCase', true))
if ~isempty(obj.UITable_R.Data)
temp_app = gui_create_temp_app(obj, [], false);
mix1 = temp_app.PS.strR{1};

if strcmpi(inputname, 'Mach')
[M1, FLAG] = gui_get_prop(obj, 'M1', obj.PR4.Value);
u1 = M1 * soundspeed(mix1);
obj.PR3.Value = compute_vector_or_scalar(u1, FLAG);
else
[u1, FLAG] = gui_get_prop(obj, 'u1', obj.PR3.Value);
M1 = u1 / soundspeed(mix1);
obj.PR4.Value = compute_vector_or_scalar(M1, FLAG);
end
else
obj.PR3.Value = '-';
end
end
end

% SUB-PASS FUNCTIONS
function value_char = compute_vector_or_scalar(value, FLAG)
if FLAG
value_char = sprintf('[%.2f:%.2f:%.2f]', value(1), value(2) - value(1), value(end));
else
value_char = sprintf('%.2f', value);
end
end
14 changes: 0 additions & 14 deletions GUI/Functions/gui_compute_propReactants.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,4 @@
app = get_FLAG_N(app);
% Compute stoichiometric matrix and properties of the mixture
app = Define_FOI(app, 1);
if ~isempty(app.PD.S_Fuel) && ~isempty(app.PD.S_Oxidizer)
% Compute percentage Fuel, Oxidizer/Fuel ratio and equivalence ratio
R = app.PD.R_Fuel + app.PD.R_Oxidizer + app.PD.R_Inert;
app.PS.strR{1}.percentage_Fuel = sum(app.PD.R_Fuel(:, 1)) / sum(R(:, 1)) * 100;
app.PS.strR{1}.FO = sum(app.PD.R_Fuel(:, 1)) / sum(app.PD.R_Oxidizer(:, 1));
app.PS.strR{1}.FO_st = sum(app.PD.R_Fuel(:, 1)) / (app.PS.strR_Fuel.x + app.PS.strR_Fuel.y/4 - app.PS.strR_Fuel.z/2);
app.PS.strR{1}.OF = 1/app.PS.strR{1}.FO;
app.PS.strR{1}.phi = app.PS.strR{1}.FO / app.PS.strR{1}.FO_st;
else
app.PS.strR{1}.percentage_Fuel = 0;
app.PS.strR{1}.FO = inf;
app.PS.strR{1}.OF = 0;
app.PS.strR{1}.phi = '-';
end
end
10 changes: 10 additions & 0 deletions GUI/Functions/gui_create_temp_app.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function app = gui_create_temp_app(obj, event, FLAG_COMPUTE_FROM_PHI)
% Function that creates an app required for preliminary calculations

% Initialize app (fast: transfer DB)
app = App('fast', obj.DB_master, obj.DB);
% Get reactant species
app = gui_get_reactants(obj, event, app, FLAG_COMPUTE_FROM_PHI);
% Compute properties of the mixture
app = gui_compute_propReactants(obj, app);
end
35 changes: 26 additions & 9 deletions GUI/Functions/gui_edit_phiValueChanged.m
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
function gui_edit_phiValueChanged(obj, event)
function [obj, temp_app] = gui_edit_phiValueChanged(obj, event)
% Update moles and mole fractions of the reactant UITables for the
% given equivalence ratio
try
% If UITable_R is empty the equivalence can not change
if isempty(obj.UITable_R.Data)
return
end
% Initialize app (fast: transfer DB)
app = App('fast', obj.DB_master, obj.DB);
% Set FLAG compute moles from equivalence ratio
FLAG_COMPUTE_FROM_PHI = true;
% Get reactant species
app = gui_get_reactants(obj, event, app, FLAG_COMPUTE_FROM_PHI);
% Compute properties of the mixture
app = gui_compute_propReactants(obj, app);
% Create temporaly app required for preliminary calculations
temp_app = gui_create_temp_app(obj, event, FLAG_COMPUTE_FROM_PHI);
% Update UITable classes
gui_update_UITable_R(obj, app);
gui_update_UITable_R(obj, temp_app);
obj.UITable_P.Data = obj.UITable_R.Data(:, 1); % (species, numer of moles, mole fractions, temperature)
obj.UITable_R2.Data = obj.UITable_R.Data(:, 1:3); % (species, numer of moles, mole fractions)
% Update GUI: equivalence ratio, O/F and percentage Fuel
gui_update_phi(obj, app);
obj.edit_phi2.Value = obj.edit_phi.Value;
obj.edit_OF.Value = 1/temp_app.PS.strR{1}.FO;
obj.edit_F.Value = temp_app.PS.strR{1}.percentage_Fuel;
% Check List of products (only in case of ListProducts == Complete reaction)
temp_app = check_ListProducts(obj, temp_app);
catch ME
message = {sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message)};
uialert(obj.UIFigure, message, 'Warning', 'Icon', 'warning');
end
end

function app = check_ListProducts(obj, app)
if strcmpi(obj.Products.Value, 'Complete Reaction')
if isempty(obj.UITable_R.Data)
% Set lamp to Error color
obj.Lamp.Color = obj.color_lamp_warning;
% Print error
message = {'First, define initial mixture'};
uialert(obj.UIFigure, message, 'Warning', 'Icon', 'warning');
end
% Update List of Products depending of the value of the equivalence ratio
phi = app.PS.strR{1}.phi;
phi_c = app.PS.strR{1}.phi_c;
app = ListSpecies(obj, obj.Products.Value, phi, phi_c);
obj.listbox_Products.Items = app.S.LS;
end
end
4 changes: 2 additions & 2 deletions GUI/Functions/gui_get_prop.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
else
flag_prop = false;
value = sscanf(value,'%f');
if isempty(value); value = app.PD.(name).value; return; end
app.PD.(name).value = value;
% if isempty(value); value = app.PD.(name).value; return; end
% app.PD.(name).value = value;
end

extra_parameters = nargin - 3;
Expand Down
8 changes: 8 additions & 0 deletions GUI/Functions/gui_update_from_uitree.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function gui_update_from_uitree(obj, selectedNodes)
% Update GUI with the data that correspond with the selected node from
% the UITree
if isempty(selectedNodes.Children)
results = selectedNodes.NodeData;
gui_write_results(obj, results, 1, 'FLAG_REACTANTS', true);
end
end
2 changes: 1 addition & 1 deletion GUI/Functions/gui_update_frozen.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ function gui_update_frozen(obj)
obj.listbox_Products.Items = {};
end
else
gui_ReactionValueChanged(obj)
gui_ProductsValueChanged(obj)
end
end
Loading

0 comments on commit b43bdc9

Please sign in to comment.