Coverage for fiqus/pro_assemblers/ProAssembler.py: 95%

19 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2024-05-20 03:24 +0200

1from fiqus.pro_templates import combined 

2from jinja2 import Environment, FileSystemLoader 

3import numpy as np 

4 

5 

6class ASS_PRO: 

7 def __init__(self, file_base_path, naming_conv=None): 

8 """ 

9 Class for generating pro templates 

10 :param file_base_path: this is a full path to a folder with the model and includes model name, but without extension 

11 :param naming_conv: a dictionary with naming convention for regions in .pro file. Optional parameter, as a default below is used, but it could be overwritten if needed. 

12 """ 

13 self.file_base_path = file_base_path 

14 if not naming_conv: 

15 self.naming_conv = {'omega': 'Omega', 'terms': 'Terms', 'bd': 'Bd', 'cond': '_c', 'powered': '_p', 'induced': '_i', 'air': '_a', 'line': 'Line'} 

16 else: 

17 self.naming_conv = naming_conv 

18 

19 def assemble_combined_pro(self, template, rm, dm, rc=None, mf=None, ps=None, ed=None, BH_curves_path: str = ''): 

20 """ 

21 Generates model .pro file from .pro template and regions model (rm) 

22 :param BH_curves_path: path of the BH curves pro file 

23 :param template: .pro template file name 

24 :param rm: regions model data structure (yaml loaded to regions data model) 

25 :param rc: regions coordinates data structure 

26 :param dm: data model structure 

27 :param mf: full path to mesh file to be used in solution 

28 :param ps: previous solution folder (this is used by CWS for co-simulation run) 

29 :param ed: excitation dictionary with lists (e.g. times and currents) to be used in a pro file (e.g. transient simulation) 

30 :return: None. Generates .pro file and saves it on disk in the model folder under model_name.pro 

31 """ 

32 loader = FileSystemLoader(combined.__path__) 

33 env = Environment(loader=loader, variable_start_string='<<', variable_end_string='>>', 

34 trim_blocks=True, lstrip_blocks=True) 

35 env.globals.update(set=set, str=str, int=int, zip=zip, enumerate=enumerate, list=list, len=len, arange=np.arange, Pi=np.pi) # this is to pass python zip function to the template, as normally it is not available. It should work for passing any python function that is not available in .pro template. 

36 pro_template = env.get_template(template) 

37 output_from_parsed_template = pro_template.render(BHcurves=BH_curves_path, rm=rm, rc=rc, dm=dm, mf=mf, nc=self.naming_conv, ps=ps, ed=ed) 

38 with open(f"{self.file_base_path}.pro", "w") as tf: 

39 tf.write(output_from_parsed_template) 

40 

41 def assemble_separate_pro(self, template, rm, dm, BH_curves_path: str = ''): 

42 """ 

43 This function is not developed yet. 

44 """ 

45 pass