Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CPACSUpdater module #271

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 57 additions & 41 deletions ceasiompy/CPACSUpdater/cpacsupdater.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,38 @@

TODO:

* This module is still a bit tricky to use, it will be simplified in the future
*

"""

# ==============================================================================
# =================================================================================================
# IMPORTS
# ==============================================================================
# =================================================================================================

from pathlib import Path
from cpacspy.cpacspy import CPACS
from ceasiompy.utils.ceasiomlogger import get_logger
from cpacspy.cpacsfunctions import get_tigl_configuration, open_tigl, open_tixi

from ceasiompy.utils.moduleinterfaces import (
check_cpacs_input_requirements,
get_toolinput_file_path,
get_tooloutput_file_path,
)

log = get_logger()

MODULE_DIR = Path(__file__).parent
MODULE_NAME = MODULE_DIR.name


# ==============================================================================
# =================================================================================================
# CLASSES
# ==============================================================================
# =================================================================================================


# ==============================================================================
# =================================================================================================
# FUNCTIONS
# ==============================================================================
# =================================================================================================


def update_cpacs_file(cpacs_path, cpacs_out_path, optim_var_dict):
Expand All @@ -53,58 +63,64 @@ def update_cpacs_file(cpacs_path, cpacs_out_path, optim_var_dict):

"""

log.info("----- Start of CPACSUpdater -----")
cpacs = CPACS(cpacs_path)
log.info(f"{cpacs_path} will be updated.")

tixi = open_tixi(cpacs_path)
tigl = open_tigl(tixi)

# Object seems to be unused, but are use in "eval" function
aircraft = get_tigl_configuration(tigl)
wings = aircraft.get_wings()
fuselage = aircraft.get_fuselages().get_fuselage(1)
# DO NOT REMOVE, those variables are used in "eval" function
wings = cpacs.aircraft.configuration.get_wings()
fuselage = cpacs.aircraft.configuration.get_fuselages().get_fuselage(1)

# Perform update of all the variable contained in 'optim_var_dict'
for name, variables in optim_var_dict.items():

# Unpack the variables
val_type, listval, _, _, getcommand, setcommand = variables
val_type, list_val, _, _, get_command, set_command = variables

if val_type == "des" and listval[0] not in ["-", "True", "False"]:
if val_type == "des" and list_val[0] not in ["-", "True", "False"]:

if setcommand not in ["-", ""]:
if set_command in ["-", ""]:

# Define variable (var1,var2,..)
locals()[str(name)] = listval[-1]
# Update value directly in the CPACS file
xpath = get_command
cpacs.tixi.updateTextElement(xpath, str(list_val[-1]))

# Update value by using tigl configuration
if ";" in setcommand: # if more than one command on the line
command_split = setcommand.split(";")
for setcommand in command_split:
eval(setcommand)
else:
eval(setcommand)
else:

# Update value directly in the CPACS file
xpath = getcommand
tixi.updateTextElement(xpath, str(listval[-1]))
# Define variable (var1,var2,..)
locals()[str(name)] = list_val[-1]

if ";" in set_command: # if more than one command on the line
commands = set_command.split(";")
else:
commands = [set_command]

aircraft.write_cpacs(aircraft.get_uid())
tigl.close()
tixi.save(str(cpacs_out_path))
# Update value by using tigl configuration
for command in commands:
eval(command)

log.info(f"{cpacs_out_path} has been saved.")
log.info("----- Start of CPACSUpdater -----")
cpacs.save_cpacs(str(cpacs_out_path))


# ==============================================================================
# =================================================================================================
# MAIN
# ==============================================================================
# =================================================================================================


def main(cpacs_path, cpacs_out_path):

log.info("----- Start of " + MODULE_NAME + " -----")

check_cpacs_input_requirements(cpacs_path)
update_cpacs_file(cpacs_path, cpacs_out_path)

log.info("----- End of " + MODULE_NAME + " -----")


if __name__ == "__main__":

print("Nothing to execute!")
cpacs_path = get_toolinput_file_path(MODULE_NAME)
cpacs_out_path = get_tooloutput_file_path(MODULE_NAME)

main(cpacs_path, cpacs_out_path)


# Other functions which could be useful
# help(aircraft)
Expand Down