-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
60 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,55 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Chemistry.Orbit where | ||
|
||
import Data.List.NonEmpty(NonEmpty) | ||
import Control.Monad.Zip(mzipWith) | ||
import Data.List(sort) | ||
import Data.List.NonEmpty(NonEmpty((:|))) | ||
import Data.Char.Small(asSup) | ||
import Data.Foldable(toList) | ||
import qualified Data.Text as T | ||
import Data.Text(Text, pack, toLower) | ||
|
||
data Orbit = S | P | D | F | G deriving (Enum, Eq, Ord, Read, Show) | ||
data OrbitElement = OrbitElement Int Orbit Int deriving (Eq, Ord, Read, Show) | ||
newtype OrbitalConfig = OrbitalConfig (NonEmpty (NonEmpty Int)) deriving (Eq, Ord, Read, Show) | ||
|
||
orbitElementToText :: OrbitElement -> Text | ||
orbitElementToText (OrbitElement i o n) = pack (show i) <> toLower (pack (show o)) <> asSup n | ||
|
||
orbitConfigs :: OrbitalConfig -> NonEmpty (NonEmpty OrbitElement) | ||
orbitConfigs (OrbitalConfig es) = mzipWith ((`mzipWith` (S :| [P ..])) . OrbitElement) (1 :| [2..]) es | ||
|
||
orbitElement :: Foldable f => f OrbitElement -> OrbitalConfig | ||
orbitElement _ = OrbitalConfig ((0 :| []) :| []) | ||
|
||
orbitalConfigToText :: OrbitalConfig -> Text | ||
orbitalConfigToText = T.unwords . toList . fmap (T.unwords . toList . fmap orbitElementToText) . orbitConfigs | ||
|
||
totalElectrons :: OrbitalConfig -> Int | ||
totalElectrons (OrbitalConfig vs) = sum (fmap sum vs) | ||
|
||
toArrowText :: Int -> Int -> String | ||
toArrowText n k = concat (replicate f "\8645\8414" ++ [ "\8593\8414" | h == 1 ] ++ replicate ((maxElectronsForOrbit' n `div` 2) - f - h) " \8414") | ||
where ~(f, h) = k `divMod` 2 | ||
toArrowText :: OrbitElement -> String | ||
toArrowText (OrbitElement m n k) = "" -- concat (replicate f "\8645\8414" ++ [ "\8593\8414" | h == 1 ] ++ replicate ((maxElectronsForOrbit' n `div` 2) - f - h) " \8414") | ||
-- where ~(f, h) = k `divMod` 2 | ||
|
||
_orbitPlaces :: Int -> Int | ||
_orbitPlaces = (1+) . (2*) | ||
|
||
maxElectronsForOrbit' :: Int -> Int | ||
maxElectronsForOrbit' = (2+) . (4*) | ||
maxElectronsForOrbit' = (2*) . _orbitPlaces | ||
|
||
maxElectronsForOrbit :: Orbit -> Int | ||
maxElectronsForOrbit = maxElectronsForOrbit' . fromEnum | ||
|
||
electronsToOrbitals :: Int -> [(Int, Orbit, Int)] | ||
electronsToOrbitals = fill 0 0 | ||
electronsToOrbitals' :: Int -> [OrbitElement] | ||
electronsToOrbitals' = fill 1 0 | ||
where fill _ _ 0 = [] | ||
fill m 0 k = (m+1, S, d) : fill (m - (m-1) `div` 2) ((m+1) `div` 2) (k-d) | ||
fill m 0 k = OrbitElement m S d : fill (m - dm + 1) dm (k-d) | ||
where d = min 2 k | ||
fill m n k = (m+1, toEnum n, d) : fill (m+1) (n-1) (k-d) | ||
dm = m `div` 2 | ||
fill m n k = OrbitElement m (toEnum n) d : fill (m+1) (n-1) (k-d) | ||
where d = min (maxElectronsForOrbit' n) k | ||
|
||
-- electronsPerShell :: Element -> [Int] | ||
-- electronsPerShell = map sum . molecularOrbital | ||
|
||
electronsToOrbitals :: Int -> [OrbitElement] | ||
electronsToOrbitals = sort . electronsToOrbitals' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Chemistry.Valence where | ||
|
||
import Chemistry.Element(Element(B, C, N, O, P, S, F, Cl, Br, I, At)) | ||
|
||
smilesValence :: Element -> [Int] | ||
smilesValence B = [3] | ||
smilesValence C = [4] | ||
smilesValence N = [3,5] | ||
smilesValence O = [2] | ||
smilesValence P = [3,5] | ||
smilesValence S = [2,4,6] | ||
smilesValence F = [1] | ||
smilesValence Cl = [1] | ||
smilesValence Br = [1] | ||
smilesValence I = [1] | ||
smilesValence At = [1] | ||
smilesValence _ = [] |