Commit 5f7ff3a5 authored by Greenwood, Scott's avatar Greenwood, Scott
Browse files

[fix][feature] removed the need for specifying SourceName in input...

[fix][feature] removed the need for specifying SourceName in input specifcation. Location is now tied to project and autodetects the default of the model. If sourcename is provided, it will use redeclare if it doesn't match the default.
parent e8ddc283
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -123,7 +123,7 @@ class AutoCSM:
    @project_path.setter
    def project_path(self, path: str) -> None:
        """Sets the project path, ensuring it's not 'package.mo'."""
        temp = pathlib.Path(path)
        temp = pathlib.Path(path).resolve()
        if temp.name == "package.mo":
            temp = temp.parent
        self._project_path = temp.as_posix()
+21 −7
Original line number Diff line number Diff line
@@ -47,12 +47,26 @@ def structure_line(data, uniform):
def sources_line(data, project_path, base_path, model_class_path, key_path, uniform):
    temp = []
    temp_path = pathlib.Path(project_path).parts[:-1]
    source_path_list = list(temp_path) + model_class_path.split('.')[0:-2] + ['Sources'] + [data['SourceName'] + '.mo']
    source_path = os.path.join(*source_path_list)
    
    #TODO: Not sure why this line is here... should remove once verified not important
    # if source_path == r'E:\ExaDigiT\datacenterCoolingModel\ORNLSupercomputing\package\mo\Sources\NULL.mo':
    #     pass
    model_path_list = list(temp_path) + model_class_path.split('.')[0:-2] + ['Models'] + [data['ClassName'] + '.mo']
    model_path = os.path.join(*model_path_list)
    
    # Find the default by searching for "redeclare replaceable Sources.* sources"
    default_source_class = modelica_parse_files.extract_default_class_from_model(model_path, 'Sources', 'source')[0]  
    
    # If 'SourceName' is not in in the input file and is not the default, redeclare
    if 'SourceName' in data.keys() and default_source_class != data['SourceName']:
        # Adds redeclaration if a non-default source class is specified
        model_class_path = '.'.join([base_path, 'Sources', data['SourceName']])
        line = f'redeclare {model_class_path} '
    else:
        line = ''
    
    if 'SourceName' in data.keys():
       default_source_class = data['SourceName'] 
       
    source_path_list = list(temp_path) + model_class_path.split('.')[0:-2] + ['Sources'] + [default_source_class + '.mo']
    source_path = os.path.join(*source_path_list)
        
    inputs = modelica_parse_files.get_variable_by_type(source_path, 'input')
    if inputs:
@@ -87,7 +101,7 @@ def sources_line(data, project_path, base_path, model_class_path, key_path, unif
                temp_struct_filled = nested_structure_utils.replace_struct_values(temp_struct, [var[3]]*nested_structure_utils.count_leaf_nodes(temp_struct))
                temp_s.append(f'{var[2]}={temp_struct_filled}'.replace('[', '{').replace(']', '}').replace("'",""))
            temp = ','.join(temp_s)
    line = f'sources({temp if temp else ""})\n'
    line += f'sources({temp if temp else ""})\n'
    return line

def create_nested_lines(data, project_path, base_path, uniform=[True,True], lines=None, is_top_system=True, key_path=None, level=0):
+10 −0
Original line number Diff line number Diff line
@@ -33,6 +33,16 @@ def get_variable_by_type(file_path, dtype='parameter'):
    parameters = extract_variable(input_string, dtype)
    return parameters

def extract_default_class_from_model(file_path, folder='Sources', instance='sources'):
    # Define the regex pattern
    pattern = r'redeclare\s+replaceable\s+{}.(\w+)\s+{}'.format(folder, instance)

    with open(file_path, 'r') as file:
        content = file.read()

    matches = re.findall(pattern, content)
    return matches

def apply_instance_naming_conventions(name):
    if name.isupper():
        # Abbreviations are all lowercase for the the instance (e.g., CDU -> cdu)
+0 −8
Original line number Diff line number Diff line
@@ -3,28 +3,24 @@
	"InstanceName":"simulator",
	"Structure":{"n":1},
	"ClassName":"v0",
	"SourceName":"NULL",
    "Systems": [
        {
            "Name": "Datacenter",
			"InstanceName":"datacenter",
			"Structure":{"n":1},
			"ClassName":"v0",
			"SourceName":"NULL",
            "Systems":[
                {
                    "Name": "CoolingBlock",
					"InstanceName":"computeBlock",
					"Structure":{"n":10},
					"ClassName":"v0",
					"SourceName":"NULL",
                    "Systems":[
                        {
                            "Name": "CDU",
							"InstanceName":"cdu",
							"ClassName":"v0",
							"Structure":{"n":1},
							"SourceName":"NULL",
                            "Systems":[{}]
                        },
                        {
@@ -32,7 +28,6 @@
							"Structure":{"n":3},
							"InstanceName":"cabinet",
							"ClassName":"v0",
							"SourceName":"v0",
                            "Systems":[{}]
                        }]
                }]
@@ -42,14 +37,12 @@
			"InstanceName":"centralEnergyPlant",
			"Structure":{"n":1},
			"ClassName":"v0",
			"SourceName":"NULL",
            "Systems":[
                {
                    "Name": "IntermediateLoop",
					"InstanceName":"intermediateLoop",
					"Structure":{"n":1},
					"ClassName":"v0",
					"SourceName":"NULL",
                    "Systems":[{}]
                },
                {
@@ -57,7 +50,6 @@
					"InstanceName":"coolingTowerLoop",
					"Structure":{"n":1},
					"ClassName":"v0",
					"SourceName":"v0",
                    "Systems":[{}]
                }]
        }]
+0 −8
Original line number Diff line number Diff line
@@ -3,28 +3,24 @@
	"InstanceName":"simulator",
	"Structure":{"n":2},
	"ClassName":"v0",
	"SourceName":"NULL",
    "Systems": [
        {
            "Name": "Datacenter",
			"InstanceName":"datacenter",
			"Structure":{"n":2},
			"ClassName":"v0",
			"SourceName":"NULL",
            "Systems":[
                {
                    "Name": "CoolingBlock",
					"InstanceName":"computeBlock",
					"Structure":{"n":2},
					"ClassName":"v0",
					"SourceName":"NULL",
                    "Systems":[
                        {
                            "Name": "CDU",
							"InstanceName":"cdu",
							"ClassName":"v0",
							"Structure":{"n":1},
							"SourceName":"NULL",
                            "Systems":[{}]
                        },
                        {
@@ -32,7 +28,6 @@
							"Structure":{"n":3},
							"InstanceName":"cabinet",
							"ClassName":"v0",
							"SourceName":"v0",
                            "Systems":[{}]
                        }]
                }]
@@ -42,14 +37,12 @@
			"InstanceName":"centralEnergyPlant",
			"Structure":{"n":1},
			"ClassName":"v0",
			"SourceName":"NULL",
            "Systems":[
                {
                    "Name": "IntermediateLoop",
					"InstanceName":"intermediateLoop",
					"Structure":{"n":1},
					"ClassName":"v0",
					"SourceName":"NULL",
                    "Systems":[{}]
                },
                {
@@ -57,7 +50,6 @@
					"InstanceName":"coolingTowerLoop",
					"Structure":{"n":1},
					"ClassName":"v0",
					"SourceName":"v0",
                    "Systems":[{}]
                }]
        }]
Loading