1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
use{
    crate::{*, traits::*},
    rand::{Rng, seq::*},
    std::{
        marker::PhantomData,
        io::Write,
        iter::*,
        collections::*,
        convert::*,
        num::*
    }
};

#[cfg(feature = "serde_support")]
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
/// Error states, that entropic sampling, or the creation of `EntropicSamplingAdaptive`
/// could encounter
pub enum EntropicErrors {
    /// # source (`WangLandauAdaptive`) was in an invalid state
    /// * did you forget to use one of the `init*` methods to initialize a valid
    /// WangLandau state? 
    InvalidWangLandau,

    /// Still in the process of gathering statistics
    /// Not enough to make an estimate
    NotEnoughStatistics,

    /// Still Gathering Statistics, this is only an estimate!
    EstimatedStatistic(Vec<f64>),

    /// Invalid trial step. Is your max_step smaller than your min_step?
    InvalidMinMaxTrialSteps,

    /// # Posible reasons
    /// * `log_density.len()` and `histogram.bin_count()` are not equal
    /// * not all values of `log_density` are finite
    InvalidLogDensity,

    /// You are trying to have a `min_best_of_count` that is 
    /// larger than the total steps you try!
    InvalidBestof,
}

/// # Entropic sampling made easy
/// > J. Lee,
/// > “New Monte Carlo algorithm: Entropic sampling,”
/// > Phys. Rev. Lett. 71, 211–214 (1993),
/// > DOI: [10.1103/PhysRevLett.71.211](https://doi.org/10.1103/PhysRevLett.71.211)
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct EntropicSamplingAdaptive<Hist, R, E, S, Res, T>
{
    rng: R,
    trial_list: Vec<usize>,
    best_of_steps: Vec<usize>,
    min_best_of_count: usize,
    best_of_threshold: f64,
    ensemble: E,
    steps: Vec<S>,
    step_res_marker: PhantomData<Res>,
    accepted_step_hist: Vec<usize>,
    rejected_step_hist: Vec<usize>,
    total_steps_rejected: usize,
    total_steps_accepted: usize,
    wl_steps_accepted: usize,
    wl_steps_rejected: usize,
    min_step: usize,
    counter: usize,
    step_count: usize,
    step_goal: usize,
    histogram: Hist,
    log_density: Vec<f64>,
    old_energy: T,
    old_bin: usize,
    adjust_bestof_every: usize,
}

impl<Hist, R, E, S, Res, Energy> GlueAble<Hist> for EntropicSamplingAdaptive<Hist, R, E, S, Res, Energy>
    where Hist: Clone + Histogram
{
    fn push_glue_entry_ignoring(
            &self, 
            job: &mut GlueJob<Hist>,
            ignore_idx: &[usize]
        ) {
        if !ignore_idx.contains(&0)
        {
            let mut missing_steps = 0;
            if self.step_count() >= self.step_goal()
            {
                missing_steps = (self.step_goal() - self.step_count()) as u64;
            }
            let rejected = self.total_entr_steps_rejected() as u64;
            let accepted = self.total_entr_steps_accepted() as u64;

            let stats = IntervalSimStats{
                sim_progress: SimProgress::MissingSteps(missing_steps),
                interval_sim_type: SimulationType::Entropic,
                rejected_steps: rejected,
                accepted_steps: accepted,
                replica_exchanges: None,
                proposed_replica_exchanges: None,
                merged_over_walkers: NonZeroUsize::new(1).unwrap()
            };

            let glue_entry = GlueEntry{
                hist: self.hist().clone(),
                prob: self.log_density_refined(),
                log_base: LogBase::BaseE,
                interval_stats: stats
            };
            job.collection.push(glue_entry);
        }
    }
}

impl<Hist, R, E, S, Res, T> TryFrom<WangLandauAdaptive<Hist, R, E, S, Res, T>>
    for EntropicSamplingAdaptive<Hist, R, E, S, Res, T>
    where 
        Hist: Histogram,
        R: Rng
{
    type Error = EntropicErrors;
    fn try_from(mut wl: WangLandauAdaptive<Hist, R, E, S, Res, T>) -> Result<Self, Self::Error> {
        if wl.old_bin.is_none() || wl.old_energy.is_none() {
            return Err(EntropicErrors::InvalidWangLandau);
        }
        let wl_steps_rejected = wl.total_steps_rejected();
        let wl_steps_accepted = wl.total_steps_accepted();
        wl.accepted_step_hist
            .iter_mut()
            .for_each(|v| *v = 0);
        wl.rejected_step_hist
            .iter_mut()
            .for_each(|v| *v = 0);
        wl.best_of_steps.clear();
        wl.histogram.reset();
        wl.trial_list.shuffle(&mut wl.rng);
        
        Ok(
            Self{
                wl_steps_rejected,
                wl_steps_accepted,
                counter: 0,
                steps: wl.steps,
                step_res_marker: wl.step_res_marker,
                log_density: wl.log_density,
                old_energy: wl.old_energy.unwrap(),
                old_bin: wl.old_bin.unwrap(),
                accepted_step_hist: wl.accepted_step_hist,
                rejected_step_hist: wl.rejected_step_hist,
                total_steps_accepted: 0,
                total_steps_rejected: 0,
                min_step: wl.min_step,
                min_best_of_count: wl.min_best_of_count,
                best_of_steps: wl.best_of_steps,
                best_of_threshold: wl.best_of_threshold,
                rng: wl.rng,
                trial_list: wl.trial_list,
                ensemble: wl.ensemble,
                step_count: 0,
                step_goal: wl.step_count,
                histogram: wl.histogram,
                adjust_bestof_every: 10usize.max(4 * wl.check_refine_every),
            }   
        )
    }
}

impl<Hist, R, E, S, Res, T> EntropicSamplingAdaptive<Hist, R, E, S, Res, T>
{

    /// # Current state of the Ensemble
    #[inline]
    pub fn ensemble(&self) -> &E
    {
        &self.ensemble
    }

    /// # Energy of ensemble
    /// * assuming `energy_fn` (see `self.entropic_step` etc.) 
    /// is deterministic and will allways give the same result for the same ensemble,
    /// this returns the energy of the current ensemble
    #[inline]
    pub fn energy(&self) -> &T
    {
        &self.old_energy
    }

    /// # Number of entropic steps done until now
    /// * will be reset by [`self.refine_estimate`](#method.refine_estimate)
    #[inline]
    pub fn step_count(&self) -> usize
    {
        self.step_count
    }

    /// # Number of entropic steps to be performed
    /// * if `self` was created from `WangLandauAdaptive`,
    /// `step_goal` will be equal to the number of WangLandau steps, that were performed
    #[inline]
    pub fn step_goal(&self) -> usize
    {
        self.step_goal
    }

    /// # Number of entropic steps to be performed
    /// * set the number of steps to be performed by entropic sampling
    #[inline]
    pub fn set_step_goal(&mut self, step_goal: usize){
        self.step_goal = step_goal;
    }

    /// # Smallest possible markov step (`m_steps` of MarkovChain trait) by entropic step
    #[inline]
    pub fn min_step_size(&self) -> usize
    {
        self.min_step
    }

    /// # Largest possible markov step (`m_steps` of MarkovChain trait) by entropic step
    #[inline]
    pub fn max_step_size(&self) -> usize 
    {
        self.min_step + self.accepted_step_hist.len() - 1
    }

    /// # Currently used best of
    /// * might have length 0, if statistics are still being gathered
    /// * otherwise this contains the step sizes, from which the next stepsize
    /// is drawn uniformly
    #[inline]
    pub fn best_of_steps(&self) -> &Vec<usize>
    {
        &self.best_of_steps
    }

    /// # Fraction of steps accepted since the statistics were reset the last time
    /// * (steps accepted since last reset) / (steps since last reset)
    /// * `NaN` if no steps were performed yet
    pub fn fraction_accepted_current(&self) -> f64 {
        let accepted: usize = self.accepted_step_hist.iter().sum();
        let total = accepted + self.rejected_step_hist.iter().sum::<usize>();
        if total == 0 {
            f64::NAN
        } else {
            accepted as f64 / total as f64
        }
    }

    /// # total number of entropic steps, that were accepted
    pub fn total_entr_steps_accepted(&self) -> usize
    {
        self.total_steps_accepted 
            + self.accepted_step_hist
                .iter()
                .sum::<usize>()
    }

    /// # total number of entropic steps, that were rejected
    pub fn total_entr_steps_rejected(&self) -> usize
    {
        self.total_steps_rejected
            + self.rejected_step_hist
                .iter()
                .sum::<usize>()
    }

    /// # Fraction of steps accepted since the creation of `self`
    /// * `NaN` if no steps were performed yet
    pub fn fraction_accepted_total_entropic(&self) -> f64 {
        let total_acc = self.total_entr_steps_accepted();
        let total_steps = total_acc + self.total_entr_steps_rejected();

        if total_steps == 0 {
            f64::NAN
        } else {
            total_acc as f64 / total_steps as f64
        }
    }

    /// * returns the (non normalized) log_density estimate log(P(E)), with which the simulation was started
    /// * if you created this from a WangLandau simulation, this is the result of the WangLandau Simulation
    pub fn log_density_estimate(&self) -> &Vec<f64>
    {
        &self.log_density
    }

    /// calculates the (non normalized) log_density estimate log(P(E)) according to the [paper](#entropic-sampling-made-easy)
    pub fn log_density_refined(&self) -> Vec<f64> 
    where Hist: Histogram{
        let mut log_density = Vec::with_capacity(self.log_density.len());
        log_density.extend(
            self.log_density
                .iter()
                .zip(self.histogram.hist().iter())
                .map(
                    |(&log_p, &h)|
                    {
                        if h == 0 {
                            log_p
                        } else {
                            log_p + (h as f64).ln()
                        }
                    }
                )
        );
        log_density
    }

    /// # Return current state of histogram
    pub fn hist(&self) -> &Hist
    {
        &self.histogram
    }
}
impl<Hist, R, E, S, Res, T> EntropicSamplingAdaptive<Hist, R, E, S, Res, T>
where Hist: Histogram,
    R: Rng
{

    /// # Creates EntropicSamplingAdaptive from a `WangLandauAdaptive` state
    /// * `WangLandauAdaptive` state needs to be valid, i.e., you must have called one of the `init*` methods
    /// - this ensures, that the members `old_energy` and `old_bin` are not `None`
    pub fn from_wl_adaptive(wl: WangLandauAdaptive<Hist, R, E, S, Res, T>) -> Result<Self, EntropicErrors>
    {
        wl.try_into()
    }




    /// # Calculates `self.log_density_refined` and uses that as estimate for a the entropic sampling simulation
    /// * returns old estimate
    /// # prepares `self` for a new entropic simulation
    /// * sets new estimate for log(P(E))
    /// * resets statistic gathering
    /// * resets step_count
    pub fn refine_estimate(&mut self) -> Vec<f64>
    {
        let mut estimate = self.log_density_refined();
        std::mem::swap(&mut estimate, &mut self.log_density);
        self.counter = 0;
        self.step_count = 0;
        self.best_of_steps.clear();
        self.histogram.reset();
        self.trial_list.shuffle(&mut self.rng);

        self.total_steps_accepted += self.accepted_step_hist.iter().sum::<usize>();
        self.accepted_step_hist
            .iter_mut()
            .for_each(|entry| *entry = 0);

        self.total_steps_rejected += self.rejected_step_hist.iter().sum::<usize>();
        self.rejected_step_hist
            .iter_mut()
            .for_each(|entry| *entry = 0);

        estimate
    }

    /// # How often to adjust `bestof_steps`?
    /// * if you try to set a value smaller 10, it will be set to 10
    /// * will reevalute the statistics every `adjust_bestof_every` steps,
    /// - this will not start new statistics gathering but just trigger a reevaluation of
    /// the gathered statistics (should be O(max_stepsize - min_stepsize))
    #[inline]
    pub fn set_adjust_bestof_every(&mut self, adjust_bestof_every: usize)
    {
        self.adjust_bestof_every = adjust_bestof_every.max(10);
    }

    /// Is the simulation in the process of rebuilding the statistics,
    /// i.e., is it currently trying many differnt step sizes?
    #[inline]
    pub fn is_rebuilding_statistics(&self) -> bool
    {
        self.counter < self.trial_list.len()
    }

    fn statistic_bin_not_hit(&self) -> bool
    {
        self.accepted_step_hist
            .iter()
            .zip(self.rejected_step_hist.iter())
            .any(|(a, r )| a + r == 0)
    }

    /// # Estimate accept/reject statistics
    /// * contains list of estimated probabilities for accepting a step of corresponding step size
    /// * list\[i\] corresponds to step size `i + self.min_step`
    /// * O(trial_step_max - trial_step_min)
    pub fn estimate_statistics(&self) -> Result<Vec<f64>, WangLandauErrors>
    {
        let calc_estimate = || {
            let mut estimate = Vec::with_capacity(self.accepted_step_hist.len());
            estimate.extend(
                self.accepted_step_hist
                    .iter()
                    .zip(
                        self.rejected_step_hist.iter()
                    ).map(|(&a, &r)|
                        {
                            a as f64 / (a + r) as f64
                        }
                    )
            );
            estimate
        };
        if self.is_rebuilding_statistics() {
            
            if self.statistic_bin_not_hit()
            {
                Err(WangLandauErrors::NotEnoughStatistics)
            } else{
                
                Err(WangLandauErrors::EstimatedStatistic(calc_estimate()))
            }
        } else {
            Ok(
                calc_estimate()
            ) 
        }
    }

    fn generate_bestof(&mut self)
    {
        let statistics = self.estimate_statistics().unwrap();
        let mut heap = BinaryHeap::with_capacity(statistics.len());
        heap.extend(statistics.into_iter()
            .enumerate()
            .map(|(index, prob)|
                {
                    ProbIndex::new(prob, index)
                }
            )
        );
        while let Some(p_i) = heap.pop() {
            if p_i.is_best_of(self.best_of_threshold) 
                || self.best_of_steps.len() < self.min_best_of_count
            {
                let step_size = p_i.index + self.min_step;
                self.best_of_steps.push(step_size);
            } else {
                break;
            }
        }
    }

    fn adjust_bestof(&mut self){
        self.best_of_steps.clear();
        self.generate_bestof();
    }

    fn get_stepsize(&mut self) -> usize {
        match self.trial_list.get(self.counter) {
            None => {
                if self.best_of_steps.is_empty(){
                    self.generate_bestof();
                }
                else if self.counter % self.adjust_bestof_every == 0 {
                    self.adjust_bestof();
                }
                *self.best_of_steps.choose(&mut self.rng).unwrap()
            },
            Some(&step_size) => {
                step_size
            },
        }
    }

    #[inline]
    fn count_accepted(&mut self, size: usize){
        self.accepted_step_hist[size - self.min_step] += 1;
        self.counter += 1;
    }

    #[inline]
    fn count_rejected(&mut self, size: usize){
        self.rejected_step_hist[size - self.min_step] += 1;
        self.counter += 1;
    }

    /// **panics** if index is invalid
    #[inline(always)]
    fn metropolis_acception_prob(&self, new_bin: usize) -> f64
    {
        
        (self.log_density[self.old_bin] - self.log_density[new_bin])
            .exp()
    }
}

impl<Hist, R, E, S, Res, T> EntropicSamplingAdaptive<Hist, R, E, S, Res, T>
where Hist: Histogram + HistogramVal<T>,
    R: Rng,
    E: MarkovChain<S, Res>,
    T: Clone,
{

    /// # Entropic sampling
    /// * performs `self.entropic_step(energy_fn)` until `condition` is false
    /// * **Note**: you have access to the current step_count (`self.step_count()`)
    /// # Parameter
    /// * `energy_fn` function calculating `Some(energy)` of the system
    /// or rather the Parameter of which you wish to obtain the probability distribution.
    /// If there are any states, for which the calculation is invalid, `None` should be returned
    /// * steps resulting in ensembles for which `energy_fn(&mut ensemble)` is `None`
    /// will always be rejected 
    /// * **Important** `energy_fn`: should be the same as used for Wang Landau, otherwise the results will be wrong!
    /// * `print_fn`: see below
    /// # Correlations
    /// * if you want to measure correlations between "energy" and other measurable quantities,
    /// use `print_fn`, which will be called after each step - use this function to write to 
    /// a file or whatever you desire
    /// * Note: You do not have to recalculate the energy, if you need it in `print_fn`:
    ///  just call `self.energy()` 
    /// * you have access to your ensemble with `self.ensemble()`
    /// * if you do not need it, you can use `|_|{}` as `print_fn`
    ///  ## Safety
    /// * While you do have mutable access to the ensemble, the energy function should not change the 
    /// ensemble in a way, which affects the next calculation of the energy
    /// * This is intended for usecases, where the energy calculation is more efficient with mutable access, e.g., through using a 
    /// buffer stored in the ensemble
    /// * Note: I chose to make this function unsafe to force users to aknowledge the (purely logical) limitations 
    /// regarding the usage of the mutable ensemble. From a programming point of view this will not lead to 
    /// any undefined behavior or such regardless of if the user fullfills the requirements
    pub unsafe fn entropic_sampling_while_unsafe<F, G, W>(
        &mut self,
        mut energy_fn: F,
        mut print_fn: G,
        mut condition: W
    ) where F: FnMut(&mut E) -> Option<T>,
        G: FnMut(&Self),
        W: FnMut(&Self) -> bool
    {
        while condition(self) {
            self.entropic_step_unsafe(&mut energy_fn);
            print_fn(self);
        }
    }

    /// # Entropic sampling
    /// * performs `self.entropic_step(energy_fn)` until `condition` is false
    /// * **Note**: you have access to the current step_count (`self.step_count()`)
    /// # Parameter
    /// * `energy_fn` function calculating `Some(energy)` of the system
    /// or rather the Parameter of which you wish to obtain the probability distribution.
    /// If there are any states, for which the calculation is invalid, `None` should be returned
    /// * steps resulting in ensembles for which `energy_fn(&mut ensemble)` is `None`
    /// will always be rejected 
    /// * **Important** `energy_fn`: should be the same as used for Wang Landau, otherwise the results will be wrong!
    /// * `print_fn`: see below
    /// # Correlations
    /// * if you want to measure correlations between "energy" and other measurable quantities,
    /// use `print_fn`, which will be called after each step - use this function to write to 
    /// a file or whatever you desire
    /// * Note: You do not have to recalculate the energy, if you need it in `print_fn`:
    ///  just call `self.energy()` 
    /// * you have access to your ensemble with `self.ensemble()`
    /// * if you do not need it, you can use `|_|{}` as `print_fn`
    pub fn entropic_sampling_while<F, G, W>(
        &mut self,
        mut energy_fn: F,
        mut print_fn: G,
        mut condition: W
    ) where F: FnMut(&E) -> Option<T>,
        G: FnMut(&Self),
        W: FnMut(&Self) -> bool
    {
        while condition(self) {
            self.entropic_step(&mut energy_fn);
            print_fn(self);
        }
    }


    /// # Entropic sampling using an accumulating markov step
    /// * performs `self.entropic_step_acc(&mut energy_fn)` until `condition(self) == false`
    /// # Parameter
    /// * `energy_fn` function calculating the energy `E` of the system
    /// (or rather the Parameter of which you wish to obtain the probability distribution)
    /// during the markov steps, which can be more efficient.
    /// * **Important** `energy_fn`: should be the same as used for Wang Landau, otherwise the results will be wrong!
    /// * `print_fn`: see below
    /// # Correlations
    /// * if you want to measure correlations between "energy" and other measurable quantities,
    /// use `print_fn`, which will be called after each step - use this function to write to 
    /// a file or whatever you desire
    /// * Note: You do not have to recalculate the energy, if you need it in `print_fn`:
    ///  just call `self.energy()` 
    /// * you have access to your ensemble with `self.ensemble()`
    /// * if you do not need it, you can use `|_|{}` as `print_fn`
    pub fn entropic_sampling_while_acc<F, G, W>(
        &mut self,
        mut energy_fn: F,
        mut print_fn: G,
        mut condition: W
    ) where F: FnMut(&E, &S, &mut T),
        G: FnMut(&Self),
        W: FnMut(&Self) -> bool
    {
        while condition(self) {
            self.entropic_step_acc(&mut energy_fn);
            print_fn(self);
        }
    }

    /// # Entropic sampling
    /// * if possible, use `self.entropic_sampling()` instead!
    /// * More powerful version of `self.entropic_sampling()`, since you now have mutable access
    /// * to access ensemble mutable, use `self.ensemble_mut()`
    /// * Note: Whatever you do with the ensemble (or self), should not change the result of the energy function, if performed again.
    /// Otherwise the results will be false!
    /// * performs `self.entropic_step_unsafe(energy_fn)` until `self.step_count == self.step_goal`
    /// # Parameter
    /// * `energy_fn` function calculating `Some(energy)` of the system
    /// or rather the Parameter of which you wish to obtain the probability distribution.
    /// If there are any states, for which the calculation is invalid, `None` should be returned
    /// * steps resulting in ensembles for which `energy_fn(&mut ensemble)` is `None`
    /// will always be rejected 
    /// * **Important** `energy_fn`: should be the same as used for Wang Landau, otherwise the results will be wrong!
    /// * `print_fn`: see below
    /// # Correlations
    /// * if you want to measure correlations between "energy" and other measurable quantities,
    /// use `print_fn`, which will be called after each step - use this function to write to 
    /// a file or whatever you desire
    /// * Note: You do not have to recalculate the energy, if you need it in `print_fn`:
    ///  just call `self.energy()` 
    /// * you have access to your ensemble with `self.ensemble()`
    /// * if you do not need it, you can use `|_|{}` as `print_fn`
    ///  ## Safety
    /// * While you do have mutable access to the ensemble, the energy function should not change the 
    /// ensemble in a way, which affects the next calculation of the energy
    /// * This is intended for usecases, where the energy calculation is more efficient with mutable access, e.g., through using a 
    /// buffer stored in the ensemble
    /// * Note: I chose to make this function unsafe to force users to aknowledge the (purely logical) limitations 
    /// regarding the usage of the mutable ensemble. From a programming point of view this will not lead to 
    /// any undefined behavior or such regardless of if the user fullfills the requirements
    pub unsafe fn entropic_sampling_unsafe<F, G>(
        &mut self,
        mut energy_fn: F,
        mut print_fn: G,
    ) where F: FnMut(&mut E) -> Option<T>,
        G: FnMut(&mut Self)
    {
        while self.step_count < self.step_goal {
            self.entropic_step_unsafe(&mut energy_fn);
            print_fn(self);
        }
    }

    /// # Entropic sampling
    /// * performs `self.entropic_step(energy_fn)` until `self.step_count == self.step_goal`
    /// # Parameter
    /// * `energy_fn` function calculating `Some(energy)` of the system
    /// or rather the Parameter of which you wish to obtain the probability distribution.
    /// If there are any states, for which the calculation is invalid, `None` should be returned
    /// * steps resulting in ensembles for which `energy_fn(&mut ensemble)` is `None`
    /// will always be rejected 
    /// * **Important** `energy_fn`: should be the same as used for Wang Landau, otherwise the results will be wrong!
    /// * `print_fn`: see below
    /// # Correlations
    /// * if you want to measure correlations between "energy" and other measurable quantities,
    /// use `print_fn`, which will be called after each step - use this function to write to 
    /// a file or whatever you desire
    /// * Note: You do not have to recalculate the energy, if you need it in `print_fn`:
    ///  just call `self.energy()` 
    /// * you have access to your ensemble with `self.ensemble()`
    /// * if you do not need it, you can use `|_|{}` as `print_fn`
    pub fn entropic_sampling<F, G>(
        &mut self,
        mut energy_fn: F,
        mut print_fn: G,
    ) where F: FnMut(&E) -> Option<T>,
        G: FnMut(&Self)
    {
        while self.step_count < self.step_goal {
            self.entropic_step(&mut energy_fn);
            print_fn(self);
        }
    }

    /// # Entropic sampling using an accumulating markov step
    /// * performs `self.entropic_step_acc(&mut energy_fn)` until `self.step_count == self.step_goal`
    /// # Parameter
    /// * `energy_fn` function calculating the energy `E` of the system
    /// (or rather the Parameter of which you wish to obtain the probability distribution)
    /// during the markov steps, which can be more efficient.
    /// * **Important** `energy_fn`: should be the same as used for Wang Landau, otherwise the results will be wrong!
    /// * `print_fn`: see below
    /// # Correlations
    /// * if you want to measure correlations between "energy" and other measurable quantities,
    /// use `print_fn`, which will be called after each step - use this function to write to 
    /// a file or whatever you desire
    /// * Note: You do not have to recalculate the energy, if you need it in `print_fn`:
    ///  just call `self.energy()` 
    /// * you have access to your ensemble with `self.ensemble()`
    /// * if you do not need it, you can use `|_|{}` as `print_fn`
    pub fn entropic_sampling_acc<F, G>(
        &mut self,
        mut energy_fn: F,
        mut print_fn: G,
    ) where F: FnMut(&E, &S, &mut T),
        G: FnMut(&Self)
    {
        while self.step_count < self.step_goal {
            self.entropic_step_acc(&mut energy_fn);
            print_fn(self);
        }
    }

    /// # Entropic step
    /// * performs a single step
    /// # Parameter
    /// * `energy_fn` function calculating `Some(energy)` of the system
    /// or rather the Parameter of which you wish to obtain the probability distribution.
    /// If there are any states, for which the calculation is invalid, `None` should be returned
    /// * steps resulting in ensembles for which `energy_fn(&mut ensemble)` is `None`
    /// will always be rejected 
    /// # Important
    /// * `energy_fn`: should be the same as used for Wang Landau, otherwise the results will be wrong!
    ///  ## Safety
    /// * While you do have mutable access to the ensemble, the energy function should not change the 
    /// ensemble in a way, which affects the next calculation of the energy
    /// * This is intended for usecases, where the energy calculation is more efficient with mutable access, e.g., through using a 
    /// buffer stored in the ensemble
    /// * Note: I chose to make this function unsafe to force users to aknowledge the (purely logical) limitations 
    /// regarding the usage of the mutable ensemble. From a programming point of view this will not lead to 
    /// any undefined behavior or such regardless of if the user fullfills the requirements
    pub unsafe fn entropic_step_unsafe<F>(
        &mut self,
        mut energy_fn: F,
    )where F: FnMut(&mut E) -> Option<T>
    {

        self.step_count += 1;
        let step_size = self.get_stepsize();


        self.ensemble.m_steps(step_size, &mut self.steps);

        let current_energy = match energy_fn(&mut self.ensemble)
        {
            Some(energy) => energy,
            None => {
                self.ensemble.steps_rejected(&self.steps);
                self.count_rejected(step_size);
                self.histogram.count_index(self.old_bin).unwrap();
                self.ensemble.undo_steps_quiet(&self.steps);
                return;
            }
        };

        self.entropic_step_helper(current_energy);
    }

    /// # Entropic step
    /// * performs a single step
    /// # Parameter
    /// * `energy_fn` function calculating `Some(energy)` of the system
    /// or rather the Parameter of which you wish to obtain the probability distribution.
    /// If there are any states, for which the calculation is invalid, `None` should be returned
    /// * steps resulting in ensembles for which `energy_fn(&mut ensemble)` is `None`
    /// will always be rejected 
    /// # Important
    /// * `energy_fn`: should be the same as used for Wang Landau, otherwise the results will be wrong!
    pub fn entropic_step<F>(
        &mut self,
        mut energy_fn: F,
    )where F: FnMut(&E) -> Option<T>
    {
        unsafe{
            self.entropic_step_unsafe(|e| energy_fn(e))
        }
    }

    /// # Accumulating entropic step
    /// * performs a single step
    /// # Parameter
    /// * `energy_fn` function calculating the energy `E` of the system
    /// (or rather the Parameter of which you wish to obtain the probability distribution)
    /// during the markov steps, which can be more efficient.
    /// # Important
    /// * `energy_fn`: should be the same as used for Wang Landau, otherwise the results will be wrong!
    pub fn entropic_step_acc<F>(
        &mut self,
        energy_fn: F,
    )
        where F: FnMut(&E, &S, &mut T)
    {
        self.step_count += 1;
        let step_size = self.get_stepsize();

        let mut current_energy = self.old_energy.clone();
        self.ensemble.m_steps_acc(
            step_size,
            &mut self.steps,
            &mut current_energy,
            energy_fn
        );
        self.entropic_step_helper(current_energy);
    }

    fn entropic_step_helper(&mut self, current_energy: T)
    {
        let step_size = self.steps.len();
        
        match self.histogram.get_bin_index(&current_energy)
        {
            Ok(current_bin) => {
                let accept_prob = self.metropolis_acception_prob(current_bin);

                if self.rng.gen::<f64>() > accept_prob {
                    // reject step
                    self.ensemble.steps_rejected(&self.steps);
                    self.count_rejected(step_size);
                    self.ensemble.undo_steps_quiet(&self.steps);
                } else {
                    // accept step
                    self.ensemble.steps_accepted(&self.steps);
                    self.count_accepted(step_size);
                    
                    self.old_energy = current_energy;
                    self.old_bin = current_bin;
                }
            },
            _  => {
                // invalid step -> reject
                self.ensemble.steps_rejected(&self.steps);
                self.count_rejected(step_size);
                self.ensemble.undo_steps_quiet(&self.steps);
            }
        };
        
        self.histogram.count_index(self.old_bin).unwrap();
    }
}

impl<Hist, R, E, S, Res, T> Entropic for EntropicSamplingAdaptive<Hist, R, E, S, Res, T>
where Hist: Histogram,
    R: Rng
{
    /// # Number of entropic steps done until now
    /// * will be reset by [`self.refine_estimate`](#method.refine_estimate)
    #[inline]
    fn step_counter(&self) -> usize
    {
        self.step_count
    }

    fn total_steps_accepted(&self) -> usize {
        self.total_entr_steps_accepted() + self.wl_steps_accepted
    }

    fn total_steps_rejected(&self) -> usize {
        self.total_entr_steps_rejected() + self.wl_steps_rejected
    }

    /// # Number of entropic steps to be performed
    /// * if `self` was created from `WangLandauAdaptive`,
    /// `step_goal` will be equal to the number of WangLandau steps, that were performed
    #[inline]
    fn step_goal(&self) -> usize
    {
        self.step_goal
    }

    fn log_density(&self) -> Vec<f64> {
        self.log_density_refined()
    }

    fn write_log<W: Write>(&self, mut w: W) -> Result<(), std::io::Error> {
        writeln!(w,
            "#Acceptance prob_total: {}\n#Acceptance prob current: {}\n#total_steps: {}",
            self.fraction_accepted_total(),
            self.fraction_accepted_current(),
            self.step_counter(),
        )?;

        writeln!(w, "#min_step_size {}", self.min_step_size())?;
        writeln!(w, "#max_step_size {}", self.max_step_size())?;

        write!(w, "#Current acception histogram:")?;
        for val in self.accepted_step_hist.iter()
        {
            write!(w, " {}", val)?;
        }

        write!(w, "\n#Current rejection histogram:")?;
        for val in self.rejected_step_hist.iter()
        {
            write!(w, " {}", val)?;
        }

        writeln!(w, "\n#bestof threshold: {}", self.best_of_threshold)?;
        writeln!(w, "#min_bestof_count: {}", self.min_best_of_count)?;
        write!(w, "\n#Current_Bestof:")?;

        for val in self.best_of_steps.iter()
        {
            write!(w, " {}", val)?;
        }

        write!(w, "#current_statistics_estimate:")?;
        let estimate = self.estimate_statistics();
        match estimate {
            Ok(estimate) => {
                for val in estimate
                {
                    write!(w, " {}", val)?;
                }
                writeln!(w)
            },
            _ => {
                writeln!(w, " None")
            }
        }
    }
}


impl<Hist, R, E, S, Res, Energy> EntropicEnergy<Energy> for EntropicSamplingAdaptive<Hist, R, E, S, Res, Energy>
where Hist: Histogram,
    R: Rng,
{
    /// # Energy of ensemble
    /// * assuming `energy_fn` (see `self.entropic_step` etc.) 
    /// is deterministic and will allways give the same result for the same ensemble,
    /// this returns the energy of the current ensemble
    #[inline]
    fn energy(&self) -> &Energy
    {
        &self.old_energy
    }
}

impl<Hist, R, E, S, Res, Energy> EntropicHist<Hist> for EntropicSamplingAdaptive<Hist, R, E, S, Res, Energy>
where Hist: Histogram,
    R: Rng,
{
    #[inline]
    fn hist(&self) -> &Hist
    {
        &self.histogram
    }
}

impl<Hist, R, E, S, Res, Energy> EntropicEnsemble<E> for EntropicSamplingAdaptive<Hist, R, E, S, Res, Energy>
where Hist: Histogram,
    R: Rng,
{
    fn ensemble(&self) -> &E {
        &self.ensemble
    }

    unsafe fn ensemble_mut(&mut self) -> &mut E {
        &mut self.ensemble
    }
}

impl<Hist, R, E, S, Res, Energy> HasRng<R> for EntropicSamplingAdaptive<Hist, R, E, S, Res, Energy>
    where R: Rng,
{
    fn rng(&mut self) -> &mut R {
        &mut self.rng
    }

    fn swap_rng(&mut self, rng: &mut R) {
        std::mem::swap(&mut self.rng, rng);
    }
}