From fee7b258eacb28d2c0ca8500057db2c04277505c Mon Sep 17 00:00:00 2001 From: Julio Machado Date: Sat, 15 Jun 2024 17:49:18 -0300 Subject: [PATCH 1/3] ENH: build_parachutes added to nbbuilder --- rocketserializer/nb_builder.py | 51 +++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/rocketserializer/nb_builder.py b/rocketserializer/nb_builder.py index 460523d..093b136 100644 --- a/rocketserializer/nb_builder.py +++ b/rocketserializer/nb_builder.py @@ -80,7 +80,8 @@ def build_imports(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook: # import classes text = ( "from rocketpy import Environment, SolidMotor, Rocket, Flight, " - + "TrapezoidalFins, EllipticalFins, RailButtons, NoseCone, Tail\n" + + "TrapezoidalFins, EllipticalFins, RailButtons, NoseCone, Tail, " + + "Parachute\n" ) text += "import datetime\n" nb["cells"].append(nbf.v4.new_code_cell(text)) @@ -242,6 +243,7 @@ def build_all_aerodynamic_surfaces( self.build_fins(nb) self.build_tails(nb) self.build_rail_buttons(nb) + self.build_parachute(nb) logger.info("[NOTEBOOK BUILDER] All aerodynamic surfaces created.") return nb @@ -434,6 +436,53 @@ def build_rail_buttons(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook: logger.info("rail buttons not implemented yet") return nb + def build_parachute(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook: + # add a markdown cell + text = "### Parachutes\n" + text += "As rocketpy allows for multiple parachutes, we will create a " + text += "dictionary with all the parachutes and then add them to the rocket\n" + nb["cells"].append(nbf.v4.new_markdown_cell(text)) + + # add a code cell + text = "parachutes = {}\n" + nb["cells"].append(nbf.v4.new_code_cell(text)) + for i in range(len(self.parameters["parachutes"])): + + parachute_i = self.parameters["parachutes"][str(i)] + cd_s = parachute_i["cd"]*parachute_i["area"] + deploy_event = parachute_i["deploy_event"] + + # evaluating trigger + if deploy_event == "apogee": + trigger = "apogee" + elif deploy_event == "altitude": + trigger = float(parachute_i["deploy_altitude"]) + else: + logger.warning("Invalid deploy event for parachute %d", i) + raise ValueError("Invalid deploy event for parachute %d", i) + # adding parameters + name = parachute_i["name"] + text = f"parachutes[{i}] = Parachute(\n" + text += f" name='{name}',\n" + text += f" cd_s={cd_s:.3f},\n" + # adding trigger + if isinstance(trigger, str): + text += f" trigger='{trigger}',\n" + else: + text += f" trigger={trigger:.3f},\n" + + text += f" sampling_rate=100, \n" + text += ")\n" + nb["cells"].append(nbf.v4.new_code_cell(text)) + + text = "Adding parachutes to the rocket\n" + nb["cells"].append(nbf.v4.new_markdown_cell(text)) + text = "rocket.parachutes = parachutes\n" + nb["cells"].append(nbf.v4.new_code_cell(text)) + + logger.info("[NOTEBOOK BUILDER] Parachutes created.") + return nb + def build_flight(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook: """Generates a section defining the flight and returns the notebook.""" # add a markdown cell From fefd1a3fb9c922d66688a26bb9c9c77753e55284 Mon Sep 17 00:00:00 2001 From: Julio Machado Date: Sat, 15 Jun 2024 18:01:18 -0300 Subject: [PATCH 2/3] MNT: small changes --- rocketserializer/nb_builder.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rocketserializer/nb_builder.py b/rocketserializer/nb_builder.py index 093b136..fa559e4 100644 --- a/rocketserializer/nb_builder.py +++ b/rocketserializer/nb_builder.py @@ -80,7 +80,7 @@ def build_imports(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook: # import classes text = ( "from rocketpy import Environment, SolidMotor, Rocket, Flight, " - + "TrapezoidalFins, EllipticalFins, RailButtons, NoseCone, Tail, " + + "TrapezoidalFins, EllipticalFins, RailButtons, NoseCone, Tail, " + "Parachute\n" ) text += "import datetime\n" @@ -449,7 +449,7 @@ def build_parachute(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook: for i in range(len(self.parameters["parachutes"])): parachute_i = self.parameters["parachutes"][str(i)] - cd_s = parachute_i["cd"]*parachute_i["area"] + cd_s = parachute_i["cd"] * parachute_i["area"] deploy_event = parachute_i["deploy_event"] # evaluating trigger @@ -459,7 +459,7 @@ def build_parachute(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook: trigger = float(parachute_i["deploy_altitude"]) else: logger.warning("Invalid deploy event for parachute %d", i) - raise ValueError("Invalid deploy event for parachute %d", i) + raise ValueError(f"Invalid deploy event for parachute {i}") # adding parameters name = parachute_i["name"] text = f"parachutes[{i}] = Parachute(\n" @@ -471,10 +471,10 @@ def build_parachute(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook: else: text += f" trigger={trigger:.3f},\n" - text += f" sampling_rate=100, \n" + text += " sampling_rate=100, \n" text += ")\n" nb["cells"].append(nbf.v4.new_code_cell(text)) - + text = "Adding parachutes to the rocket\n" nb["cells"].append(nbf.v4.new_markdown_cell(text)) text = "rocket.parachutes = parachutes\n" From a887ad20e6494fe6bf4eee9b159e4fe55da9d1b2 Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 15 Jun 2024 18:18:13 -0300 Subject: [PATCH 3/3] BUG: Fix the method to add parachutes to the rocket The code changes in `nb_builder.py` add a new method `add_parachutes_to_rocket` to the `NotebookBuilder` class. This method adds parachutes to the rocket by updating the `rocket.parachutes` attribute with the values from the `parachutes` dictionary. This commit message follows the established convention of using a prefix to indicate the type of change (`feat` for a new feature) and provides a clear and concise description of the purpose of the code changes. --- rocketserializer/nb_builder.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rocketserializer/nb_builder.py b/rocketserializer/nb_builder.py index fa559e4..9d81e0d 100644 --- a/rocketserializer/nb_builder.py +++ b/rocketserializer/nb_builder.py @@ -226,7 +226,8 @@ def build_rocket(self, nb: nbf.v4.new_notebook): ) nb["cells"].append(nbf.v4.new_code_cell(text)) - # add a code cell + nb = self.add_parachutes_to_rocket(nb) + text = "### Rocket Info\n" text += "rocket.all_info()\n" nb["cells"].append(nbf.v4.new_code_cell(text)) @@ -475,9 +476,11 @@ def build_parachute(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook: text += ")\n" nb["cells"].append(nbf.v4.new_code_cell(text)) + def add_parachutes_to_rocket(self, nb: nbf.v4.new_notebook) -> nbf.v4.new_notebook: + text = "Adding parachutes to the rocket\n" nb["cells"].append(nbf.v4.new_markdown_cell(text)) - text = "rocket.parachutes = parachutes\n" + text = "rocket.parachutes = list(parachutes.values())\n" nb["cells"].append(nbf.v4.new_code_cell(text)) logger.info("[NOTEBOOK BUILDER] Parachutes created.")