Skip to content

Commit

Permalink
Add demo for two components
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-gonz committed Nov 14, 2023
1 parent b1604ab commit a3de16e
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 7 deletions.
Binary file modified app.mlapp
Binary file not shown.
32 changes: 27 additions & 5 deletions src/demo.m
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
% Parser
parser_constants = ParserConstants();
% To initialize a parser: parser = Parser(file_path, H_unit, M_unit, curve_type)
% curve_type options:
% ANHYSTERETIC_CURVE_TYPE
% HYSTERESIS_LOOP_TYPE
% Refer ParserConstants.m to explore all units
parser = Parser('.\data\sampleData\Finemet - TA.csv', parser_constants.H_AMPERE_PER_METER, parser_constants.B_TESLA, parser_constants.HYSTERESIS_LOOP_TYPE);
[H, M, H_raw, M_raw] = parser.import();

data_curve = DataAnhystereticCurve(H, M);

seed = [5 0.6]; %[Hcr mcr]
lower_bound = [4 0.5]; %[(lower bound for Hcr), (lower bound for mcr)]
upper_bound = [6 0.7]; %[(upper bound for Hcr), (upper bound for mcr)]
select_fit = {true true}; %[(boolean for fitting Hcr), (boolean for fitting mcr)]
% Set values for seed: seed = [Hcr m(Hcr)]
seed = [5 0.6];

% Set values for lower bounds: lower_bounds = [Hcr m(Hcr)]
lower_bound = [4 0.5];

% Set values for upper bounds: upper_bounds = [Hcr m(Hcr)]
upper_bound = [6 0.7];

% Set values for fitting booleans: slect_fit = [Hcr m(Hcr)]
select_fit = {true true};

fit_constants = FitContants();
% To use fit function: [Hcr, mcr, Hx] = fit(data_curve, seed, select_a, error_type, lower_bound, upper_bound, select_fit)
% select_a options:
% fit_constants.LOW_A
% fit_constants.HIGH_A
% error_type options:
% fit_constants.HORIZONTAL_ERROR_TYPE
% fit_constants.VERTICAL_ERROR_TYPE
% fit_constants.DIAGONAL_ERROR_TYPE
[Hcr, mcr, Hx] = fit(data_curve, seed, fit_constants.LOW_A, fit_constants.DIAGONAL_ERROR_TYPE, lower_bound, upper_bound, select_fit);

magnetic_parameters_constants = MagneticParametersConstants();
% To initialize magnetic_parameters: magnetic_parameters = MagneticParameters(data_curve, Hcr, mcr, Hx, select_a)
% select_a options:
% magnetic_parameters_constants.LOW_A
% magnetic_parameters_constants.HIGH_A
magnetic_parameters = MagneticParameters(data_curve, Hcr, mcr, Hx, magnetic_parameters_constants.LOW_A);

[HTip, ~] = Utils().find_tip(data_curve.H, data_curve.M);
Expand Down Expand Up @@ -59,7 +80,8 @@


% Plot
colors = [ 0.58 0 0.70; 0.70 0 0]; %[R G B; R G B] 0-1 scale
% Set colors: colors = [R G B; R G B] 0-1 scale
colors = [ 0.58 0 0.70; 0.70 0 0];
plotter = Plotter(data_curve, modeled_curve, Hcr, colors);

figure();
Expand Down
96 changes: 94 additions & 2 deletions src/demo_2_components.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,96 @@
parser = Parser('.\data\sampleData\Finemet - TA.csv', "H [A/m]", "B [T]", "Hysteretic loop");
% Parser
parser_constants = ParserConstants();
% To initialize a parser: parser = Parser(file_path, H_unit, M_unit, curve_type)
% To initialize a parser: parser = Parser(file_path, H_unit, M_unit, curve_type)
% curve_type options:
% ANHYSTERETIC_CURVE_TYPE
% HYSTERESIS_LOOP_TYPE
% Refer ParserConstants.m to explore all units
parser = Parser('.\data\sampleData\Finemet - TA.csv', parser_constants.H_AMPERE_PER_METER, parser_constants.B_TESLA, parser_constants.HYSTERESIS_LOOP_TYPE);
[H, M, H_raw, M_raw] = parser.import();

data_curve = DataAnhystereticCurve(H, M);
data_curve = DataAnhystereticCurve(H, M);

% Set values for seed: seed = [Hcr1 Hcr2 m(Hcr1) m(Hcr2) Hx1]
seed = [0.0100 0.0200 0.5000 0.5000 0.0150];

% Set values for lower bounds: lower_bounds = [Hcr1 Hcr2 m(Hcr1) m(Hcr2) Hx1]
lower_bound = [0; 0; 0.4496; 0.4496; 0];

% Set values for upper bounds: upper_bounds = [Hcr1 Hcr2 m(Hcr1) m(Hcr2) Hx1]
upper_bound = [1000000; 1000000; 1; 1; 1000000];

% Set boolean values for fitting: slect_fit = [Hcr1 Hcr2 m(Hcr1) m(Hcr2) Hx1]
select_fit = {true; true; true; true; true};

% Set low/high value for ai: select_a = categorical({'a1';'a2'});
select_a = categorical({'low';'low'});

fit_constants = FitContants();
% To use fit function: [Hcr, mcr, Hx] = fit(data_curve, seed, select_a, error_type, lower_bound, upper_bound, select_fit)
[Hcr, mcr, Hx] = fit(data_curve, seed, select_a, fit_constants.DIAGONAL_ERROR_TYPE, lower_bound, upper_bound, select_fit);

magnetic_parameters_constants = MagneticParametersConstants();
% To initialize magnetic_parameters: magnetic_parameters = MagneticParameters(data_curve, Hcr, mcr, Hx, select_a)
% select_a options:
% magnetic_parameters_constants.LOW_A
% magnetic_parameters_constants.HIGH_A
magnetic_parameters = MagneticParameters(data_curve, Hcr, mcr, Hx, magnetic_parameters_constants.LOW_A);

% Get modeled anhysteretic curve
[HTip, ~] = Utils().find_tip(data_curve.H, data_curve.M);
N = 100;
Hhat = logspace(log10(data_curve.H(2)),log10(HTip),N);

modeled_curve = ModeledAnhystereticCurve(Hhat, magnetic_parameters);


% Errors
error_calculator = DiagonalErrorCalculator(data_curve, modeled_curve);
diagonal_error = error_calculator.get_error();
disp("Diagonal error:")
disp(diagonal_error)

error_calculator = VerticalErrorCalculator(data_curve, modeled_curve);
vertical_error = error_calculator.get_error();
disp("Vertical error:")
disp(vertical_error)

error_calculator = HorizontalErrorCalculator(data_curve, modeled_curve);
horizontal_error = error_calculator.get_error();
disp("Horizontal error:")
disp(horizontal_error)

% Residues
residue_calculator = MagnetizationResidueCalculator(data_curve, modeled_curve);
magnetization_residue = residue_calculator.get_residue();

% To initialize residue_plotter: residue_plotter = ResiduePlotter(X, Y, Xhat, Yhat, Residue, Log, Label, varargin)
residue_plotter = ResiduePlotter(data_curve.H(2:end-1), data_curve.M(2:end-1), modeled_curve.H, modeled_curve.M, magnetization_residue, true, "M [A/m]");
residue_plotter.plot()

residue_calculator = SusceptibilityResidueCalculator(data_curve, modeled_curve);
susceptibility_residue = residue_calculator.get_residue();

residue_calculator = SemilogDerivativeResidueCalculator(data_curve, modeled_curve);
semilog_derivative_residue = residue_calculator.get_residue();


% Plot
% Set colors: colors = [R G B; R G B] 0-1 scale
colors = [0.58 0 0.70; 0.70 0 0; 0 0 0.70];
plotter = Plotter(data_curve, modeled_curve, Hcr, colors);

figure();
ax = nexttile;
plot_components = true;
plot_grid = true;
plotter.plot_M(ax, plot_components, plot_grid);

figure();
ax = nexttile;
plotter.plot_HdMdH_log(ax, plot_components, plot_grid);

figure();
ax = nexttile;
plotter.plot_raw_log(ax, modeled_curve.H, modeled_curve.M, "H", "M", "plot con log")

0 comments on commit a3de16e

Please sign in to comment.