Skip to content

Commit e68bea1

Browse files
committed
feat(config): introduce profile-based configuration system
1 parent 6f783bd commit e68bea1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2988
-690
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
- A new type of doping box has been introduced, `CustomDoping` which accepts a `SpatialDataArray` to define doping concentration. Unlike in the case where a `SpatialDataArray`, custom doping defined with `CustomDoping` have additive behavior, i.e., one can add other doping on top. This deprecates the `SpatialDataArray` as direct input for `N_a` and `N_d`.
2525
- Non-isothermal Charge simulations are now available. One can now run this type of simulations by using the `SteadyChargeDCAnalysis` as the `analysis_spec` of a `HeatChargeSimulation`. This type of simulations couple the heat equation with the drift-diffusion equations which allow to account for self heating behavior.
2626
- Because non-isothermal Charge simulations are now supported, new models for the effective density of states and bandgap energy have been introduced. These models are the following: `ConstantEffectiveDOS`, `IsotropicEffectiveDOS`, `MultiValleyEffectiveDOS`, `DualValleyEffectiveDOS`.
27+
- Introduced a profile-based configuration manager with TOML persistence and runtime overrides exposed via `tidy3d.config`.
2728

2829
### Changed
2930
- `LayerRefinementSpec` defaults to assuming structures made of different materials are interior-disjoint for more efficient mesh generation.

docs/api/configuration.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Configuration API
2+
=================
3+
4+
.. currentmodule:: tidy3d.config
5+
6+
The objects and helpers below expose the public configuration interface.
7+
8+
Manager and Helpers
9+
-------------------
10+
11+
.. autosummary::
12+
:toctree: _autosummary/
13+
:template: module.rst
14+
15+
ConfigManager
16+
get_manager
17+
reload_config
18+
config
19+
20+
Legacy Compatibility
21+
--------------------
22+
23+
.. autosummary::
24+
:toctree: _autosummary/
25+
:template: module.rst
26+
27+
LegacyConfigWrapper
28+
Env
29+
Environment
30+
EnvironmentConfig
31+
32+
Registration Utilities
33+
----------------------
34+
35+
.. autosummary::
36+
:toctree: _autosummary/
37+
:template: module.rst
38+
39+
register_section
40+
register_plugin
41+
register_handler
42+
get_sections
43+
get_handlers

docs/api/constants.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Tidy3D Configuration
3535
:toctree: _autosummary/
3636
:template: module.rst
3737

38-
tidy3d.config.Tidy3dConfig
38+
tidy3d.config.config
3939

4040
Default Absorber Parameters
4141
----------------------------
@@ -92,4 +92,4 @@ Precision & Comparator Values
9292
tidy3d.constants.fp_eps
9393
tidy3d.constants.pec_val
9494
tidy3d.constants.LARGE_NUMBER
95-
tidy3d.constants.GLANCING_CUTOFF
95+
tidy3d.constants.GLANCING_CUTOFF

docs/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ API |:computer:|
1818
output_data
1919
analytic_beams
2020
utilities
21+
configuration
2122
mesh/index
2223
heat/index
2324
charge/index

docs/configuration/index.rst

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
Configuration Guide |:gear:|
2+
============================
3+
4+
.. highlight:: python
5+
6+
Working with cloud simulations usually requires a handful of settings such as
7+
your API key, the active environment, and any local tweaks you make while
8+
experimenting. The ``tidy3d.config`` module keeps all of this in one place
9+
through a single object called ``config``. This page explains how it behaves,
10+
where values are stored, and how to keep your changes consistent across
11+
sessions.
12+
13+
Getting Started
14+
---------------
15+
16+
Most users only need the following import::
17+
18+
from tidy3d.config import config
19+
20+
You can then read or update sections just like attributes::
21+
22+
# read values
23+
print(config.web.api_endpoint)
24+
print(config.logging.level)
25+
26+
# update values
27+
config.logging.level = "DEBUG"
28+
config.web.timeout = 60
29+
config.save()
30+
31+
The ``save()`` call writes your edits to disk so the same settings load the
32+
next time you import ``tidy3d``.
33+
34+
Where Settings Are Stored
35+
-------------------------
36+
37+
Tidy3D chooses a configuration directory the first time you import the module.
38+
The location depends on your operating system:
39+
40+
.. list-table:: Default configuration directory
41+
:widths: 30 70
42+
:header-rows: 1
43+
44+
* - Platform
45+
- Path
46+
* - macOS / Linux
47+
- ``~/.config/tidy3d``
48+
* - Windows
49+
- ``C:\\Users\\<you>\\.config\\tidy3d``
50+
51+
You can override this by setting the ``TIDY3D_BASE_DIR`` environment variable
52+
*before* importing ``tidy3d``. When it is present, the config files are kept in
53+
``<base>/.tidy3d``. If the chosen location is not writable, Tidy3D falls back to
54+
a temporary directory and warns that the settings will not persist.
55+
56+
Files Inside the Directory
57+
~~~~~~~~~~~~~~~~~~~~~~~~~~
58+
59+
- ``config.toml`` – base settings shared by all profiles.
60+
- ``profiles/<name>.toml`` – optional overrides for custom profiles. Each file
61+
only contains the differences from the base settings.
62+
63+
Priority Order
64+
--------------
65+
66+
Whenever you read ``config.<section>.<field>``, the value comes from the highest
67+
priority source in the list below. Lower entries only apply when the ones above
68+
them do not set a value.
69+
70+
1. Runtime changes you make in the current Python session.
71+
2. Environment variables (``TIDY3D_<SECTION>__<FIELD>``).
72+
3. Profile overrides from ``profiles/<name>.toml``.
73+
4. The base ``config.toml`` file.
74+
5. Built-in profiles (for example ``prod`` and ``dev``) bundled with Tidy3D.
75+
6. Default values defined by the software.
76+
77+
This means environment variables always win over ``config.toml``, and any
78+
attribute you set in code wins over everything else until you discard it or
79+
call ``save()``.
80+
81+
Making Changes That Last
82+
------------------------
83+
84+
Runtime Updates
85+
~~~~~~~~~~~~~~~
86+
87+
Assignments like ``config.logging.level = "INFO"`` apply immediately but only
88+
live in memory. They affect new simulations started in the same interpreter but
89+
disappear when the process exits.
90+
91+
Saving to Disk
92+
~~~~~~~~~~~~~~
93+
94+
Call ``config.save()`` to write the current profile to disk. The method removes
95+
environment-variable overrides automatically so you never persist an API key or
96+
other secret that was loaded from the shell. To store the full set of values,
97+
including defaults, pass ``include_defaults=True``::
98+
99+
config.save(include_defaults=True)
100+
101+
Profiles
102+
--------
103+
104+
Tidy3D ships with built-in profiles such as ``prod`` and ``dev``. Switch between
105+
them with::
106+
107+
config.switch_profile("dev")
108+
109+
To create your own profile, switch to a new name, edit settings, then call
110+
``save()``::
111+
112+
config.switch_profile("customer")
113+
config.web.api_endpoint = "https://example.com"
114+
config.save()
115+
116+
This writes ``profiles/customer.toml`` containing only the adjustments you
117+
made. Use ``config.profiles.list()`` to discover available built-in and user
118+
profiles.
119+
120+
Environment Variables
121+
---------------------
122+
123+
Environment variables let you override individual options without touching any
124+
files. The naming pattern is ``TIDY3D_<SECTION>__<FIELD>``, for example::
125+
126+
export TIDY3D_LOGGING__LEVEL=WARNING
127+
128+
Supported variables take effect the next time you import ``tidy3d``. Remove a
129+
variable or clear the shell environment to restore the lower priority setting.
130+
131+
Plugin Settings
132+
---------------
133+
134+
Plugins can register their own sections so their options appear under
135+
``config.plugins``. A typical plugin exposes a model decorated with
136+
``@register_plugin``::
137+
138+
from tidy3d.config.sections import ConfigSection
139+
from tidy3d.config import register_plugin
140+
141+
@register_plugin("sample")
142+
class SamplePluginConfig(ConfigSection):
143+
enabled: bool = False
144+
threshold: float = 0.5
145+
146+
After the plugin is imported, you can read or modify its settings with::
147+
148+
config.plugins.sample.enabled = True
149+
config.save()
150+
151+
If the plugin registers later in the session, previously saved values are
152+
loaded automatically.
153+
154+
Command Line Helpers
155+
--------------------
156+
157+
Use ``tidy3d configure`` to store your API key in ``config.toml``. The command
158+
creates the directory if it is missing and updates only the ``web`` section.
159+
160+
If you have older files in ``~/.tidy3d``, run ``tidy3d config migrate`` to move
161+
them into the new location described above.
162+
163+
Legacy Access Points
164+
--------------------
165+
166+
Older code paths such as ``tidy3d.config.logging_level`` and ``tidy3d.config.Env``
167+
still work. They emit a ``DeprecationWarning`` each time you use them to help
168+
you transition to the modern interface. See :doc:`migration` for advice on
169+
updating scripts that depend on these names.
170+
171+
Next Steps
172+
----------
173+
174+
- :doc:`migration`
175+
- :doc:`../api/configuration`

docs/configuration/migration.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Upgrading Existing Setups
2+
=========================
3+
4+
This short note highlights the differences you may notice when moving from
5+
earlier versions of Tidy3D to the current configuration manager.
6+
7+
File Locations
8+
--------------
9+
10+
- Previous releases stored settings in ``~/.tidy3d`` on all platforms. The new
11+
manager now prefers the platform-specific paths described in
12+
:doc:`index`.
13+
- Your existing ``~/.tidy3d/config.toml`` is still respected. Run
14+
``tidy3d config migrate`` if you would like to copy it into the new directory.
15+
16+
Environment Switching
17+
---------------------
18+
19+
- The ``Env`` helper remains available. Calls such as ``Env.dev.active()`` now
20+
forward to the new manager and produce a ``DeprecationWarning`` to encourage
21+
the modern API, e.g. ``config.switch_profile("dev")``.
22+
23+
Legacy Attributes
24+
-----------------
25+
26+
- Shorthand properties ``config.logging_level``, ``config.log_suppression``,
27+
and ``config.use_local_subpixel`` still work and set the equivalent fields in
28+
``config.logging`` or ``config.simulation``. Each call raises a warning so
29+
you can update scripts at your own pace.
30+
31+
Working Safely With Existing Scripts
32+
------------------------------------
33+
34+
- Prefer the new attribute paths (``config.logging.level``) in fresh code.
35+
- When editing older notebooks, import ``warnings`` and silence specific
36+
deprecations temporarily if needed::
37+
38+
import warnings
39+
warnings.filterwarnings("ignore", category=DeprecationWarning)
40+
41+
- After updating your scripts, remove the filter so you notice future changes.
42+
43+
Need Help?
44+
----------
45+
46+
Reach out through the `Tidy3D Discussions board <https://github.com/flexcompute/tidy3d/discussions>`_
47+
or contact support@flexcompute.com if you hit any issues while upgrading.
48+

docs/extras/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ You can check whether local subpixel averaging is turned on::
6666

6767
tidy3d.packaging.tidy3d_extras["use_local_subpixel"]
6868

69+
For a broader overview of configuration options and how they are stored, see
70+
:doc:`../configuration/index`.
71+
6972
Licenses
7073
--------
7174

docs/index.rst

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ Get Started
5252
5353
tidy3d configure --apikey=XXX
5454
55-
And enter your API key when prompted.
56-
57-
For more detailed installation instructions, see `this page <./install.html>`_.
55+
For more detailed installation instructions, see `this page <./install.html>`_,
56+
and refer to :doc:`configuration/index` if you would like to fine-tune your
57+
settings.
5858

5959
.. group-tab:: On Windows |:window:|
6060

@@ -78,19 +78,9 @@ Get Started
7878
If you're running into trouble, you may need to manually set the API key directly in the configuration file where Tidy3D looks for it.
7979
You need to place the ``$HOME/.tidy3d/config`` file in your home directory such as ``C:\Users\username\`` (where ``username`` is your username).
8080

81-
The API key must be in a file called ``$HOME/.tidy3d/config`` located in your home directory, with the following contents
82-
83-
.. code-block:: bash
84-
85-
apikey = "XXX"
86-
87-
You can manually set up your file like this, or do it through the command line line:
88-
89-
.. code-block:: bash
90-
91-
echo 'apikey = "XXX"' > ~/.tidy3d/config
92-
93-
Note the quotes around `XXX`.
81+
The ``tidy3d configure`` command stores the API key for you. If you prefer
82+
to manage the file yourself, see :doc:`configuration/index` for the current
83+
location and format on each platform.
9484

9585
.. group-tab:: In the Cloud |:cloud:|
9686

@@ -249,6 +239,7 @@ Contents
249239
:maxdepth: 2
250240

251241
install
242+
configuration/index
252243
lectures/index
253244
notebooks/docs/index
254245
faq/docs/index
@@ -260,5 +251,3 @@ Contents
260251
About our Solver <https://www.flexcompute.com/tidy3d/solver/>
261252

262253

263-
264-

docs/install.rst

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,9 @@ Alternatively, the API key can be set up using the environment variable ``SIMCLO
7878
7979
export SIMCLOUD_APIKEY="XXX"
8080
81-
Finally, one may manually set the API key directly in the configuration file where Tidy3D looks for it.
82-
83-
The API key must be in a file called ``.tidy3d/config`` located in your home directory, with the following contents
84-
85-
.. code-block:: python
86-
87-
apikey = "XXX"
88-
89-
You can manually set up your file like this, or do it through the command line line:
90-
91-
.. code-block:: python
92-
93-
echo 'apikey = "XXX"' > ~/.tidy3d/config
94-
95-
Note the quotes around `XXX`.
96-
97-
Note that Windows users will most likely need to place the ``.tidy3d/config`` file in their ``C:\Users\username\`` directory (where ``username`` is your username).
81+
Finally, one may manually set the API key directly in the configuration file
82+
where Tidy3D looks for it. The path and file format differ slightly between
83+
platforms; see :doc:`configuration/index` for the up-to-date layout.
9884

9985

10086

0 commit comments

Comments
 (0)