03-BCC Sweep

03-BCC Sweep

"""Example script demonstrating running multiple jobs in parallel.

Note the Metafold API applies rate limiting to job dispatches so running jobs in
parallel will give limited performance improvements.

Usage:
    python examples/surface_lattice_metrics_parallel.py -t <token> -p <project> -n <procs>

For more details on the available job types please refer to the Metafold REST API
documentation.
"""
import pandas as pd
import numpy as np
import os
import csv
from numpy.typing import ArrayLike
from pprint import pprint
from scipy.spatial.transform import Rotation as R
from metafold import MetafoldClient
from metafold.func_types import JSONEvaluator, FuncType, Vec3f, Mat4f
from metafold.func import *


project_id = 5701
access_token = ""
client=MetafoldClient(access_token,project_id)




source = GenerateSamplePoints(       
                              {"offset": [
                                        -15,
                                        -15,
                                        0
                                        ],
                                        "size": [
                                        30,
                                        30,
                                        30
                                        ],
                                        "resolution": [
                                        128,
                                        128,
                                        128
                                        ]
                              }
                            )

section_radius = [0.1, 0.125, 0.15]
scale = [3, 6, 9]
bcc_params= {
            "lattice_data" : {
                      "edges": [
                            [
                            0,
                            7
                            ],
                            [
                            4,
                            7
                            ],
                            [
                            1,
                            7
                            ],
                            [
                            2,
                            7
                            ],
                            [
                            5,
                            7
                            ],
                            [
                            6,
                            7
                            ],
                            [
                            3,
                            7
                            ],
                            [
                            7,
                            8
                            ]
                        ],
                        "nodes": [
                            [
                            0,
                            0,
                            0
                            ],
                            [
                            0,
                            1,
                            0
                            ],
                            [
                            1,
                            0,
                            0
                            ],
                            [
                            1,
                            1,
                            0
                            ],
                            [
                            0,
                            0,
                            1
                            ],
                            [
                            0,
                            1,
                            1
                            ],
                            [
                            1,
                            0,
                            1
                            ],
                            [
                            0.5,
                            0.5,
                            0.5
                            ],
                            [
                            1,
                            1,
                            1
                            ]
                        ]
            },
        }
densities=[]
for section_radius_value in section_radius:
    for scale_value in scale:
        bcc_params["section_radius"]= section_radius_value
        bcc_params["scale"]= [scale_value, scale_value, scale_value]
        shape_func= Threshold(
                        Redistance(
                                    SampleLattice(source, parameters=bcc_params)             
                        ),
                        {
                            "width": 0.03,
                        },
                    )      

        evaluator = JSONEvaluator(source) 
        shape_func(evaluator)
        graph_json=evaluator.json()
        job= client.jobs.run("evaluate_metrics", {"graph": graph_json, "point_source": 0})
        pprint((scale_value, section_radius_value, job.meta["relative_density"]))
        densities.append((scale_value, section_radius_value, job.meta["relative_density"]))
        export_job = client.jobs.run("export_triangle_mesh", {
                "graph": graph_json,
                "point_source": 0,
            })
        export_asset = export_job.assets[0].id
        client.assets.download_file(export_asset, f"bcc_{scale_value}_{section_radius_value * 1000}.stl")

# Convert to DataFrame
df = pd.DataFrame(densities, columns=["section_radius_value", "scale_value", "relative_density"])
df.to_csv('out.csv', index=False)