Skip to content

Commit db097e9

Browse files
committed
update json
1 parent 5d82e17 commit db097e9

3 files changed

Lines changed: 141 additions & 30 deletions

File tree

polyfempy/autoclass.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
from dataclasses import make_dataclass
2+
import typing
3+
import dataclasses
4+
import json
5+
import polyfempy
6+
7+
def getter(contain, key):
8+
if key not in contain:
9+
return []
10+
return contain[key]
11+
12+
def parse_tree(rules, mark_required = False):
13+
tree = dict()
14+
for r in rules:
15+
pt = r['pointer']
16+
pt = pt.replace('/*','')
17+
18+
path = pt.split('/')
19+
if pt == '/':
20+
path = ['']
21+
22+
subtree = tree
23+
for k in path:
24+
if k not in subtree:
25+
subtree[k] = dict()
26+
subtree = subtree[k]
27+
req, opt = (getter(r,'required'), getter(r, 'optional'))
28+
for k in req:
29+
if k not in subtree:
30+
subtree[k] = {}
31+
if mark_required:
32+
subtree[k]['R'] = '0'
33+
for k in opt:
34+
if k not in subtree:
35+
subtree[k] = {}
36+
return tree
37+
38+
39+
def snake_to_camel(name):
40+
if name == '':
41+
return 'PolyFEM'
42+
return ''.join([n.capitalize() for n in name.split('_')])
43+
44+
def print_script():
45+
# deprecated to the make_dataclasses interface
46+
key = ''
47+
node = tree[key]
48+
indent = 4
49+
repre = 'from dataclasses import dataclass\nimport typing\n'
50+
51+
def rec(key, val, h):
52+
global repre
53+
if key == 'lambda':
54+
return
55+
if key !='':
56+
repre += (' '*h*indent + f'{key} : typing.Any = None\n')
57+
58+
if len(val) == 0:
59+
return
60+
classname = snake_to_camel(key)
61+
repre += (' '*h*indent + f'@dataclass\n')
62+
repre += (' '*h*indent + f'class {classname}:\n')
63+
for k,v in (val.items()):
64+
rec(k,v, h+1)
65+
66+
rec(key, node, 0)
67+
68+
repre += """
69+
if __name__ == '__main__':
70+
print(PolyFEM(geometry='mesh.obj'))
71+
"""
72+
73+
with open('test.py', 'w') as fp:
74+
fp.write(repre)
75+
76+
77+
78+
def recursive_data_class_builder(node, name):
79+
fields = []
80+
namespace = dict()
81+
for k, v in node.items():
82+
if k == 'lambda':
83+
continue
84+
fields.append((k,typing.Any,None))
85+
if isinstance(v, dict) and len(v) != 0:
86+
name0 = snake_to_camel(k)
87+
namespace[name0] = recursive_data_class_builder(v, name0)
88+
return make_dataclass(name, fields=fields, namespace = namespace)
89+
90+
91+
def prune_none_entry(node):
92+
if not isinstance(node, dict):
93+
if isinstance(node, list):
94+
for v in node:
95+
prune_none_entry(v)
96+
else:
97+
for k, v in list(node.items()):
98+
prune_none_entry(v)
99+
if v is None or (hasattr(v, '__len__') and len(v) == 0):
100+
node.pop(k)
101+
102+
def generate_input_dict(config):
103+
output = dataclasses.asdict(config)
104+
prune_none_entry(output)
105+
return output
106+
107+
108+
with open('data/default_rules.json')as fp:
109+
rules = json.load(fp)
110+
111+
tree = parse_tree(rules)
112+
pf = recursive_data_class_builder(tree[''], '')
113+
114+
geometry = pf.Geometry(mesh='import.obj')
115+
matnu = pf.Materials(id = 0, nu=0.1)
116+
matE = pf.Materials(id = 1,E=10)
117+
118+
config = pf(geometry = geometry, materials = [matnu, matE])
119+
120+
def solve(**kwargs):
121+
d = generate_input_dict(config)
122+
polyfempy.solve(**d)
123+
124+
if __name__ == '__main__':
125+

setup.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ def __init__(self, name, sourcedir=''):
2020

2121
class CMakeBuild(build_ext):
2222
def run(self):
23-
if platform.system() == 'Darwin':
24-
self.build_temp = self.build_temp.replace("build", "build.nosync")
25-
2623
try:
2724
out = subprocess.check_output(['cmake', '--version'])
2825
except OSError:

src/binding.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -342,38 +342,30 @@ PYBIND11_MODULE(polyfempy, m)
342342
"load_mesh_from_settings", [](polyfem::State &s, const bool normalize_mesh, const double vismesh_rel_area, const int n_refs, const double boundary_id_threshold) {
343343
init_globals(s);
344344
// py::scoped_ostream_redirect output;
345-
throw std::runtime_error("Need to convert JSON");
346-
s.args["normalize_mesh"] = normalize_mesh;
347-
s.args["n_refs"] = n_refs;
348-
s.args["boundary_id_threshold"] = boundary_id_threshold;
349-
s.args["vismesh_rel_area"] = vismesh_rel_area;
345+
s.args["geometry"]["advanced"]["normalize_mesh"] = normalize_mesh;
346+
s.args["output"]["paraview"]["vismesh_rel_area"] = vismesh_rel_area;
350347
s.load_mesh(); },
351348
"Loads a mesh from the 'mesh' field of the json and 'bc_tag' if any bc tags", py::arg("normalize_mesh") = bool(false), py::arg("vismesh_rel_area") = double(0.00001), py::arg("n_refs") = int(0), py::arg("boundary_id_threshold") = double(-1))
352349

353350
.def(
354351
"load_mesh_from_path", [](polyfem::State &s, const std::string &path, const bool normalize_mesh, const double vismesh_rel_area, const int n_refs, const double boundary_id_threshold) {
355352
init_globals(s);
356353
// py::scoped_ostream_redirect output;
357-
throw std::runtime_error("Need to convert JSON");
358354
s.args["mesh"] = path;
359-
s.args["normalize_mesh"] = normalize_mesh;
360-
s.args["n_refs"] = n_refs;
361-
s.args["boundary_id_threshold"] = boundary_id_threshold;
362-
s.args["vismesh_rel_area"] = vismesh_rel_area;
355+
s.args["geometry"]["advanced"]["normalize_mesh"] = normalize_mesh;
356+
s.args["output"]["paraview"]["vismesh_rel_area"] = vismesh_rel_area;
363357
s.load_mesh(); },
364358
"Loads a mesh from the path and 'bc_tag' from the json if any bc tags", py::arg("path"), py::arg("normalize_mesh") = bool(false), py::arg("vismesh_rel_area") = double(0.00001), py::arg("n_refs") = int(0), py::arg("boundary_id_threshold") = double(-1))
365359

366360
.def(
367361
"load_mesh_from_path_and_tags", [](polyfem::State &s, const std::string &path, const std::string &bc_tag, const bool normalize_mesh, const double vismesh_rel_area, const int n_refs, const double boundary_id_threshold) {
368362
init_globals(s);
369363
// py::scoped_ostream_redirect output;
370-
throw std::runtime_error("Need to convert JSON");
371364
s.args["mesh"] = path;
372365
s.args["bc_tag"] = bc_tag;
373-
s.args["normalize_mesh"] = normalize_mesh;
366+
s.args["geometry"]["advanced"]["normalize_mesh"] = normalize_mesh;
374367
s.args["n_refs"] = n_refs;
375-
s.args["boundary_id_threshold"] = boundary_id_threshold;
376-
s.args["vismesh_rel_area"] = vismesh_rel_area;
368+
s.args["output"]["paraview"]["vismesh_rel_area"] = vismesh_rel_area;
377369
s.load_mesh(); },
378370
"Loads a mesh and bc_tags from path", py::arg("path"), py::arg("bc_tag_path"), py::arg("normalize_mesh") = bool(false), py::arg("vismesh_rel_area") = double(0.00001), py::arg("n_refs") = int(0), py::arg("boundary_id_threshold") = double(-1))
379371

@@ -388,11 +380,10 @@ PYBIND11_MODULE(polyfempy, m)
388380
s.mesh = std::make_unique<polyfem::mesh::CMesh3D>();
389381
s.mesh->build_from_matrices(V, F);
390382

391-
throw std::runtime_error("Need to convert JSON");
392-
s.args["normalize_mesh"] = normalize_mesh;
393-
s.args["n_refs"] = n_refs;
383+
s.args["geometry"]["advanced"]["normalize_mesh"] = normalize_mesh;
384+
s.args["geometry"]["n_refs"] = n_refs;
394385
s.args["boundary_id_threshold"] = boundary_id_threshold;
395-
s.args["vismesh_rel_area"] = vismesh_rel_area;
386+
s.args["output"]["paraview"]["vismesh_rel_area"] = vismesh_rel_area;
396387

397388
s.load_mesh(); },
398389
"Loads a mesh from vertices and connectivity", py::arg("vertices"), py::arg("connectivity"), py::arg("normalize_mesh") = bool(false), py::arg("vismesh_rel_area") = double(0.00001), py::arg("n_refs") = int(0), py::arg("boundary_id_threshold") = double(-1))
@@ -408,12 +399,11 @@ PYBIND11_MODULE(polyfempy, m)
408399
s.mesh = std::make_unique<polyfem::mesh::CMesh3D>();
409400
s.mesh->build_from_matrices(V, F);
410401
s.mesh->attach_higher_order_nodes(nodes_pos, nodes_indices);
411-
throw std::runtime_error("Need to convert JSON");
412402

413-
s.args["normalize_mesh"] = normalize_mesh;
414-
s.args["n_refs"] = n_refs;
403+
s.args["geometry"]["advanced"]["normalize_mesh"] = normalize_mesh;
404+
s.args["geometry"]["n_refs"] = n_refs;
415405
s.args["boundary_id_threshold"] = boundary_id_threshold;
416-
s.args["vismesh_rel_area"] = vismesh_rel_area;
406+
s.args["output"]["paraview"]["vismesh_rel_area"] = vismesh_rel_area;
417407

418408
s.load_mesh(); },
419409
"Loads an high order mesh from vertices, connectivity, nodes, and node indices mapping element to nodes", py::arg("vertices"), py::arg("connectivity"), py::arg("nodes_pos"), py::arg("nodes_indices"), py::arg("normalize_mesh") = bool(false), py::arg("vismesh_rel_area") = double(0.00001), py::arg("n_refs") = int(0), py::arg("boundary_id_threshold") = double(-1))
@@ -441,8 +431,7 @@ PYBIND11_MODULE(polyfempy, m)
441431
"set_rhs_from_path", [](polyfem::State &s, std::string &path) {
442432
init_globals(s);
443433
// py::scoped_ostream_redirect output;
444-
throw std::runtime_error("Need to convert JSON");
445-
s.args["rhs_path"] = path; },
434+
throw std::runtime_error("No RHS path"); },
446435
"Loads the rhs from a file", py::arg("path"))
447436
.def(
448437
"set_rhs", [](polyfem::State &s, const Eigen::MatrixXd &rhs) {
@@ -898,7 +887,7 @@ PYBIND11_MODULE(polyfempy, m)
898887
in_args["n_refs"] = n_refs;
899888
if (!problem_name.empty())
900889
in_args["problem"] = problem_name;
901-
in_args["normalize_mesh"] = !skip_normalization;
890+
in_args["geometry"]["advanced"]["normalize_mesh"] = !skip_normalization;
902891
in_args["project_to_psd"] = project_to_psd;
903892

904893
if (!scalar_formulation.empty())
@@ -927,7 +916,7 @@ PYBIND11_MODULE(polyfempy, m)
927916
in_args["solver_type"] = solver;
928917

929918
if (vis_mesh_res > 0)
930-
in_args["vismesh_rel_area"] = vis_mesh_res;
919+
in_args["output"]["paraview"]["vismesh_rel_area"] = vis_mesh_res;
931920

932921
if (export_material_params)
933922
in_args["export"]["material_params"] = true;
@@ -982,7 +971,7 @@ PYBIND11_MODULE(polyfempy, m)
982971
in_args.contains("discr_order") ? int(in_args["discr_order"]) : 1;
983972

984973
if (discr_order == 1 && !in_args.contains("vismesh_rel_area"))
985-
in_args["vismesh_rel_area"] = 1e10;
974+
in_args["output"]["paraview"]["vismesh_rel_area"] = 1e10;
986975

987976
polyfem::State state;
988977
state.init_logger("", log_level, false);

0 commit comments

Comments
 (0)