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

21 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-12-25 02:54 +0100

1from fiqus.pro_templates import combined 

2from jinja2 import Environment, FileSystemLoader 

3import numpy as np 

4import os 

5 

6 

7class ASS_PRO: 

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

9 """ 

10 Class for generating pro templates 

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

12 :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. 

13 """ 

14 self.file_base_path = file_base_path 

15 if not naming_conv: 

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

17 else: 

18 self.naming_conv = naming_conv 

19 

20 def assemble_combined_pro(self, template, dm, rm=None, mf=None, ps=None, ed=None, mp=None, rm_EM=None, rm_TH=None, rc=None, 

21 BH_curves_path: str = '', external_templates_paths: list = None): 

22 """ 

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

24 :param external_templates_paths: list of paths to external templates directories 

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

26 :param template: .pro template file name 

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

28 :param rm_EM: regions model data structure for electromagnetics (yaml loaded to regions data model) 

29 :param rm_TH: regions model data structure for thermal (yaml loaded to regions data model) 

30 :param rc: regions coordinates data structure 

31 :param dm: data model structure 

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

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

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

35 :param mp: material properties data structure 

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

37 """ 

38 external_templates_paths = external_templates_paths if external_templates_paths else [] 

39 loader = FileSystemLoader([os.path.dirname(combined.__file__)] + external_templates_paths) 

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

41 trim_blocks=True, lstrip_blocks=True, extensions=['jinja2.ext.do']) 

42 env.globals.update(set=set, str=str, int=int, float=float, zip=zip, enumerate=enumerate, list=list, 

43 len=len, isinstance=isinstance, 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. 

44 pro_template = env.get_template(template) 

45 output_from_parsed_template = pro_template.render(BHcurves=BH_curves_path, dm=dm, rm=rm, mf=mf, nc=self.naming_conv, 

46 ps=ps, ed=ed, mp=mp, rm_EM=rm_EM, rm_TH=rm_TH, rc=rc) 

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

48 tf.write(output_from_parsed_template) 

49 

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

51 """ 

52 This function is not developed yet. 

53 """ 

54 pass