Coverage for strongcoca/response/random.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-10-26 18:44 +0000

1import numpy as np 

2from .casida import CasidaResponse 

3from .utilities import Broadening, NoArtificialBroadening 

4 

5 

6def build_random_casida(n_states: int, 

7 random_seed: int = 42, 

8 broadening: Broadening = NoArtificialBroadening(), 

9 name: str = 'Random') -> CasidaResponse: 

10 """Builds Casida response from random numbers. 

11 

12 This is intended to be used for sandbox applications and testing. 

13 

14 Parameters 

15 ---------- 

16 n_states 

17 Number of states. 

18 random_seed 

19 Seed for the random number generator. 

20 broadening 

21 Artificial broadening used for the continuous response; 

22 defaults to no broadening. 

23 name 

24 Name of response function. 

25 

26 Examples 

27 -------- 

28 

29 The following code snippet shows how to set up a randomized Casida matrix 

30 with 3 states. 

31 

32 >>> from strongcoca.response import build_random_casida 

33 >>> response = build_random_casida(n_states=3, name='Elephant', random_seed=42) 

34 """ 

35 if n_states < 1: 

36 raise ValueError('Number of states must be larger than zero') 

37 np.random.seed(seed=random_seed) 

38 D_n = np.random.rand(n_states) * n_states + 1 

39 K_nn = np.random.rand(n_states, n_states) 

40 K_nn = np.dot(K_nn, K_nn.T) # make K positive semidefinite 

41 mu_nv = np.random.rand(n_states, 3) 

42 

43 response = CasidaResponse(D_n, K_nn, mu_nv, broadening=broadening, 

44 name=name, units='eVA') 

45 return response