KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
generate_factory.py
Go to the documentation of this file.
1#!/bin/python
2
3import json
4import glob
5import re
6import sys
7import os
8
9root=sys.argv[1]
10config_file = sys.argv[2]
11target = sys.argv[3]
12factoryfile = sys.argv[4]
13
14if os.path.exists(root+'/config.json'):
15 config_file = root + '/config.json'
16
17# print(root, config_file, target, factoryfile)
18
19with open(config_file, 'r', encoding='utf-8') as load_f:
20 global data
21 data = json.load(load_f)
22
23def creat_object_codes(header, name1, name2, name3):
24 _txt = ''.join(open(header.replace('..', root)).readlines())
25 codes = ''
26 for i in re.findall('class (.*?) : public', _txt, re.S):
27 codes += '''
28 } else if (%s == %s::name()) {
29 %s = new %s(%s);'''%(name1, i, name2, i, name3)
30 return codes
31
32objs = []
33# print modelfactory.cpp
34if target == 'models':
35 for i in data['models']:
36 list1 = glob.glob(root+'/'+i)
37 for j in list1:
38 objs += [ j.replace(root, "..") ]
39
40 txt=''
41 txt+='#include "modelfactory.h"\n\n'
42 txt+='#include <string>\n\n'
43 for i in objs:
44 txt += '#include "%s.h"\n'%i[:-4]
45
46 txt += '''
47Model* init_model(const std::string& model_name, const Param& parm) {
48 Model* mymodel = NULL;
49 if (false) { // just do nothing (placeholder)'''
50
51 for i in objs:
52 txt += creat_object_codes(i[:-4]+'.h', 'model_name', 'mymodel', 'parm')
53
54 txt += '''
55 } else {
56 LOG(FATAL) << "Cannot parse <forcefield> " << model_name << std::endl;
57 }
58 return mymodel;
59}
60'''
61 f = open(factoryfile, 'w')
62 f.write(txt)
63 f.close()
64
65 for i in objs:
66 print(i.replace('../models/', ''))
67
68elif target == 'solvers':
69 for i in data['solvers']:
70 list1 = glob.glob(root+'/'+i)
71 for j in list1:
72 objs += [ j.replace(root, "..") ]
73
74 txt=''
75 txt+='#include "solverfactory.h"\n\n'
76 txt+='#include <string>\n\n'
77 for i in objs:
78 txt += '#include "%s.h"\n'%i[:-4]
79
80 txt += '''
81Solver* init_solver(const std::string& solver_name, const Param& parm, Model* pM) {
82 Solver* mysolver = NULL;
83 if (false) { // just do nothing (placeholder)'''
84
85 for i in objs:
86 txt += creat_object_codes(i[:-4]+'.h', 'solver_name', 'mysolver', 'parm, pM')
87 txt += '''
88 } else {
89 LOG(FATAL) << "Cannot parse method name: " << solver_name << std::endl;
90 }
91 return mysolver;
92}
93'''
94 f = open(factoryfile, 'w')
95 f.write(txt)
96 f.close()
97
98 for i in objs:
99 print(i.replace('../solvers/', '') + ';')
100
101
creat_object_codes(header, name1, name2, name3)