import simscape.battery.builder.*

%% Cell options
pouch = PouchGeometry(Length = simscape.Value(0.0745,"m"), ...
    Thickness = simscape.Value(0.0098,"m"), Height = simscape.Value(0.1745,"m"), ...
    TabLocation="Standard", ...
    TabWidth= simscape.Value(0.025, "m"), ...
    TabHeight= simscape.Value(0.02, "m"));

batteryCell = Cell(Geometry = pouch);

batteryCell.CellModelOptions.BlockParameters.T_dependence = "yes";
batteryCell.CellModelOptions.BlockParameters.thermal_port = "model";
batteryCell.Mass = simscape.Value(0.274, "kg");
batteryCell.CellModelOptions.BlockParameters.prm_dyn = "rc1";


%% Assembly
batteryParallelAssembly = ParallelAssembly(Cell = batteryCell, ...
    NumParallelCells = 1, ...
    Rows = 1, ...
    Topology = "SingleStack", ...
    ModelResolution = "Detailed", ...
    BalancingStrategy= "Passive", ...
    AmbientThermalPath= "CellBasedThermalResistance");


%% Module
batteryModule = Module(ParallelAssembly = batteryParallelAssembly,...
    NumSeriesAssemblies = 17, ...
    InterParallelAssemblyGap = simscape.Value(0.005,"m"), ...
    ModelResolution = "Detailed");

batteryModuleAssembly= ModuleAssembly(Module = repmat(batteryModule,1,1), ...
    InterModuleGap = simscape.Value(0.05,"m"));


%% BatteryPack
batteryPack= Pack(ModuleAssembly = repmat(batteryModuleAssembly,1,8), ...
    InterModuleAssemblyGap = simscape.Value(0.02,"m"));


%% Balancing
batteryPack.BalancingStrategy = "Passive";
batteryPack.AmbientThermalPath = "CellBasedThermalResistance"; 


%% Visualization
disp(batteryPack.NumModels);
f = uifigure(Color="w");
tl = tiledlayout(1,2,"Parent",f,"TileSpacing","Compact");
nexttile(tl)
batteryPackChart = BatteryChart(Parent = tl, Battery = batteryPack);
nexttile(tl)
batteryPackChart = BatteryChart(Parent = tl, Battery = batteryPack, SimulationStrategyVisible = "On");


%% Simscape model build
buildBattery(batteryPack,LibraryName ="eD2batterypack1RC",MaskParameters="VariableNames",MaskInitialTargets="VariableNames");

