リビジョン | c10ac77be5f614fec8c42094facd38606743edd8 (tree) |
---|---|
日時 | 2023-03-08 19:55:42 |
作者 | Lorenzo Isella <lorenzo.isella@gmai...> |
コミッター | Lorenzo Isella |
Code to parallelize the generation of Clifford attractors.
@@ -0,0 +1,131 @@ | ||
1 | +rm(list=ls()) | |
2 | + | |
3 | +library(Rcpp) | |
4 | + | |
5 | +library(ggplot2) | |
6 | +library(scattermore) | |
7 | +library(dplyr) | |
8 | +library(furrr) | |
9 | + | |
10 | + | |
11 | +return_cores <- function(x){ | |
12 | + | |
13 | +return(x) | |
14 | + | |
15 | +} | |
16 | + | |
17 | + | |
18 | + | |
19 | +opt = theme(legend.position = "none", | |
20 | + | |
21 | + panel.background = element_rect(fill="white"), | |
22 | + | |
23 | + axis.ticks = element_blank(), | |
24 | + | |
25 | + panel.grid = element_blank(), | |
26 | + | |
27 | + axis.title = element_blank(), | |
28 | + | |
29 | + axis.text = element_blank()) | |
30 | + | |
31 | +cppFunction('DataFrame createTrajectory(int n, double x0, double y0, | |
32 | + | |
33 | + double a1, double a2, double a3, double a4) { | |
34 | + | |
35 | + // create the columns | |
36 | + | |
37 | + NumericVector x(n); | |
38 | + | |
39 | + NumericVector y(n); | |
40 | + | |
41 | + x[0]=x0; | |
42 | + | |
43 | + y[0]=y0; | |
44 | + | |
45 | + for(int i = 1; i < n; ++i) { | |
46 | + | |
47 | + x[i] = sin(a1*y[i-1])+a3*cos(a1*x[i-1]); | |
48 | + | |
49 | + y[i] = sin(a2*x[i-1])+a4*cos(a2*y[i-1]); | |
50 | + | |
51 | + } | |
52 | + | |
53 | + // return a new data frame | |
54 | + | |
55 | + return DataFrame::create(_["x"]= x, _["y"]= y); | |
56 | + | |
57 | + } | |
58 | + | |
59 | + ') | |
60 | + | |
61 | + | |
62 | +######################################################################### | |
63 | +######################################################################### | |
64 | +######################################################################### | |
65 | + | |
66 | +n_cores <- 8 | |
67 | + | |
68 | +plan(multicore(workers=return_cores(n_cores))) | |
69 | + | |
70 | + | |
71 | +set.seed(1253) | |
72 | + | |
73 | +n_trial <- 50 | |
74 | + | |
75 | +npoints <- 10000000 | |
76 | + | |
77 | + | |
78 | +trial_seq <- 1:n_trial | |
79 | + | |
80 | + | |
81 | +## a= -1. ## -1.24458046630025 | |
82 | + | |
83 | +## b= -2. ## -1.25191834103316 | |
84 | + | |
85 | +## c= -1. ## -1.81590817030519 | |
86 | + | |
87 | +## d= -1.1 ## -1.90866735205054 | |
88 | + | |
89 | + | |
90 | +a1_seq <- runif( n_trial,-3, 3) | |
91 | +a2_seq <- runif( n_trial,-3, 3) | |
92 | +a3_seq <- runif( n_trial,-3, 3) | |
93 | +a4_seq <- runif( n_trial,-3, 3) | |
94 | + | |
95 | + | |
96 | +df_seq <- tibble(a1=a1_seq, a2=a2_seq, a3=a3_seq, | |
97 | + a4=a4_seq) | |
98 | + | |
99 | +saveRDS(df_seq, "sequence_data.RDS") | |
100 | + | |
101 | + | |
102 | + | |
103 | + | |
104 | + | |
105 | + | |
106 | +clifford_gen <- function( i){ | |
107 | + | |
108 | +a1 <- df_seq$a1[i] | |
109 | +a2 <- df_seq$a2[i] | |
110 | +a3 <- df_seq$a3[i] | |
111 | +a4 <- df_seq$a4[i] | |
112 | + | |
113 | +# Number of points to draw | |
114 | + | |
115 | +df=createTrajectory(npoints, 0, 0, a1, a2, a3, a4) | |
116 | + | |
117 | +fname <- paste("clifford_new_", i, ".png", sep="") | |
118 | + | |
119 | + | |
120 | +gpl <- ggplot(df, aes(x, y)) + | |
121 | +geom_point(color="black", shape=46, alpha=.01) + opt | |
122 | + | |
123 | +ggsave(plot=gpl, filename=fname, dpi=300) | |
124 | + | |
125 | +} | |
126 | + | |
127 | +future_map( trial_seq, clifford_gen ) | |
128 | + | |
129 | + | |
130 | + | |
131 | +print("So far so good") |