From 1903040975b0032d003314086c97b2a1f3756f0b Mon Sep 17 00:00:00 2001
From: Mauco03 <marco.gaibotti@skywarder.eu>
Date: Sun, 5 May 2024 20:44:12 +0200
Subject: [PATCH] [refactoring-ode][classes] Improved object constructor

---
 classes/Bay.m                           |  2 +-
 classes/Component.m                     | 42 ++-----------------------
 classes/Rocket.m                        |  3 +-
 classes/bays/Motor.m                    | 13 ++++----
 classes/components/Environment.m        | 12 ++++---
 classes/components/WindCustom.m         |  4 +--
 functions/eventFunctions/eventLanding.m |  2 +-
 7 files changed, 23 insertions(+), 55 deletions(-)

diff --git a/classes/Bay.m b/classes/Bay.m
index d051373..b7e1775 100644
--- a/classes/Bay.m
+++ b/classes/Bay.m
@@ -10,7 +10,7 @@ classdef Bay < Component & matlab.mixin.Heterogeneous
         xCg         % [m]       Cg relative to bay upper side
     end
 
-    methods (Static,Sealed,Access=protected)
+    methods (Static, Sealed, Access=protected)
         function default = getDefaultScalarElement
             default = Payload;
         end
diff --git a/classes/Component.m b/classes/Component.m
index 554d155..5d90bc9 100644
--- a/classes/Component.m
+++ b/classes/Component.m
@@ -28,48 +28,12 @@ classdef Component < Config
                 varName = strtok(fileName, 'C');
             end
 
-            [m, n] = size(varsIn.(varName));
-            objMatrix(m,n) = obj;
-
-            for i = 1:m
-                for j = 1:n
-                    varIn.(varName) = varsIn.(varName)(i,j);
-                    objMatrix(i,j).loadConfig(varIn);
-                end
-            end
-            obj = objMatrix;
+            [varsIn.(varName).mission] = deal(mission);
+            obj = varsIn.(varName);
         end
     end
-
+    
     methods(Access = protected)
-        function loadConfig(obj, varargin)
-            % This method loads desired configs into calling component.
-            % The component can chose between:
-            %   - Only specifying the configName (name of the config file):
-            %       The loader will look for a variable named like the
-            %       config file.
-            %   - Specifying both the configName and the variableName:
-            %       Useful for when you want to group multiple small
-            %       objects into a larger config file
-            %
-            % Syntax:
-            %   obj.loadConfig()        - To load from a config file
-            %   obj.loadConfig(vars)    - To load from given variables
-
-            fileName = obj.configName;
-            varName = obj.variableName;
-            if isempty(varName), varName = strtok(fileName, 'C'); end
-
-            if nargin == 2, configObj = varargin{end}.(varName);
-            else, configObj = obj.getConfig(); end
-
-            fields = obj.getProperties('writable');
-            for j = 1:size(fields, 2)
-                if isempty(configObj.(fields{j})), continue; end
-                obj.(fields{j}) = configObj.(fields{j}); 
-            end
-        end
-
         function outputVariables = getConfig(obj)
             fileName = obj.configName;
             filePath = obj.mission.configPath;
diff --git a/classes/Rocket.m b/classes/Rocket.m
index 8559326..517f748 100644
--- a/classes/Rocket.m
+++ b/classes/Rocket.m
@@ -229,12 +229,13 @@ classdef Rocket < Component
                 options.checkGeometry       logical = true
             end
             obj@Component(mission, varIn);
-            obj.mission = mission;
 
             %% Loading data
             if nargin == 0
                 return;
             end
+            obj.mission = mission;
+
             vars             = obj.getConfig(); % Load config once and apply to other bays
             obj.payload      = Payload(mission, vars);
             obj.recovery     = Recovery(mission, vars);
diff --git a/classes/bays/Motor.m b/classes/bays/Motor.m
index 3f1598b..909d992 100644
--- a/classes/bays/Motor.m
+++ b/classes/bays/Motor.m
@@ -30,7 +30,7 @@ classdef Motor < Bay
         ae                  double                  % [Pa] Eflux Area
     end
 
-    properties(Dependent)
+    properties
         mass                                        % [kg] Total Motor mass
         fuselageXCg         double                  % [m]  xcg of the engine fuselage only from tank tip
     end
@@ -53,20 +53,19 @@ classdef Motor < Bay
             end
             obj@Bay(mission, varIn);
             obj.loadData();
+            obj.updateAll();
         end
 
         function set.name(obj, name)
             obj.name = name;
             obj.loadData();
+            obj.updateAll();
         end
 
-        function mass = get.mass(obj)
-            mass = obj.propellantMass + obj.structureMass;
-        end
-
-        function fuselageXCg = get.fuselageXCg(obj)
-            fuselageXCg = (obj.length - ...
+        function updateAll(obj)
+            obj.fuselageXCg = (obj.length - ...
                 obj.tankLength)/2 + obj.tankLength;
+            obj.mass = obj.propellantMass + obj.structureMass;
         end
     end
 
diff --git a/classes/components/Environment.m b/classes/components/Environment.m
index cbdc0e7..4c68061 100644
--- a/classes/components/Environment.m
+++ b/classes/components/Environment.m
@@ -56,15 +56,19 @@ classdef Environment < Component
             obj.omega = deg2rad(obj.omega);
             obj.phi = deg2rad(obj.phi);
 
-            obj.updateG0;
-            obj.updatePinDistance;
-            obj.updateRamp;
-            obj.updateLocal;
+            obj.updateAll();
         end
     end
     
     % Updaters
     methods
+        function updateAll(obj)
+            obj.updateG0;
+            obj.updatePinDistance;
+            obj.updateRamp;
+            obj.updateLocal;
+        end
+
         function obj = updateG0(obj)
             obj.g0 = gravitywgs84(obj.z0, obj.lat0);
         end
diff --git a/classes/components/WindCustom.m b/classes/components/WindCustom.m
index af8f0f8..99d26ba 100644
--- a/classes/components/WindCustom.m
+++ b/classes/components/WindCustom.m
@@ -38,7 +38,7 @@ classdef WindCustom < Component
                 varIn = []
             end
             obj@Component(mission, varIn);
-            obj.setData();
+            obj.updateAll();
         end
 
         function [uw, vw, ww] = getVels(obj, z)
@@ -57,7 +57,7 @@ classdef WindCustom < Component
             end
         end
 
-        function obj = setData(obj)
+        function updateAll(obj)
             s = length(obj.altitudes);
             magVector = nan(1,s);
             azVector = nan(1,s);
diff --git a/functions/eventFunctions/eventLanding.m b/functions/eventFunctions/eventLanding.m
index 3df933f..fc7aa9d 100644
--- a/functions/eventFunctions/eventLanding.m
+++ b/functions/eventFunctions/eventLanding.m
@@ -1,4 +1,4 @@
-function [value, isterminal, direction] = eventLanding(~, Y, ~, ~, ~, ~)
+function [value, isterminal, direction] = eventLanding(~, Y, varargin)
 %{
 eventLanding - Event function to stop simulation at landing checking when a
                value tends to zero; the value taken is to account is the 
-- 
GitLab