MCPcopy Create free account
hub / github.com/SZAILAB/MaterialDFT-Demo / CppCoreBackend

Class CppCoreBackend

src/materialdft/backends/cpp_core.py:184–1804  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

182
183
184class CppCoreBackend:
185 name = "cpp_core"
186
187 def __init__(
188 self,
189 *,
190 default_potcar_root: Path | None = None,
191 default_potcar_set: str | None = None,
192 ):
193 self._module: Any | None = None
194 self._import_error: Exception | None = None
195 self.default_potcar_root = default_potcar_root.resolve() if default_potcar_root is not None else None
196 self.default_potcar_set = default_potcar_set
197 try:
198 import materialdft_cpp_core as cpp_core
199
200 self._module = cpp_core
201 except Exception as exc: # pragma: no cover - import failure path is exercised via runtime backend selection
202 self._import_error = exc
203
204 def _ensure_module(self) -> Any:
205 if self._module is None:
206 msg = "cpp_core backend is unavailable: pybind11 module import failed"
207 if self._import_error is not None:
208 msg = f"{msg}: {self._import_error}"
209 raise BackendExecutionError(msg, "RUNTIME_ERROR", "PREPARE")
210 return self._module
211
212 def _input_obj(self, spec: SimulationSpec) -> dict[str, Any]:
213 if isinstance(spec.input, dict):
214 return spec.input
215 return {}
216
217 def _runtime_policy(self, spec: SimulationSpec) -> dict[str, Any]:
218 if isinstance(spec.runtime_policy, dict):
219 return spec.runtime_policy
220 return {}
221
222 def _resources(self, spec: SimulationSpec) -> dict[str, Any]:
223 if isinstance(spec.resources, dict):
224 return spec.resources
225 return {}
226
227 def _dump_cpp_core_payload_enabled(self, spec: SimulationSpec) -> bool:
228 runtime_policy = self._runtime_policy(spec)
229 if "dump_cpp_core_payload" in runtime_policy:
230 return _as_bool(runtime_policy.get("dump_cpp_core_payload"), False)
231 return _as_bool(os.environ.get("MaterialDFT_DUMP_CPP_CORE_PAYLOAD"), False)
232
233 def _gate_min_nelm(self, spec: SimulationSpec) -> int | None:
234 gate_profile = self._runtime_policy(spec).get("gate_profile")
235 if not isinstance(gate_profile, dict):
236 return None
237 min_nelm = _as_int(gate_profile.get("min_nelm"), 0)
238 if min_nelm <= 0:
239 return None
240 return min_nelm
241

Calls

no outgoing calls